MySQL命令行和事务 [英] MySQL command line and transactions
问题描述
我有一个关于MySQL的问题,并且无法找到答案。我知道自动提交在MySQL中默认开启。我需要在一个事务中从命令行运行几个更新查询,但我不知道MySQL将如何处理它们。如果我有这样的:
I have a question about MySQL and have been unable to find an answer. I know auto-commit is turned on by default in MySQL. I need to run a few update queries from the command line in one transaction but I don't know how MySQL will handle them. If I have something like this:
mysql -uroot -proot -e 'QUERY 1; QUERY 2; QUERY3'
它将作为一个事务执行还是MySQL自动提交每个语句?
will it execute as one transaction or will MySQL auto-commit each statement individually? I need to ensure atomicity.
推荐答案
您可以使用MySQL的 START TRANSACTION
语法创建事务提交:
You can use MySQL's START TRANSACTION
syntax to create a transactional commit:
源: http://dev.mysql.com/doc/refman/5.0/en/commit.html
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;
您还可以在.sql文件中写入查询,并将其写入mysql:
You could also write your query in a .sql file, and pipe it into mysql:
$ cat query.sql | mysql -uroot -proot
这篇关于MySQL命令行和事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!