Channi Studies

[MySQL] Day 4. AUTOCOMMIT, COMMIT, ROLLBACK 본문

SQL

[MySQL] Day 4. AUTOCOMMIT, COMMIT, ROLLBACK

Chan Lee 2025. 3. 26. 18:17

Today, I learned about AUTOCOMMIT, COMMIT, and ROLLBACK keywords in MySQL.

 

AUTOCOMMIT is a mode; by default, AUTOCOMMIT is set to ON. 

When AUTOCOMMIT is on (by default), every change you make is immediately nad permanently saved. 

 

It means that you can't roll back if you made an error because each statement is automatically committed.

We can turn it off by using following statement:

SET AUTOCOMMIT = OFF;

 

 

Now, we can create a kind of a savepoint of your database. 

COMMIT;

Simple as that.

 

 

Now, take a look at my database. 

 

Yesterday, Eugene Krabs moved his job so I wanted to remove his data. 

But, I forgot to include WHERE keyword after DELETE FROM keyword!!!

--delete every data from table employees
DELETE FROM employees;

Lost the whole employee data!!!

 

Well, luckily I saved our table yesterday before I got off the work!

I want to roll back to the savepoint from yesterday by using:

ROLLBACK;

SELECT * from employees;

 

Our data was beautifully restored!! 🎉