Mariadb Truncate Table Statement

MariaDB: TRUNCATE TABLE Statement

This MariaDB tutorial explains how to use the MariaDB TRUNCATE TABLE statement with syntax and examples.

Description

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.

Syntax

The syntax for the TRUNCATE TABLE statement in MariaDB is:

TRUNCATE [TABLE] [database_name.]table_name;

Parameters or Arguments

database_name

Optional. If specified, it is the name of the database.

table_name

The table that you wish to truncate.

Note

  • 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.

Example

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.

For example:

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.

For example:

TRUNCATE TABLE dbd.pages;

This example would truncate the table called pages in the database called dbd.