What is the purpose of truncate in MySQL?

The TRUNCATE statement in MySQL is used to delete all data from a table while preserving the structure, indexes, and constraints. This means that unlike the DELETE statement, it not only deletes the data in the table, but also removes all indexes, frees up the space occupied, and resets the table size to zero.

The TRUNCATE statement is faster than the DELETE statement because it directly deletes the data in the table without deleting row by row. Additionally, after executing the TRUNCATE statement, the auto-increment counter will reset to 1 and the deleted data will not be stored in the rollback log.

It is important to note that the TRUNCATE statement is a Data Definition Language (DDL) statement, not a Data Manipulation Language (DML) statement, so it cannot be rolled back. Additionally, only users with sufficient privileges can execute the TRUNCATE statement.

bannerAds