# -创建一个账户表
# create table my_account(
# id int primary key auto_increment,
# number char(16) not null unique
# comment'账户',
# name varchar(20) not null,
# money decimal(10,2) default 0.0
# comment '账户余额'
# )charset utf-8;
# --插入数据
# insert into my_account values
# (null,'0000001','张三',1000),
# (null,'0000002','李四',2000);
# --张三转账1000给李四
# update my_account set money=money-1000 where id=1;
# --事务安全
# --开启事务
# start transacion;
# --事务操作:1,李四账户减少
# update my_account set money=money-1000 where id=2;
# --事务操作:1,张三账户增加
# update my_account set money=money+1000 where id=1;
# --提交事务
# commit;
# --回滚点操作
# --开启事务
# start transaction;
# --事务处理1:张三发工资了,加钱
# update my_account set money=money+10000 where id=1;
# --设置回滚点
# savepoint spl;
# --银行扣税
# update my_account set money=money-10000*0.05 where id=2;
# --错误
# --回滚到回滚点
# rollback to spl;
# --继续操作
# update my_account set money=money-10000*0.05 where id=1;
# --查看结果
# select * from my_account;
# --提交结果
# commit;
# --显示系统变量autocommit(模糊查询)
# show variables like 'autocommit';
# --关闭事务自动提交
# set autocommit=0;
# --给李四发工资
# update my_account set money=money+10000 where id=2;
# commit;
# update my_account set money=money-10000*0.05 where id=2;
# --事务的隔离性
# start transaction;
# --给张三返税,反500块钱
# update my_account set money=money+500 where id=1;
# select * from my_account;
# --另外窗口开启事务
# start transaction;
# --李四淘宝花了500
# update my_account set money=money-500 where id=2;
# select * from my_account;
# commit;
# select * from my_account;
# --回到张三窗口 事务回滚
# rollback;
# select * from my_account;
# --两边一致
# --锁机制
# start transaction;
# --使用非索引字段(name),行锁自动上升为表锁
# update my_account set money=money+500 where name='张三'
# update my_account set money=money+1000 where id=2;
# rollback;
# select * from my_account;
# --SQL演练
# --查询类型cate_name为‘超极本’的商品名称,价格
# select name as 商品名称,price as 商品价格 from goods where cate_name='超级本';
# --显示商品的种类
# select distinct cate_name from goods;
# select cate_name from goods
# group by cate_name;
# select cate_name,group_concat(name
# )from goods group by
# cate_name;
# --求所有电脑产品的平均价格,并且保留两位小数
# select round(avg(price),2)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 * 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;
# --查询每种类型中最贵的电脑信息
# 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;
# select * from (
# select cate_name,max(price) as max_price from goods group by cate_name
# )as g_new left join goods as g on g_new.cate_name=g.cate_name
# and g_new.max_price = g.price prder;
# insert into goods values(0,'东哥牌电脑','笔记本','老王','4999',default,default);
# --创建商品分类表
# create table if not exists
# goods_cates(
# id int unsigned primary key
# auto_increment,
# name varchar(40) not null
# );
# show tables;
# select cate_name fromgoods
# group by cate_name;
# --将分组结果写入到goods_cates数据表
# insert into goods_cates(name)
# select cate_name from goods
# group by cate_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;
# --插入类别
# insert into goods_cates(name)
# values('路由器'),('交换机'),('网卡');
# --插入商品
# insert into goods(name,cate_name,brand_name,price)
# values('LaserJet Pro P1606dn黑白激光打印机',12,4,''1849);
# --修改表结构
# alter table goods
# change cate_name cate_id int unsigned not null;
# delete from goods where id=23;
# --添加外键
# alter table goods add foreign
# key(cate_id) references
# gppds_cates(id);
# --插入商品
# insert into goods(name,cate_id,brand_name,price)
# values('LaserJet PRo P1606dn黑白激光打印机',12,4,'1849');
# --失败