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?
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.
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 aBEGIN ... ENDblock. Begin a transaction in this context withSTART TRANSACTIONinstead.
So:
START TRANSACTION
BEGIN:
BEGIN by itself marks the start of a compound statement. You can only use START TRANSACTION to start a transaction.BEGIN WORK). But it's silly and confusing to use it this way, so avoid it.BEGIN WORK:
START TRANSACTION. I'd avoid using this completely to prevent confusion.