事物分为 自动(默认的)和手动(需开启事务)两种
-- 事务 start transaction; -- commite; 提交事务 -- rollback; 回滚事务
create table if not exists account( id int primary key auto_increment, name varchar(14) not null, RMB int ); insert into account(name,RMB) values('Which',1000),('Tj',998);
rollback; 进行一步操作后,可以撤回这个操作
-- 开始事务 手动事物
start transaction;
-- 事物操作 update account set RMB =RMB-992 where name = 'Which'; -- \q 退出mysql(中断事务) -- mysql -u name -p; 回到mysql
-- 继续事务操作 -- 回滚事务 rollback;
+----+-------+------+ -- insert | id | name | RMB | +----+-------+------+ | 1 | Which | 8 | | 2 | Tj | 998 | +----+-------+------+ -- update | --+-------+------+ | id | name | RMB | +----+-------+------+ | 1 | Which | 8 | | 2 | Tj | 998 | +----+-------+------+
-- rollback | +----+-------+------+ | id | name | RMB | +----+-------+------+ | 1 | Which | 8 | | 2 | Tj | 998 | +----+-------+------+
commite: 自动提交
-- 事物操作 update account set RMB =RMB-992 where name = 'Which'; -- \q 退出mysql(事务) -- mysql -u name -p; 回到mysql
-- 实际上在这儿退出后,再回到account表,值并没有-992,但是再进行下一步操作后,992就转给Tj了。
-- 继续事务操作 update account set RMB =RMB+992 where name = 'Tj';
-- insert Which 1000,Tj 998 +----+-------+------+ | id | name | RMB | +----+-------+------+ | 1 | Which | 1000 | | 2 | Tj | 998 | +----+-------+------+ -- update Which-992 +----+-------+------+ | id | name | RMB | +----+-------+------+ | 1 | Which | 8 | | 2 | Tj | 998 | +----+-------+------+ -- update Tj+992 +----+-------+------+ | id | name | RMB | +----+-------+------+ | 1 | Which | 8 | | 2 | Tj | 1990 | +----+-------+------+
原子性操作:Innodb引擎支持事务
要么全部成功,要么全部失败