众所周知,START 事务将启动事务,而 COMMIT 用于在启动事务后进行任何更改。在下面的示例中,我们使用 COMMIT 和 START 事务创建了一个存储过程,它将插入一条新记录并在具有以下数据的表“employee.tbl”中提交更改 -
mysql> Select * from employee.tbl;
+----+---------+
| Id | Name |
+----+---------+
| 1 | Mohan |
| 2 | Gaurav |
| 3 | Rahul |
| 4 | Saurabh |
+----+---------+
4 rows in set (0.00 sec)
示例
mysql> Delimiter //
mysql> Create Procedure st_transaction_commit()
-> BEGIN
-> START TRANSACTION;
-> INSERT INTO employee.tbl(name) values ('Yash');
-> UPDATE employee.tbl set name = 'Sohan' WHERE id = 3;
-> COMMIT;
-> END //
Query OK, 0 rows affected (0.03 sec)
现在,当我们调用此过程时,它将插入并更新表employee.tbl 中的值。它还将提交更改。
mysql> Delimiter ;
mysql> Call st_transaction_commit();
Query OK, 0 rows affected (0.17 sec)
mysql> Select * from employee.tbl;
+----+---------+
| Id | Name |
+----+---------+
| 1 | Mohan |
| 2 | Gaurav |
| 3 | Sohan |
| 4 | Saurabh |
| 5 | Yash |
+----+---------+
5 rows in set (0.00 sec)