Mysql事务处理_MySQL
事务处理在各种管理系统中都有着广泛的应用,比如人员管理系统,很多同步数据库操作大都需要用到事务处理。比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事务! 删除的SQL语句 deletefrom userinfo where ~~~delete from mail where ~~delete fromarticle where~~~~
+------------------+-------+|Variable_name | Value |+------------------+-------+|have_bdb | YES || have_crypt | YES ||have_innodb | YES || have_isam | YES ||have_raid | YES || have_symlink | YES ||have_openssl | NO || have_query_cache | YES |+------------------+-------+8 rows in set (0.05sec) 如果是这样的,那么我们就可以创建一张支持事务处理的表来试试了。 mysql>use test;Database changedmysql> CREATE TABLE `dbtest`(-> id int(4)-> ) TYPE=INNODB;Query OK, 0 rowsaffected, 1 warning (0.05 sec)mysql> select * from dbtest-> ;Empty set (0.01 sec)mysql> begin;QueryOK, 0 rows affected (0.00 sec)mysql> insert into dbtestvalue(5);Query OK, 1 row affected (0.00 sec)mysql>insert into dbtest value(6);Query OK, 1 row affected (0.00sec)mysql> commit;Query OK, 0 rows affected (0.00sec)mysql> select * from dbtest;+------+| id |+------+| 5 || 6 |+------+2 rows in set(0.00 sec)mysql> begin;Query OK, 0 rows affected (0.00sec)mysql> insert into dbtest values(7);Query OK, 1row affected (0.00 sec)mysql> rollback;Query OK, 0rows affected (0.00 sec)mysql> select * fromdbtest;+------+| id |+------+| 5 || 6|+------+2 rows in set (0.00sec)mysql>*******************************************************************************************************************[PHP]functionTran( $sql ) {$judge = 1;mysql_query('begin');foreach ($sql as $v) {if ( !mysql_query($v) ) {$judge =0;}}if ($judge == 0){mysql_query('rollback');return false;}elseif ($judge == 1) {mysql_query('commit');return true;}}[/PHP]************************************************ |