BEGIN...END vs START TRANSACTION...COMMIT [英] BEGIN...END vs START TRANSACTION...COMMIT

查看:118
本文介绍了BEGIN...END vs START TRANSACTION...COMMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做和做有什么区别:

START TRANSACTION
...
COMMIT

或者做:

BEGIN
...
END

后者是否自动提交,或者使用其中之一的实际示例是什么?

Does the later autocommit, or what might be a practical example of using one of the other?

推荐答案

在 MySQL 5.7 和 MySQL 8 中,BEGINEND 与 T-SQL 相同并且表示复合语句"也称为代码块",就像 C、Java、C# 等中的花括号一样.

In both MySQL 5.7 and MySQL 8, BEGIN and END is the same as in T-SQL and represents a "compound statement" also known as "a block of code", just like curly-braces in C, Java, C#, etc.

但是,BEGIN 关键字 (令人困惑地)重载为 BEGIN WORKSTART TRANSACTION 的别名,它们的语义取决于它们是否被是否在存储程序中使用:

However, the BEGIN keyword is also (confusingly) overloaded as an alias for BEGIN WORK and START TRANSACTION, and their semantics depend on if they're being used inside a stored program or not:

在所有存储程序(存储过程和函数、触发器和事件)中,解析器将 BEGIN [WORK] 视为 BEGIN ... END 的开始堵塞.在此上下文中使用 START TRANSACTION 开始事务.

Within all stored programs (stored procedures and functions, triggers, and events), the parser treats BEGIN [WORK] as the beginning of a BEGIN ... END block. Begin a transaction in this context with START TRANSACTION instead.

所以:

  • 开始交易
    • 始终启动事务.您应该更喜欢这种语法.
    • 如果您在存储过程、函数、触发器或事件中,则 BEGIN 本身标志着复合语句的开始.您只能使用 START TRANSACTION 开始交易.
    • 如果您直接针对 MySQL 执行 SQL,那么这也会启动一个事务(因为它被解释为 BEGIN WORK).但是这样使用它是愚蠢和混乱的,所以要避免它.
    • If you're in a Stored Procedure, Function, Trigger or Event, then BEGIN by itself marks the start of a compound statement. You can only use START TRANSACTION to start a transaction.
    • If you're directly executing SQL against MySQL, then this also starts a transaction (as it's interpreted as BEGIN WORK). But it's silly and confusing to use it this way, so avoid it.
    • 这是START TRANSACTION 的别名.我会避免完全使用它以防止混淆.
    • This is an alias for START TRANSACTION. I'd avoid using this completely to prevent confusion.

    这篇关于BEGIN...END vs START TRANSACTION...COMMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆