tencent cloud

TDSQL Boundless

Data Deletion

PDF
フォーカスモード
フォントサイズ
最終更新日: 2026-03-26 14:37:49
Introduces how to delete data from a table, primarily covering the usage methods, differences, and considerations of two statements: DELETE and TRUNCATE TABLE.

DELETE statement

The DELETE statement is standard SQL syntax used to remove specified records from a table.

Syntax

DELETE FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count];

Parameter Description

WHERE where_condition: (Optional) Specifies the deletion condition, deleting only records that meet the condition.
ORDER BY: (Optional) Specifies the deletion order.
LIMIT row_count: (Optional) Limits the number of rows to delete in a single operation.

Use Case

1. Create a test table.
CREATE TABLE sbtest1 (id INT PRIMARY KEY, v1 INT, v2 INT, v3 INT);
2. Delete data that meets the condition.
DELETE FROM sbtest1 WHERE v1 = 100;
3. Delete the specified number of data.
DELETE FROM sbtest1 WHERE v2 < 1000 LIMIT 1000;
4. Delete the specified number of data in the specified order.
DELETE FROM sbtest1 ORDER BY id LIMIT 500;
5. Delete all data from the entire table.
DELETE FROM sbtest1;

TRUNCATE TABLE

TRUNCATE TABLE is used to quickly remove all data from a table.

Syntax

TRUNCATE TABLE tbl_name;

Use Case

1. Clear table data.
-- Create a test table.
CREATE TABLE sbtest1 (
id INT PRIMARY KEY,
v1 INT,
v2 INT,
v3 INT
);

-- Quickly clear all data in the table.
TRUNCATE TABLE sbtest1;
2. Clear a partition.
-- Create a RANGE partitioned table
CREATE TABLE sbtest2 (
id INT,
create_time DATE,
v1 INT,
PRIMARY KEY (id, create_time)
)
PARTITION BY RANGE (TO_DAYS(create_time)) (
PARTITION p202401 VALUES LESS THAN (TO_DAYS('2024-02-01')),
PARTITION p202402 VALUES LESS THAN (TO_DAYS('2024-03-01')),
PARTITION pmax VALUES LESS THAN MAXVALUE
);

-- Quickly clean up data for January 2024.
ALTER TABLE sbtest2
TRUNCATE PARTITION p202401;

Limitations

1. It is recommended to back up important data before deletion operations are performed.
2. It is recommended to verify large-scale deletion operations in a test environment before they are executed in a production environment.
3. Monitor the impact of deletion operations on system performance.
4. Properly configure the transaction timeout period to avoid long-running transactions blocking the system.


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック