原来:
drop table if exists account;
create table account
(
name varchar(20),
money int
);
insert into account values('scy',1500),('sxr',1500);
show variables like 'autocommit';
set autocommit=0;
start transaction;
update account set money=0 where name ='scy';
update account set money=0 where name ='sxr';
rollback;
结果:

正确方法:
drop table if exists account;
create table account
(
name varchar(20),
money int
)engine=innodb;
insert into account values('scy',1500),('sxr',1500);
show variables like 'autocommit';
set autocommit=0;
start transaction;
update account set money=0 where name ='scy';
update account set money=0 where name ='sxr';
rollback;
结果:

本文探讨了在数据库操作中,使用InnoDB引擎创建和更新账户表时,正确设置autocommit和事务处理的重要性。通过实例展示了如何避免数据丢失,并强调了表结构设置engine=innodb的影响。
142

被折叠的 条评论
为什么被折叠?



