Quickly clear table data
For users who use the DB2 database, data in the table needs to be cleared sometimes. Here we provide four data deletion methods for you to choose based on your needs:
Use the delete statement:
Delete from <Table Name>
This statement clears all data in the table, but the execution speed is relatively slow because this operation logs. In addition, if the table is large, you should consider whether there is sufficient log space to ensure the deletion operation is successful.
Use the not logged initially option, that is:
Alter table <Table Name> activate not logged initially with empty table
This method can be used only when the not logged initially option is selected for the table to be operated during creation. The entire delete operation will not be logged, so the execution speed is the fastest among the four methods, but the deleted data cannot be recovered.
Use the load command:
Load from/dev/null of del replace into <Table Name> nonrecoverable -- (UNIX system or load from <null File> of del replace into <Table Name> nonrecoverable
In this method, the replace import method first clears all data in the table, and then import/load imports empty data into the table, thus clearing the data.
Use the drop/create table statement, that is:
Drop table <Table Name> Create Table <Table Name> <field definition>
If you have saved a table definition statement or obtained a table definition script using the db2look command, you can also delete the entire table and recreate the table. If the table is large, data is cleared faster than the delete statement. However, when using this method, note that if the table contains many foreign keys, you must maintain the integrity of the foreign keys.
9.7 added truncate table tabname immediate
Quickly delete all rows in the table and cannot roll back