DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Basics » Db2 TRUNCATE TABLE

Db2 TRUNCATE TABLE

Summary: in this tutorial, you will learn how to use the Db2 TRUNCATE TABLE statement to delete all rows from a table.

Introduction to Db2 TRUNCATE TABLE statement

The DELETE statement without a WHERE clause allows you to delete all rows from a table. However, if you have a table with a large volume of data, the DELETE statement will not be efficient.

Fortunately, Db2 provides the TRUNCATE TABLE that also delete all rows from a table, but in a more efficient way.

The following shows the syntax of the TRUNCATE TABLE statement:

TRUNCATE TABLE table_name [DROP STORAGE | REUSE STORAGE] [IGNORE DELETE TRIGGERS | RESTRICT WHEN DELETE TRIGGERS] [IMMEDIATE]
Code language: SQL (Structured Query Language) (sql)

In this syntax:

First, specify the name of the table from which you want to delete all data.

Second, the DROP STORAGE or REUSE STORAGE option determines whether Db2 should drop or reuse the existing storage allocated for the table.

  • The DROP STORAGE releases all storages allocated for the table and make the storage available for use for the same table or any other tables within the tablespace.
  • The REUSE STORAGE makes all storages empty, but continues allocating for the table.

The default option is DROP STORAGE.

Third, the IGNORE DELETE TRIGGERS and RESTRICT WHEN DELETE TRIGGERS options determine the behavior of the truncate operation when the table has associated delete triggers.

  • The IGNORE DELETE TRIGGERS option will not fire any trigger defined on the table.
  • The RESTRICT WHEN DELETE TRIGGERS option will result in an error if the table has one or more deleted triggers.

By default, the TRUNCATE TABLE statement uses IGNORE DELETE TRIGGERS option.

Fourth, the IMMEDIATE option, if specified, will delete all rows from the table without the ability to roll back. On the other hand, if you don’t specify the IMMEDIATE option, you can use a ROLLBACK statement to undo the truncate operation.

Notice that when you use the TRUNCATE TABLE statement in a transaction, you must place it as the first statement.

Db2 TRUNCATE TABLE examples

Let’s take some examples of using the TRUNCATE TABLE statement.

First, create a new table named books2 that has the same structure as the books table:

CREATE TABLE books2 LIKE books;
Code language: SQL (Structured Query Language) (sql)

Then, insert all rows from the books table to the books2 table using the INSERT INTO SELECT statement:

INSERT INTO books2 SELECT * FROM books;
Code language: SQL (Structured Query Language) (sql)

1) Using Db2 TRUNCATE TABLE statement within a transaction example

First, start a new transaction:

BEGIN TRANSACTION;
Code language: SQL (Structured Query Language) (sql)

Second, truncate the book2 table:

TRUNCATE TABLE books2;
Code language: SQL (Structured Query Language) (sql)

Third, rollback the transaction:

ROLLBACK;
Code language: SQL (Structured Query Language) (sql)

Fourth, verify the truncate operation by querying some rows from the books2 table:

SELECT title FROM books2 ORDER BY title LIMIT 5;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

As you can see clearly from the output, the truncate operation on the books2 table was rolled back successfully.

2) Using Db2 TRUNCATE TABLE statement to truncate a table immediately

The following example uses the TRUNCATE TABLE statement to truncate the books2 table immediately with the IMMEDIATE option:

TRUNCATE TABLE books2 IMMEDIATE;
Code language: SQL (Structured Query Language) (sql)

If you query data from the books2 table, you will get an empty result set:

SELECT * FROM books2;
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you’ve learned how to use the Db2 statement to delete all data from a table.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 DROP TABLE
Next Db2 RENAME TABLE

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.