Chapter 3. The SQL language 79
3.2.3 The basic truncation
Like any delete function, to execute TRUNCATE you must have an exclusive
access to that table. TRUNCATE is very fast and efficient in scenarios where the
table contains large amounts of data. This is because the deletion does not
happen on individual rows, which means that no delete triggers are fired. So any
logging of activity or other recording done in a trigger does not occur. This is one
of the main reasons why TRUNCATE cannot be used on tables that are a part of
Enterprise Replication (ER) but can be used with High Availability Data
Replication (HDR).
Example 3-4 shows the truncate statement in action. The examples in this
section all use a table called
dual, which has just one column and a single row of
data. TRUNCATE is not a large operation for a table with just one column and
one row. However, the operation is very fast on table of any size because the
individual rows are not deleted.
Example 3-4 Preparing the TRUNCATE data
> CREATE TABLE dual ( c1 INTEGER);
Table created.
> TRUNCATE dual;
Table truncated.
> SELECT * FROM dual;
c1
No rows found.
> INSERT INTO dual VALUES (1);
1 row(s) inserted.
> SELECT * FROM dual;
c1
1
1 row(s) retrieved.