commit的用法和短語,Mysql中autocommit的用法

 2023-11-19 阅读 27 评论 0

摘要:定義 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.該變量為全局與會話變量

定義

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控制當前會話是否自動提交事務。
519126-20170905161512476-1148685193.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';
519126-20170905160849351-32575096.png

會話內修改方法

SET autocommit = {0 | 1}

commit的用法和短語,關閉自動提交: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文件如下:
519126-20170905162046804-1617440141.png

用法展示

1.修改前狀態
519126-20170905162437147-186358840.png

519126-20170905162350335-1903680651.png

2.開啟會話1修改
START TRANSACTION;
UPDATEemployees.salariesSET salary = 200053 WHERE emp_no = 10001;
SELECT * FROMemployees.salariesWHERE emp_no = 10001;
519126-20170905162703163-230452994.png

autocommit、3.開啟會話2來查詢
519126-20170905162802304-1952440174.png

519126-20170905162812741-27202390.png

4.會話1中提交事務
COMMIT;
519126-20170905162903382-1620146402.png

5.會話2中查詢
519126-20170905162927913-390119902.png

復雜用法鏈接

(https://dev.mysql.com/doc/refman/5.7/en/commit.html)

轉載于:https://www.cnblogs.com/grey-wolf/p/7479348.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/179865.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息