英文:
MySQL source command and transactions
问题
I'm using MySQL client:
mysql --version
mysql Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)
Basically I don't understand the difference between running a script SQL in these ways:
Work properly with transactions, I mean rollback everything if there is an error:
scenario 1:
mysql --show-warnings --verbose -u root -p < script.sql
Execute everything even if we have an error in the middle:
scenario 2:
mysql> source script.sql
My script is basically like this:
Start transaction
update 1 (ok)
update 2 (error)
commit
I'm expecting to get a rollback if I have an error in the middle of the transaction (update 2) but that doesn't happen if I use source script.sql
script.sql
START TRANSACTION;
UPDATE onboarding.components_options
SET value = 'Business Details'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'title';
UPDATE onboarding.components_options
SET value2 = 'Is your business incorporated?'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'subtitle';
COMMIT;
scenario 1 (work fine)
# mysql --show-warnings --verbose -u root -p < script.sql
Enter password:
--------------
START TRANSACTION
--------------
--------------
UPDATE onboarding.components_options
SET value = 'Business Details'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'title'
--------------
--------------
UPDATE onboarding.components_options
SET value2 = 'Is your business incorporated?'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'subtitle'
--------------
ERROR 1054 (42S22) at line 8: Unknown column 'value2' in 'field list'
bash-4.4# mysql -u root -p
scenario 2 (unexpected result, at least for me)
Pay attention, the commit sentence is executed -> 4
mysql> source script.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
ERROR 1054 (42S22): Unknown column 'value2' in 'field list'
Query OK, 0 rows affected (0.02 sec)
英文:
I'm using MySQL client:
# mysql --version
mysql Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)
Basically I don't understand the difference between running a script SQL in these ways:
Work properly with transactions, I mean rollback everything if there is an error:
scenario 1:
# mysql --show-warnings --verbose -u root -p < script.sql
Execute everything even if we have an error in the middle:
scenario 2:
mysql> source script.sql
My script is basically like this:
Start transaction
update 1 (ok)
update 2 (error)
commit
I'm expecting to get a rollback if I have an error in the middle of the transaction (update 2) but that doesn't happen if I use source script.sql
script.sql
START TRANSACTION;
UPDATE onboarding.components_options
SET value = 'Business Details'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'title';
UPDATE onboarding.components_options
SET value2 = 'Is your business incorporated?'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'subtitle';
COMMIT;
scenario 1 (work fine)
# mysql --show-warnings --verbose -u root -p < script.sql
Enter password:
--------------
START TRANSACTION
--------------
--------------
UPDATE onboarding.components_options
SET value = 'Business Details'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'title'
--------------
--------------
UPDATE onboarding.components_options
SET value2 = 'Is your business incorporated?'
WHERE component_id = (select id from onboarding.components where reference = 'BUSINESS_ENTITY')
and type = 'subtitle'
--------------
ERROR 1054 (42S22) at line 8: Unknown column 'value2' in 'field list'
bash-4.4# mysql -u root -p
scenario 2 (unexpected result, at least for me)
Pay attention, the commit sentence is executed -> 4
mysql> source script.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
ERROR 1054 (42S22): Unknown column 'value2' in 'field list'
Query OK, 0 rows affected (0.02 sec)
答案1
得分: 1
在运行mysql
时,使用批处理模式(mysql --show-warnings --verbose -u root -p < script.sql
),默认情况下,当出现错误时,脚本将停止处理。因此,当执行第三条语句并产生错误时,脚本(和会话)将结束,并且事务会隐式回滚。您可以通过在命令行中向mysql客户端传递--force选项来更改此行为。
当您使用source
执行语句时,即使发生错误,脚本也会继续运行,从而执行您的COMMIT
语句,使更改持久化。
这种功能在过去的功能请求中曾受到质疑。
英文:
When running mysql
in batch mode (mysql --show-warnings --verbose -u root -p < script.sql
), by default the script will stop processing when an error occurs. So, when your third statement is executed and produces an error, the script (and session) will end and the transaction is implicitly rolled back. You can change this behaviour by passing the --force option to mysql client on the command line.
When you execute your statements using source
the script continues after an error occurs, thus running your COMMIT
statement and the changes persist.
This functionality has been questioned in feature requests in the past.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论