MySQL源命令和事务

huangapple go评论62阅读模式
英文:

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 &lt; script.sql

Execute everything even if we have an error in the middle:

scenario 2:

mysql&gt; 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 = &#39;Business Details&#39;
        WHERE component_id = (select id from onboarding.components where reference = &#39;BUSINESS_ENTITY&#39;)
            and type = &#39;title&#39;;
           
    UPDATE onboarding.components_options
        SET value2 = &#39;Is your business incorporated?&#39;
        WHERE component_id = (select id from onboarding.components where reference = &#39;BUSINESS_ENTITY&#39;)
            and type = &#39;subtitle&#39;;
           
COMMIT;

scenario 1 (work fine)

# mysql --show-warnings --verbose -u root -p &lt; script.sql
Enter password: 
--------------
START TRANSACTION
--------------
--------------
UPDATE onboarding.components_options
        SET value = &#39;Business Details​&#39;
        WHERE component_id = (select id from onboarding.components where reference = &#39;BUSINESS_ENTITY&#39;)
            and type = &#39;title&#39;
--------------
--------------
UPDATE onboarding.components_options
        SET value2 = &#39;Is your business incorporated?​&#39;
        WHERE component_id = (select id from onboarding.components where reference = &#39;BUSINESS_ENTITY&#39;)
            and type = &#39;subtitle&#39;
--------------
ERROR 1054 (42S22) at line 8: Unknown column &#39;value2&#39; in &#39;field list&#39;
bash-4.4# mysql -u root -p

scenario 2 (unexpected result, at least for me)

Pay attention, the commit sentence is executed -> 4

mysql&gt; 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 &#39;value2&#39; in &#39;field list&#39;
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 &lt; 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.

huangapple
  • 本文由 发表于 2023年5月11日 04:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定