一、准备数据
1.1 创建数据表
create database jing_dong charset = utf8;
use jing_dong;
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
) ;
1.2 插入数据
insert into goods values ( 0 , 'r510vc 15.6英寸笔记本' , '笔记本' , '华硕' , '3399' , default , default ) ;
insert into goods values ( 0 , 'y400n 14.0英寸笔记本电脑' , '笔记本' , '联想' , '4999' , default , default ) ;
insert into goods values ( 0 , 'g150th 15.6英寸游戏本' , '游戏本' , '雷神' , '8499' , default , default ) ;
insert into goods values ( 0 , 'x550cc 15.6英寸笔记本' , '笔记本' , '华硕' , '2799' , default , default ) ;
insert into goods values ( 0 , 'x240 超极本' , '超级本' , '联想' , '4880' , default , default ) ;
insert into goods values ( 0 , 'u330p 13.3英寸超极本' , '超级本' , '联想' , '4299' , default , default ) ;
insert into goods values ( 0 , 'svp13226scb 触控超极本' , '超级本' , '索尼' , '7999' , default , default ) ;
insert into goods values ( 0 , 'ipad mini 7.9英寸平板电脑' , '平板电脑' , '苹果' , '1998' , default , default ) ;
insert into goods values ( 0 , 'ipad air 9.7英寸平板电脑' , '平板电脑' , '苹果' , '3388' , default , default ) ;
insert into goods values ( 0 , 'ipad mini 配备 retina 显示屏' , '平板电脑' , '苹果' , '2788' , default , default ) ;
insert into goods values ( 0 , 'ideacentre c340 20英寸一体电脑 ' , '台式机' , '联想' , '3499' , default , default ) ;
insert into goods values ( 0 , 'vostro 3800-r1206 台式电脑' , '台式机' , '戴尔' , '2899' , default , default ) ;
insert into goods values ( 0 , 'imac me086ch/a 21.5英寸一体电脑' , '台式机' , '苹果' , '9188' , default , default ) ;
insert into goods values ( 0 , 'at7-7414lp 台式电脑 linux )' , '台式机' , '宏碁' , '3699' , default , default ) ;
insert into goods values ( 0 , 'z220sff f4f06pa工作站' , '服务器/工作站' , '惠普' , '4288' , default , default ) ;
insert into goods values ( 0 , 'poweredge ii服务器' , '服务器/工作站' , '戴尔' , '5388' , default , default ) ;
insert into goods values ( 0 , 'mac pro专业级台式电脑' , '服务器/工作站' , '苹果' , '28888' , default , default ) ;
insert into goods values ( 0 , 'hmz-t3w 头戴显示设备' , '笔记本配件' , '索尼' , '6999' , default , default ) ;
insert into goods values ( 0 , '商务双肩背包' , '笔记本配件' , '索尼' , '99' , default , default ) ;
insert into goods values ( 0 , 'x3250 m4机架式服务器' , '服务器/工作站' , 'ibm' , '6888' , default , default ) ;
insert into goods values ( 0 , '商务双肩背包' , '笔记本配件' , '索尼' , '99' , default , default ) ;
二、SQL演练
查询类型cate_name为 ‘超极本’ 的商品名称、价格
select name, price from goods where cate_name = '超级本' ;
select cate_name from goods group by cate_name;
select round ( avg ( price) , 2 ) as avg_price from goods;
select cate_name, avg ( price) from goods group by cate_name;
select cate_name, max ( price) , min ( price) , avg ( price) , count ( * ) from goods group by cate_name;
查询所有价格大于平均价格的商品,并且按价格降序排序
select id, name, price from goods
where price > ( select round ( avg ( price) , 2 ) as avg_price from goods)
order by price desc ;
select * from goods
inner join
(
select
cate_name,
max ( price) as max_price,
min ( price) as min_price,
avg ( price) as avg_price,
count ( * ) from goods group by cate_name
) as goods_new_info
on goods. cate_name= goods_new_info. cate_name and goods. price= goods_new_info. max_price;
三、数据库的设计
3.1 创建 “商品分类”" 表(拆表)
create table if not exists goods_cates(
id int unsigned primary key auto_increment ,
name varchar ( 40 ) not null
) ;
select cate_name from goods group by cate_name;
insert into goods_cates ( name) select cate_name from goods group by cate_name;
3.1.1 同步表数据
通过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;
3.2 创建 “商品品牌表” 表
通过create…select来创建数据表并且同时写入记录,一步到位
create table goods_brands (
id int unsigned primary key auto_increment ,
name varchar ( 40 ) not null ) select brand_name as name from goods group by brand_name;
3.2.1 同步数据
通过goods_brands数据表来更新goods数据表
update goods as g inner join goods_brands as b on g. brand_name= b. name set g. brand_name= b. id;
3.3 修改表结构
查看 goods 的数据表结构,会发现 cate_name 和 brand_name对应的类型为 varchar
但是存储的都是数字
desc goods;
alter table goods
change cate_name cate_id int unsigned not null ,
change brand_name brand_id int unsigned not null ;
3.4 外键
分别在 goods_cates 和 goods_brands表中插入记录
insert into goods_cates( name) values ( '路由器' ) , ( '交换机' ) , ( '网卡' ) ;
insert into goods_brands( name) values ( '海尔' ) , ( '清华同方' ) , ( '神舟' ) ;
insert into goods ( name, cate_id, brand_id, 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;
3.4.1 更新外键约束
如何防止无效信息的插入,就是可以在插入前判断类型或者品牌名称是否存在呢? 可以使用之前讲过的外键来解决 外键约束:对数据的有效性进行验证 关键字: foreign key,只有 innodb数据库引擎, 支持外键约束 对于已经存在的数据表 如何更新外键约束
alter table goods add foreign key ( brand_id) references goods_brands( id) ;
alter table goods add foreign key ( cate_id) references goods_cates( id) ;
delect from goods where id= 22 ;
3.4.2 创建数据表的时候就设置外键约束
注意: goods 中的 cate_id 的类型一定要和 goods_cates 表中的 id 类型一致
create table goods(
id int primary key auto_increment not null ,
name varchar ( 40 ) default '' ,
price decimal ( 5 , 2 ) ,
cate_id int unsigned ,
brand_id int unsigned ,
is_show bit default 1 ,
is_saleoff bit default 0 ,
foreign key ( cate_id) references goods_cates( id) ,
foreign key ( brand_id) references goods_brands( id)
) ;
3.4.3 删除外键约束
show create table goods;
alter table goods drop foreign key goods_ibfk_1;
alter table goods drop foreign key goods_ibfk_2;
在实际开发中,很少会使用到外键约束,会极大的降低表更新的效率
3.5 创建 “顾客” 表
create table customer(
id int unsigned auto_increment primary key not null ,
name varchar ( 30 ) not null ,
addr varchar ( 100 ) ,
tel varchar ( 11 ) not null
) ;
3.6 创建 “订单” 表
create table orders(
id int unsigned auto_increment primary key not null ,
order_date_time datetime not null ,
customer_id int unsigned ,
foreign key ( customer_id) references customer( id)
) ;
3.7 创建 “订单详情” 表
create table order_detail(
id int unsigned auto_increment primary key not null ,
order_id int unsigned not null ,
goods_id int unsigned not null ,
quantity tinyint unsigned not null ,
foreign key ( order_id) references orders( id) ,
foreign key ( goods_id) references goods( id)
) ;
3.8 说明
以上创建表的顺序是有要求的,即如果goods表中的外键约束用的是goods_cates或者是goods_brands,那么就应该先创建这2个表,否则创建goods会失败 创建外键时,一定要注意类型要相同,否则失败。