May 15, 2021 MySQL
MySQL transactions are primarily used to process data with high operational volumes and complexity. For example, in a personnel management system, you delete a person, you need to delete the basic information of the person, but also to delete information related to that person, such as mailboxes, articles, etc., so that these database operating statements constitute a transaction!
In general, transactions must meet four conditions (ACID): Atomicity (atomicity or indivisible), Consistency (consistency), Isolation (isolation or independence), Durability (persistence)
1, explicitly starting a transaction:
start transaction
Or
begin
2, do save points, a transaction can have more than one save point:
savepoint 保存点名称
3, commit the transaction and make all changes made in the database permanent:
commit
Or
commit work
4, roll back the end of the user's transaction and undo all pending uncommitted modifications:
rollback
Or
rollback work
5. Delete the save point of a transaction, if no save point is specified, the statement operation will be thrown wrong.
release savepoint 保存点名称
6. Roll the transaction back to the marker point:
rollback to 标记点
7. Set the isolation level of the transaction. The InnoDB storage engine provides isolation levels for transactions such as READ UNCOMMITTED, READCOMMITTED, REPEATABLE READ, and SERIALIZABLE.
set transaction
1. Use begin, rollback, commit to implement transaction processing.
2. Use set to change MySQL's auto-submission mode.
<?php $handler=mysql_connect("localhost","root","password"); mysql_select_db("task"); mysql_query("SET AUTOCOMMIT=0");//设置为不自动提交,因为MYSQL默认立即执行 mysql_query("BEGIN");//开始事务定义 if(!mysql_query("insert into trans (id) values('2')")) { mysql_query("ROOLBACK");//判断当执行失败时回滚 } if(!mysql_query("insert into trans (id) values('4')")) { mysql_query("ROOLBACK");//判断执行失败回滚 } mysql_query("COMMIT");//执行事务 mysql_close($handler); ?>