This MariaDB tutorial explains how to use the MariaDB TRUNCATE TABLE statement with syntax and examples.
The TRUNCATE TABLE statement is used to remove all records from a table in MariaDB. It performs the same function as a DELETE statement without a WHERE clause.
Warning: If you truncate a table, the TRUNCATE TABLE statement can not be rolled back.
The syntax for the TRUNCATE TABLE statement in MariaDB is:
TRUNCATE [TABLE] [database_name.]table_name;
Parameters or Arguments
Optional. If specified, it is the name of the database.
The table that you wish to truncate.
- When you truncate a table, the AUTO_INCREMENT counters on the table will be reset.
- MariaDB truncates the table by dropping and creating the table. Thus, the DELETE triggers for the table do not fire during the truncation.
In MariaDB, truncating a table is a fast way to clear out records from a table if you don't need to worry about rolling back. Let's look at an example of how to use the TRUNCATE TABLE statement in MariaDB.
TRUNCATE TABLE sites;
This example would truncate the table called sites and remove all records from that table.
It would be equivalent to the following DELETE statement in MariaDB:
DELETE FROM sites;
Both of these statements would result in all data from the sites table being deleted. The main difference between the two is that you can roll back the DELETE statement if you choose, but you can't roll back the TRUNCATE TABLE statement.
Let's look at one more example where we prefix the table name with the database name.
TRUNCATE TABLE totn.pages;
This example would truncate the table called pages in the database called totn.