MySQL·MySQL from first query to production

Transactions

Group writes so they succeed or fail together, and choose the right isolation.

Beginner 14 min read Lesson 6 of 300 Challenge included
What you will be able to do
  • Use START TRANSACTION and COMMIT.
  • ROLLBACK on error.
  • Keep transactions short to reduce lock contention.
  • Retry deadlocks with backoff.

In plain English

A transaction is an atomic unit of work. InnoDB gives you ACID guarantees. Use transactions for multi-step writes, keep them short, and handle deadlocks by retrying the whole transaction.

transaction.sqlSQL
START TRANSACTION;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;
Your turn

Wrap two updates

Easy

Write a transaction that updates two rows and commits.

SQL · starter
START TRANSACTION;
Check your understanding before moving on.
Knowledge checkQuestion 1

Which statement makes a transaction’s changes permanent?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Users and permissions
Lesson 6 of 300 · 0% of course