事务的创建:
1、关闭事务的自动提交:
命令:set autocommit=0;
1 mysql>
2 mysql> set autocommit=0;
3 Query OK, 0 rows affected (0.00 sec)
4
5 mysql>
2、修改默认SQL命令结束符
命令:delimiter // 或者 delimiter % # 默认的结束符:“;”
1 mysql>
2 mysql>
3 mysql> delimiter //
4 mysql>
5 mysql>
3、事务的创建
命令:
1 mysql> start transaction;
2 -> update yck set teacher="Rshine" where name="lucy";
3 -> update yck set teacher="Rony" where name="natasha";
4 -> update yck set teacher="Robote" where name="jery";
5 -> // #直接结束事务并没有提交,可以回滚。命令:commit 提交事务,一旦提交数据将永久性的修改,无法回滚。
6 Query OK, 0 rows affected (0.00 sec)
7
8 Query OK, 1 row affected (0.00 sec)
9 Rows matched: 1 Changed: 1 Warnings: 0
10
11 Query OK, 1 row affected (0.00 sec)
12 Rows matched: 1 Changed: 1 Warnings: 0
13
14 Query OK, 1 row affected (0.00 sec)
15 Rows matched: 1 Changed: 1 Warnings: 0
16
17 mysql>
yck表原内容
yck事务执行后的内容
4、回滚
命令:
1 mysql>
2 mysql> rollback//
3 Query OK, 0 rows affected (0.00 sec)
回滚后yck表的内容
回滚之后的数据和之前的数据一样。