Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
BEGIN...END vs START TRANSACTION...COMMIT

What is the difference between doing:

START TRANSACTION
...
COMMIT

Or doing:

BEGIN
...
END

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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

  • MySQL 5.7: https://dev.mysql.com/doc/refman/5.7/en/begin-end.html
  • MySQL 8.0: https://dev.mysql.com/doc/refman/8.0/en/begin-end.html

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:

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.

So:

  • START TRANSACTION
    • Always starts a transaction. You should prefer this syntax.
  • BEGIN:
    • 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.
  • BEGIN WORK:
    • This is an alias for START TRANSACTION. I'd avoid using this completely to prevent confusion.
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda