
MySQL TRUNCATE Table
The MySQL can use the TRUNCATE TABLE statement to quickly remove all rows from a table, essentially resetting the table to an empty state. The TRUNCATE TABLE statement is a faster and more efficient way to remove all data from a table compared to the DELETE statement, especially for large tables. However, it’s essential to be aware of the following points:
- The
TRUNCATE TABLEstatement is a DDL (Data Definition Language) statement, and it operates differently from theDELETEstatement, which is a DML (Data Manipulation Language) statement. As a result,TRUNCATE TABLEis faster but less flexible. TRUNCATE TABLEremoves all rows from the table and resets the auto-increment counter, if applicable, to its initial value.- You cannot use a
WHEREclause withTRUNCATE TABLE. It will remove all rows unconditionally. - You need the
DROPprivilege on the table to useTRUNCATE TABLE.
Here’s the basic syntax for TRUNCATE TABLE:
TRUNCATE TABLE table_name;
table_name: The name of the table you want to truncate (remove all rows from).
Example:
TRUNCATE TABLE employees;
This statement removes all rows from the “employees” table.
Use TRUNCATE TABLE when you want to quickly and efficiently remove all data from a table, especially when you don’t need to conditionally delete specific rows. If you need more control or want to delete specific rows based on conditions, you should use the DELETE statement instead.