此为原创:转载请注明出处;
作者:小宝(ilsanbao)
博客:http://www.daxi8.cn/(技术起点)
autocommit 在mysql5.1 版本以后可以通过mysql命令客户端修改;
查看默认设置:
mysql>show variables like 'autoc%' #默认为 ON
默认为ON,任何客户端发送的SQL指令都将自动提交给mysql server进行处理。
大家一般在使用中多为on, 这里就不多讲解了。
修改autocommit:
mysql> set autocommit='Off'; Query OK, 0 rows affected (0.00 sec)
创建一个测试表:
mysql> create table t_autocommit( -> id int not null auto_increment, -> amount int not null default '0', -> primary key(id) -> )engine=innodb; Query OK, 0 rows affected (0.03 sec) mysql>insert into t_autocommit set amount=1;
这是我们修改amount字段
mysql>update t_autocommit set amount=amount+10; 然后查看数据 mysql> select * from t_autocommit; +----+--------+ | id | amount | +----+--------+ | 1 | 11 | +----+--------+
可以看到已经修改成功,其实不然,我们断开mysql,再重新连接
mysql> select * from t_autocommit; +----+--------+ | id | amount | +----+--------+ | 1 | 1 | +----+--------+
看到了吧, amount值没有被修改,为什么呢?很简单因为SQL语句没有被自己提交给server端去处理,只是在当前连接中做了相应处理。
注:autocommit变量是session类型的,所做的修改只能在当前连接中有效!