MySQL与Python交互

准备数据

1>创建数据表

 创建丝芙兰数据库

create database SEPHORE charset=utf8;

 创建一个商品goods数据表

create table goods(

    id int unsigned primary key auto_increment not null,

    name varchar(150) not null, -- 商品名称

    cate_name varchar(40) not null, -- 商品类别

    brand_name varchar(40) not null, -- 品牌

    price decimal(10,3) not null default 0,-- 价格

    is_show bit not null default 1, -- 默认显示 显示不显示

    is_saleoff bit not null default 0

)

2>插入数据

 在 goods商品表插入商品

insert into goods values

(0,'SK-II护肤精华露“神仙水SK-II

FACIAL TREATMENT ESSENCE VALUE SET”+大眼眼霜惠选套组',

'神仙水+眼霜','SK-II','2130',default,default);

要插入多个数据 这里只列出一个

2 SQL语句演练

1 查询类型cate_name为 '口红' 的商品名称、价格

select * from goods where cate_name = '口红';

select name as 商品名称,price as 商品价格 from goods where cate_name = '口红';

2 显示商品种类

select distinct cate_name from goods;  去重

select cate_name from goods group by cate_name; 分组效率低 数据复杂建议用

3 查每个种类的商品名称

select cate_name,group_concat(name) from goods group by cate_name;

4 删除goods表

drop table goods;

5 查询类型cate_name为 '眼霜' 的商品名称、价格

select name,price from goods where cate_name = '眼霜';

6 显示商品的种类

select cate_name from goods group by cate_name;

7 求所有产品的平均价格,并且保留两位小数

select round(avg(price),2) from goods;

8 显示每种商品的平均价格

select cate_name from goods group by cate_name; -- 商品种类

select cate_name,avg(price) from goods group by cate_name;

9 查询每种类型的商品中 最贵、最便宜、平均价、数量

select cate_name,max(price),min(price),avg(price),count(*) from goods group by cate_name;

10 查询所有价格大于平均价格的商品,并且按价格降序排序

select avg(price) from goods;

select * from goods where price>987.2;

-- 创建商品分类表

create table if not exists goods_cates(

    id int unsigned primary key auto_increment,

    name varchar(40) not null

);

-- 创建商品品牌表

create table if not exists goods_brands(

    id int unsigned primary key auto_increment,

    name varchar(40) not null

);

-- 查询goods表中商品的种类

select cate_name from goods group by cate_name;

-- 查询goods表中商品的品牌

select brand_name from goods group by brand_name;

-- 将分组结果写入到goods_cates数据表

insert into goods_cates (name) select cate_name from

goods group by cate_name;

-- 将分组结果写入到goods_brands数据表

insert into goods_brands (name) select brand_name from

goods group by brand_name;

-- 通过goods_cates数据表来更新goods表

update goods as g inner join goods_cates as c on

g.cate_name=c.name set g.cate_name=c.id;

-- 通过goods_cates数据表来更新goods表

update goods as g inner join goods_brands as b on

g.brand_name=b.name set g.brand_name=b.id;

-- 分别在 goods_cates 和 goods_brands表中插入记录

insert into goods_cates(name) values (

'路由器'),('交换机'),('网卡');

insert into goods_brands(name) values

('海尔'),('清华同方'),('神舟');

-- 在 goods 数据表中写入任意记录

insert into goods (name,cate_name,brand_name,price)

values('LaserJet Pro P1606dn 黑白激光打印机', 12, 4,'1849');

-- 查询所有商品的详细信息 (通过内连接)

select g.id,g.name,c.name,b.name,g.price from goods as g

inner join goods_cates as c on g.cate_id=c.id

inner join goods_brands as b on g.brand_id=b.id;

-- 查询所有商品的详细信息 (通过左连接)

select g.id,g.name,c.name,b.name,g.price from goods as g

left join goods_cates as c on g.cate_id=c.id

left join goods_brands as b on g.brand_id=b.id;

-- 如何防止无效信息的插入,就是可以在插入前判断类型或者品牌名称是否存在呢? 可以使用之前讲过的外键来解决

-- 外键约束:对数据的有效性进行验证

-- 关键字: foreign key,只有 innodb数据库引擎 支持外键约束

对于已经存在的数据表 如何更新外键约束

-- 给brand_id 添加外键约束成功

alter table goods add foreign key (brand_id)

references goods_brands(id);

-- 给cate_id 添加外键约束成功

alter table goods add foreign key (cate_id)

references goods_cates(id);

-- 给cate_id 添加外键失败

-- 会出现1452错误

-- 错误原因:已经添加了一个不存在的cate_id值12,因此需要先删除

alter table goods add foreign key (cate_id) references

goods_cates(id);

insert into goods_brands (name) select brand_name from

goods group by brand_name;

select brand_name from goods group by brand_name;

insert into goods_cates (name) select cate_name from

goods group by cate_name;

Query OK, 15 rows affected (0.03 sec)

Records: 15  Duplicates: 0  Warnings: 0

update goods as g inner join goods_cates as c on

g.cate_name=c.name set g.cate_name=c.id;

update goods Type='int(10) unsigned' where Field='cate_name';

 修改表结构

这是改类别表的,把名字cate_name 换成cate_id,类型是 int unsigned

alter table goods 

change cate_name cate_id int unsigned not null;

alter table goods 

change brand_name brand_id int unsigned not null;

 取消外键

alter table goods drop foreign key goods_ibfk_1;

alter table goods drop foreign key goods_ibfk_2;

你可能感兴趣的:(MySQL与Python交互)