创建一个账户,

-- 创建一个账户

create table my_account(

idint primary key auto_increment,

number char (16)not null unique

comment'账户',

name varchar (20)not null ,

moneydecimal (10,2)default 0.0

comment'账户余额'

)charset utf8;

-- 插入数据

insert into my_accountvalues

(null,'1234567890000000','张三','1000'),

(null,'1234567890000001','李四','2000');

-- 张三转账1000元给李四

update my_accountset money=money-1000 where id=1;

-- 事物安全

-- 开启事物

starttransaction;

-- 事物的操作;1,李四账户减少钱

update my_accountset money=money-1000

where id=2;

-- 事物操作;2,张三账户增加

update my_accountset money=money+1000

where id=1;

-- 提交事物

commit;

-- 回滚点操作

-- 开启事物加钱

starttransaction ;

-- 事物处理1;张三发工资了,

update my_accountset money=money+10000

where id=1;

-- 设置回滚点

savepoint sp1;

-- 银行扣税

update my_accountset money=money-10000*0.05 where id=2;-- 错误

-- 回滚到回滚点

rollback to sp1;

-- 继续操作  银行扣税

update my_accountset money=money-10000*0.05 where id=1;

-- 查看结果

select * from my_account;

-- 提交结果

commit;

-- 显示系统变量autocommit(模糊查询)

show variableslike 'autocommit';

-- 关闭事物自动提交

set autocommit=0;--off/0

-- 给李四发工资

update my_accountset money=money+10000 where id=2;

commit;

-- 银行扣税

update my_accountset money=money-10000*0.05 where id=2;

-- 事物的隔离性

starttransaction;

-- 给张三返税,返500块钱

update my_accountset money=money+500

where id=1;

-- 另外窗口开启事物

        starttransaction;

-- 李四淘宝花了500

        update my_accountset money=money-500 where id=2;

select * from my_account;

commit;

select * from my_account;

-- 回到张三窗口;事物回滚

rollback;

select * from my_account;-- 两边一致

-- 锁机制

starttransaction;

-- 使用非索引字段(name),行锁自动上升为表锁

update my_accountset money=money+500

where name='张三';

update my_accountset money=money+1000 where id=2;

作者:BJ000

链接:https://www.jianshu.com/p/3d69e419656f

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容