mysql autocommit 脚本_Mysql中autocommit的用法

定义

Mysql文档原文:SET autocommit disables or enables the default autocommit mode for the current session. Autocommit is a session variable and must be set for each session.

By default, MySQL runs with autocommit mode enabled.

该变量为全局与会话变量,默认值为1,表示自动提交事务。autocommit控制当前会话是否自动提交事务。

ac6748e1885d32d5ce38030e89e71f97.png

If set to 1, all changes to a table take effect immediately. If set to 0, you must use COMMIT to accept a transaction or ROLLBACK to cancel it. If autocommit is 0 and you change it to 1, MySQL performs an automatic COMMIT of any open transaction. Another way to begin a transaction is to use a START TRANSACTION or BEGIN statement.

如果设置为1,对一个表的所有改变立即生效。

如果设置为0,你必须使用COMMIT去提交事务或者用ROLLBACK来回滚事务。

如果autocommit为0,你修改其为1,Mysql对任何开放的事务进行自动提交。

另一个开启一个事务的方式是:使用START TRANSACTION或者BEGIN。

查看方法

SHOW VARIABLES LIKE 'autocommit';

42a11490f4e29291e2e8b14178550c90.png

会话内修改方法

SET autocommit = {0 | 1}

关闭自动提交:SET autocommit=0;

打开自动提交:SET autocommit=1;

全局修改方法

By default, client connections begin with autocommit set to 1. To cause clients to begin with a default of 0, set the global autocommit value by starting the server with the --autocommit=0 option. To set the variable using an option file, include these lines:

默认情况下下,客户端连接默认autocommit为1. 如果希望客户端一连接即默认autocommit为0,设置全局的autocommit通过以下方式:

*启动服务器时携带--autocommit=0选项

*修改option文件如下:

e655fa27f5fc59939efed7ddd1dd41a8.png

用法展示

1.修改前状态

4fb85d34f62045ed5ca9bfd5d50b69a0.png

3222517b5b266f21477109860486928c.png

2.开启会话1修改

START TRANSACTION;

UPDATEemployees.salariesSET salary = 200053 WHERE emp_no = 10001;

SELECT * FROMemployees.salariesWHERE emp_no = 10001;

a46ac6e10afa40f7b6381e1dd9addd96.png

3.开启会话2来查询

18e20ab4b1a241bbe2f55ac57579d356.png

bc931a469a628ac26f947e0f2f736a5e.png

4.会话1中提交事务

COMMIT;

c5f0d5d8ae3af42f5f7a947a7cb44c7a.png

5.会话2中查询

45cc203244b064fdf4ec85ab2a494e65.png

复杂用法链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值