
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 TABLE
statement is a DDL (Data Definition Language) statement, and it operates differently from theDELETE
statement, which is a DML (Data Manipulation Language) statement. As a result,TRUNCATE TABLE
is faster but less flexible. TRUNCATE TABLE
removes all rows from the table and resets the auto-increment counter, if applicable, to its initial value.- You cannot use a
WHERE
clause withTRUNCATE TABLE
. It will remove all rows unconditionally. - You need the
DROP
privilege 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.