事务

CREATE TABLE ck(
id int PRIMARY key auto_increment,
ename VARCHAR(5),
ck DOUBLE(10,2)
);

INSERT INTO ck VALUES 
(null,'张三',1000),
(null,'李四',1000);

SELECT * from ck;

-- 开启事务
BEGIN;

UPDATE ck set ck = ck - 500 WHERE ename = '张三';
UPDATE ck set ck = ck + 500 WHERE ename = '李四';

-- 提交事务
COMMIT;

-- 回滚  
ROLLBACK;

UPDATE ck set ck = 10000;

SELECT * from ck;

Mysql事务是默认开启的

查看事务的提交方式

SELECT @@autocommit;

修改成手动提交

set @@autocommit = 0;

您可能还喜欢...