愚公移山日记·32
今天在学的是sql的用法,相比于前天的学习,今天的内容更复杂一些。
复习
查询类型 cata_name 为’超极本’的商品名称 name 价格 price
select name,price from goods where acta_name = ‘超极本’;
显示商品的种类
分组的方式:
select cata_name from goods group by cata_name;
去重的方法
selecr distinct cata_name from goods;
求所有的电脑产品的平均价格,并且保留两位小数
select round(avg(price),2) from goods;
显示每种类型的平均价格
select cata_name,avg(pricr) from goods group by cata_name;
查询每种类型的商品中最贵,最便宜,平均价格,数量
selecr cata_naem,max(price),min(price),avg(price),count(*) from goods group by cata_name;
查询所有价格大于平均价格的产品并且按价格降序排序
查询平均价格
select avg(price) from goods;
使用子查询
select * from goods where price >(select avg(price) from goods);
查询每种类型中最贵的电脑所有信息
selecr cata_name,max(price) from goods group by cata_name;
selecr * from goods
inner join
(selecr cata_name,max(price) as max_price from goods group by cata_name) as max_price_goods
on goods.cata_name = max_price_goods.cata_name and goods.price = max_price_goods.max_price;
SQL操作实战
第一步:创建表(商品种类表 goods_catas)
careat table if not exicts goods_catas(
id int unsigned primary key auto_increment,
name varchar(40) nut null);
第二步: 同步商品分类表 数据 将商品的所有信息吸入到商品种类表中
按照分组的方式查询goods 表中的所有 种类(cata_name)
selsct cata_name from goods group by cata_name;
子查询 拆入数据到新的分类列表中
insert into goods_cata(name)(selsct cata_name from goods group by cata_name)
第三步 同步商品表 数据通过 goods_cata 数据表来更新 goods 表
因为要通过goods_cata表更新goods 表所以要把两个连接起来(goods.cata_name = goods_cata.name)
select *from goods inner join goods_cata on goods.cata_name = goods_cata.name;
把商品表goods中的cata_name 全部替换成列表中的商品 id(update(…)…set)
update goods inner join goods_cata on goods.cata_name = goods_cata.name set goods.cata_name = gooda_cata.name
第四步 修改表结构
查看表结构注意两个表达 外键类型需要一致
alter table goods change cata_name cate_id int nusigned not null;
另外的联系
careat table if not exicts brand_name(
id int unsigned primary key auto_increment,
name varchar(40) nut null);
select brand_name from goods group by bramd_name;
insert into goods_brand(name)(select brand_name from goods group by bramd_name);
updata goods inner join goods_brands on goods.brand_name = goods_brand.name set goods.brand_name = goods_brand.id;
alter table goods change brand_name brand_id int nusigned not null;
外键使用
做表和表之间的约束:一个表的主键A在另外的一个表B中出现 我们说A是B的一个人外键
约束 数据的插入 使用外键 forign key
添加外键 later table 表名 add foreign key (外键字段) reference 表名
alter table goods add foreign key (cata_id) references goods_cata(id);
数据库存在一条 不符合规则的数据 需要先删除再从新添加外键
delete form goods where cata_id = 12;
给goods 表的brand_id 添加goods_brands 的id做外键连接
later table goods add foreign key(brand_id) rreferences goods_brands(id);
创建表的同时设置外键(注意goods_catas 和goods_brands 两个表必须事先存在)
创建gooods_test 表字段为(id,name,cata_id,brand_id)
其中cata_id 外键关联 goods_cata 的id 字段
其中brand_id 外键关联 goods_brand的id字段
creat table goods_test (id int primary key auto_inctement,name vachar(150) not null
cata_id int unsigned not null,
brand_id int unsigned not null,
foreign key (cata_id) references goods_catas(id),
foreign key (brand_id) references goods_brand(id));
取消外键的束缚
查看外键名
show create table goods; 查看建表语句
获取名称之后可以根据名称来删除外键的约束
alter tabble goods add foreign key (brand_id) references goods_cata(id);
视图
视图的作用
视图:虚拟的表仅仅支持查询
视图的使用
创建,使用,查看,删除—关联特别多的表格
视图是以v开头
creat view v_goods_info as selecr goods.name ganme,goods_cata.name gcname,goods_brands.name gbname from goods inner join goods_cata on goods gbname from goods inner join goods_cata on goods.cata_id inner join goods_brands on goods.brand_id = goods_brands.id;
查看表
show table
改变元数据
uodate goods set name = ‘xxx’ where id = 24
删除视图
drop view v_goods__info;
show table;
表引擎
show engines
开启事务
begin
操作 insert updata delet
提交事务
commit
回滚事务
rollback