MySQL command line and transactions

北慕城南 提交于 2019-12-05 07:28:25

You can use MySQL's START TRANSACTIONsyntax to create a transactional commit:

Source: 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;

You could also write your query in a .sql file, and pipe it into mysql:

$ cat query.sql | mysql -uroot -proot

Pipe is a great idea!

echo "START TRANSACTION;" > start.sql
echo "COMMIT;" > commit.sql

cat start.sql yourScript.sql commit.sql | mysql -uroot -proot

or

cat start.sql yourScript.sql - | mysql -uroot -proot

and so, you can commit o rollback by hand according the result of yourScript

Good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!