Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!