TRUNCATE — empties a table


TRUNCATE TABLE [schema.]tableName


TRUNCATE operates on the table instead of individual rows. Therefore the command TRUNCATE TABLE removes all rows from a table without logging individual row deletions. It is more efficient than using DELETE without a WHERE clause.

The user must have the DELETE privilege on the table in order to invoke TRUNCATE on it. After execution of TRUNCATE TABLE, disk space is not reclaimed immediately. The NuoDB Check tool (see NuoDB Archive) needs to run to reclaim this new unused disk space.

Note: Inside an explicit transaction, ROLLBACK has no effect when using TRUNCATE TABLE, unlike DELETE, which can be rolled back inside an explicit transaction.

Note: When using multiple statements in a single call, the TRUNCATE command can only be used if it is the first statement in the call. If it is not, an error is returned.

For example:

SQL> create table if not exists a (a int); truncate table a;
Internal error: failed precondition !statementStack.empty() && statementStack.back().get()->getNodeType() != Select, file /Users/asenac/workdirs/master/Alpha/SQL/SQLIRBuilder.cpp, line 306



CREATE TABLE tab_truncate (col1 STRING);
INSERT INTO tab_truncate VALUES ('row1'),('row2'),('row3');
SELECT * FROM tab_truncate;

TRUNCATE TABLE tab_truncate; 
SELECT * FROM tab_truncate;

/* There are no records selected */