MySQL:事务处理
事务处理用来维护数据库的完整性,用来管理成批执行的MySQL操作。下面分别举例说明怎么开启事务,进行回滚,提交和使用保留点。
1、开启事务
START TRANSACTION
2、使用回滚Rollback
例1:删除表的所有行再回滚
SELECT * FROM ordersnew;
START TRANSACTION;
DELETE FROM ordersnew;
SELECT * FROM ordersnew;
ROLLBACK
SELECT * FROM ordersnew;
3、提交事务commit
START TRANSACTION
DELETE FROM orderitems WHERE order_num=20010;
DELETE FROM orders WHERE order_num=20010;
COMMIT;
4、使用保留点
起一个默认的名字 如del1,可以回滚到创建的保留点,提交时保留点自动释放,也可以release savepoint释放掉
SELECT * FROM ordersnew;
START TRANSACTION;
INSERT INTO ordersnew (odate,cui)VALUES ('2019-01-03 20:00:00',10001)
SAVEPOINT del1;
DELETE FROM ordersnew;
SELECT * FROM ordersnew;
ROLLBACK TO del1;
SELECT * FROM ordersnew;