mysql创建表用于银行储蓄系统

-- 创建表用于保存账户

create table my_account(
id int unsigned not null primary key auto_increment,
account varchar(16) not null unique,
name varchar(10) not null)charset utf8;

--插入数据

insert into my_account values
(1,"1111111111111111","张三"),
(2,"2222222222222222","李四"),
(3,"3333333333333333","王军");

--修改表

alter table my_account add money decimal(20,2) after name; -- 在姓名后增加存储金额表格

--更新数据

update my_account set money=1000 where id=1;
update my_account set money=1050 where id=2;
update my_account set money=3000 where id=3;    --根据条件修改表中数据

--转账功能

update my_account set money=money-500 where id=2; 

--事务操作(1、手动提交,2、自动提交)

--开启事务

start transaction;

--查询功能

select * from my_account; -- 结果是减掉五百的数据(结果是经事务日志处理的)

-- 转账功能

update my_account set money=money+500 where id=1;

--手动转账

commit;

--回滚点
--开启事务
start transaction;
--转账功能
update my_account set money=money-500 where id=2;
update my_account set money=money-500 where id=3;
--设置回滚点
savepoint sp1;
--转账出错
update my_account set money=money-500 where id=3;
--回滚  //返回上一步操作
rollback to sp1;
--转账正确
update my_account set money=money-500 where id=2;
update my_account set money=money+1000 where id=1;
--提交
commit;

猜你喜欢

转载自blog.csdn.net/tmh_15195862719/article/details/81706825