WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Intermediate · question 23 of 100

What is a transaction, and how do you implement transactions in MySQL using COMMIT and ROLLBACK?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

In database systems, a transaction is a sequence of one or more SQL operations that are treated as a single logical unit of work. These operations can include selecting, updating, deleting, or inserting data into a table. Transactions allow these operations to be treated as a single cohesive operation that must either be completed in its entirety or not at all.

In MySQL, transactions can be implemented using the ‘COMMIT‘ and ‘ROLLBACK‘ commands.

When a transaction is started in MySQL, all subsequent SQL statements are treated as part of that transaction until the transaction is either committed or rolled back. Any changes made during the transaction are not immediately made permanent in the database, but are rather held in a temporary state until the transaction is committed. If the transaction is rolled back, any changes made during the transaction are discarded and the database state is returned to what it was before the transaction started.

The basic syntax for implementing a transaction in MySQL is as follows:

START TRANSACTION;
-- SQL statements here
COMMIT;
-- or
ROLLBACK;

The ‘START TRANSACTION‘ statement begins the transaction, and all subsequent SQL statements are treated as part of the transaction. Once the SQL statements have been executed, the ‘COMMIT‘ statement is used to commit the changes made during the transaction to the database. Alternatively, the ‘ROLLBACK‘ statement can be used to discard the changes and revert the database to its state before the transaction began.

For example, consider the following scenario:

Suppose we have a table ‘employees‘ with columns ‘id‘ and ‘salary‘, and we want to give a bonus to all employees whose salary is above a certain threshold. We can implement this using a transaction as follows:

START TRANSACTION;
UPDATE employees SET salary = salary * 1.1 WHERE salary > 50000;
COMMIT;

In this example, the ‘UPDATE‘ statement is the SQL operation we want to perform as part of the transaction. The ‘START TRANSACTION‘ statement begins the transaction, and the ‘COMMIT‘ statement at the end of the example commits the changes made by the ‘UPDATE‘ statement to the database.

If, for some reason, we want to roll back the transaction, we can use the following code:

START TRANSACTION;
UPDATE employees SET salary = salary * 1.1 WHERE salary > 50000;
ROLLBACK;

In this example, the ‘ROLLBACK‘ statement is used to discard any changes made by the ‘UPDATE‘ statement and return the database to its state prior to the ‘START TRANSACTION‘ statement.

In summary, transactions in MySQL are used to ensure that a sequence of SQL statements are treated as a single logical unit of work. This can be accomplished using the ‘START TRANSACTION‘, ‘COMMIT‘, and ‘ROLLBACK‘ statements. The ‘COMMIT‘ statement is used to save any changes made during the transaction to the database, while the ‘ROLLBACK‘ statement is used to discard any changes made during the transaction and return the database to its state before the transaction began.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics