实战演练
goods表练习:
加载数据:
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
) ; 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 ) ;
开始实战:
select name, price from goods where cate_name= "超级本" ;
select cate_name from goods group by cate_name;
select distinct cate_name from goods;
select round ( avg ( price) , 2 ) from goods;
select cate_name, round ( avg ( price) , 2 ) from goods group by cate_name;
select max ( price) , min ( price) , avg ( price) , count ( * ) from goods group by cate_name;
select *
from goods
where price > ( select avg ( price) from goods)
order by price desc ;
select cate_name, max ( price)
from goods
group by cate_name;
select *
from goods
inner join ( select cate_name, max ( price) as maxfrom goodsgroup by cate_name) maxprice
on maxprice. max = goods. priceand maxprice. cate_name = goods. cate_name;
表的优化:
删除异常:删除的时候,连带一起删除了,准确说就是表的内容太多了,需要分表
如何优化?
第一步:常见新表
第二步:同步数据到新表
第三步:更新旧表数据
第四步:更新表结构以goods为例:
create table if not exists good_cates( id int unsigned primary key auto_increment , name varchar ( 40 ) not null
) ;
insert into good_cates( name) ( select cate_name from goods group by cate_name) ;
select *
from goods
inner join good_cates gc
on goods. cate_name = gc. name;
update ( goodsinner join good_cates gcon goods. cate_name = gc. name)
set goods. cate_name= gc. id;
desc goods;
alter table goods change cate_name cate_id int unsigned not null ;