1 準(zhǔn)備數(shù)據(jù)
1>創(chuàng)建數(shù)據(jù)表
?創(chuàng)建絲芙蘭數(shù)據(jù)庫
create database SEPHORE charset=utf8;
?創(chuàng)建一個商品goods數(shù)據(jù)表
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, -- 默認(rèn)顯示 顯示不顯示
? ? is_saleoff bit not null default 0
)
2>插入數(shù)據(jù)
?在 goods商品表插入商品
insert into goods values
(0,'SK-II護膚精華露“神仙水SK-II
FACIAL TREATMENT ESSENCE VALUE SET”+大眼眼霜惠選套組',
'神仙水+眼霜','SK-II','2130',default,default);
要插入多個數(shù)據(jù) 這里只列出一個
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; 分組效率低 數(shù)據(jù)復(fù)雜建議用
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 求所有產(chǎn)品的平均價格,并且保留兩位小數(shù)
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 查詢每種類型的商品中 最貴、最便宜晒骇、平均價、數(shù)量
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;
-- 創(chuàng)建商品分類表
create table if not exists goods_cates(
? ? id int unsigned primary key auto_increment,
? ? name varchar(40) not null
);
-- 創(chuàng)建商品品牌表
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;
-- 將分組結(jié)果寫入到goods_cates數(shù)據(jù)表
insert into goods_cates (name) select cate_name from
goods group by cate_name;
-- 將分組結(jié)果寫入到goods_brands數(shù)據(jù)表
insert into goods_brands (name) select brand_name from
goods group by brand_name;
-- 通過goods_cates數(shù)據(jù)表來更新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數(shù)據(jù)表來更新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 (
'路由器'),('交換機'),('網(wǎng)卡');
insert into goods_brands(name) values
('海爾'),('清華同方'),('神舟');
-- 在 goods 數(shù)據(jù)表中寫入任意記錄
insert into goods (name,cate_name,brand_name,price)
values('LaserJet Pro P1606dn 黑白激光打印機', 12, 4,'1849');
-- 查詢所有商品的詳細(xì)信息 (通過內(nèi)連接)
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;
-- 查詢所有商品的詳細(xì)信息 (通過左連接)
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;
-- 如何防止無效信息的插入,就是可以在插入前判斷類型或者品牌名稱是否存在呢? 可以使用之前講過的外鍵來解決
-- 外鍵約束:對數(shù)據(jù)的有效性進行驗證
-- 關(guān)鍵字: foreign key,只有 innodb數(shù)據(jù)庫引擎 支持外鍵約束
對于已經(jīng)存在的數(shù)據(jù)表 如何更新外鍵約束
-- 給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 添加外鍵失敗
-- 會出現(xiàn)1452錯誤
-- 錯誤原因:已經(jīng)添加了一個不存在的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';
?修改表結(jié)構(gòu)
這是改類別表的涂佃,把名字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;