mysql

-- 创建一个账户表
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'账户余额'
);


-- 插入数据
insert into my_account values(null,'000000000001','张三','1000'),
(null,'0000000000002','李四',2000);

-- 张三转账1000元给李四
update my_account set money=money-1000 where id=1;


-- 事务安全
-- 开启事务
start transaction;
-- 事务操作:1,李四账户减少
update my_account set money=money-1000 where id=2;
-- 事务操作:2, 张三账户增加
update my_account set money=money+1000 where id=1;
-- 提交事务
commit;


-- 回滚点操作
-- 开启事务
start transaction;

-- 事务处理:1,张三发工资了,加钱
update my_account set money=money+1000 where id=1;
-- 设置回滚点
savepoint sp1;
-- 银行扣税
update my_account set money=money-10000*0.05 where id=2;-- 错误

-- 回滚到回滚点
rollback to sp1;
-- 继续操作
update my_account set money=money+1000 where id=1;

-- 查看结果
select * from my_account;
-- 提交结果
commit;


-- 显示系统变量'autocommit'(模糊查询)
show variables like 'autocommit';

-- 关闭事务自动提交
set autocommit=0;


-- 锁机制
start transaction;
update my_account set money=money+500 where name='张三';

update my_account set money=money+1000 where id=2;
-- 提交或回滚都能解锁


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;

insert into goods values(0,'r510vc 15.6英寸笔记本','笔记本','华硕',
'3399',default,default);

你可能感兴趣的:(mysql)