Methods for clearing data in DB2 tables

Source: Internet
Author: User
Tags table definition

 

For users who use the DB2 database, there is sometimes a need to clear the data in the table. Here we provide four data deletion methods for users to choose based on their own needs:

1. Use the delete statement, namely:

Delete from <Table Name>

This statement clears all data in the table. However, because this operation logs, the execution speed is relatively slow. Note that if the table is large, to ensure the deletion operation is successful, check whether there is sufficient log space.

2. 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 several methods, but the deleted data cannot be recovered.

3. Use the import or load command, that is:

Import from/dev/null of del replace into <Table Name> -- (UNIX System)
Or import from <empty File> of del replace into <Table Name>

Load from/dev/null of del replace into <Table Name> nonrecoverable -- (UNIX System)

Or load from <empty 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.

4. 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 used the db2look command to obtain the table definition script, you can also delete the entire table and recreate the table. If the table is large, data is cleared faster than method 1.

If you want to implement the truncate table effect, use the import from empty file method. Other methods are not good.

For users who use the DB2 database, there is sometimes a need to clear the data in the table. Here we provide four data deletion methods for users to choose based on their own needs:

1. Use the delete statement, namely:

Delete from <Table Name>

This statement clears all data in the table. However, because this operation logs, the execution speed is relatively slow. Note that if the table is large, to ensure the deletion operation is successful, check whether there is sufficient log space.

2. 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 several methods, but the deleted data cannot be recovered.

3. Use the import or load command, that is:

Import from/dev/null of del replace into <Table Name> -- (UNIX System)
Or import from <empty File> of del replace into <Table Name>

Load from/dev/null of del replace into <Table Name> nonrecoverable -- (UNIX System)

Or load from <empty 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.

4. 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 used the db2look command to obtain the table definition script, you can also delete the entire table and recreate the table. If the table is large, data is cleared faster than method 1.

If you want to implement the truncate table effect, use the import from empty file method. Other methods are not good.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.