TRUNCATE TABLE

最近更新时间:2026-05-20 14:11:22

我的收藏

描述

TRUNCATE TABLE 语句用于清空表中的全部数据,但保留表结构(Schema)和元数据不变。与 DELETE 不同,TRUNCATE 是一个高效操作,通常直接删除底层数据文件而非逐行删除。

语法

TRUNCATE TABLE table_name;

注意事项

TRUNCATE 操作不可回滚。
清空的是表中全部行数据,Schema、TBLPROPERTIES、分区定义等均保持不变。
TRUNCATE 比 DELETE FROM table 效率高得多。

示例

创建基础表

DROP TABLE IF EXISTS tci_truncate_test;

CREATE TABLE tci_truncate_test (
id BIGINT NOT NULL,
name STRING,
score DOUBLE
) USING tci
TBLPROPERTIES ('primary-key' = 'id');

插入数据并验证

INSERT INTO tci_truncate_test
VALUES (1, 'Alice', 95.5), (2, 'Bob', 88.0), (3, 'Charlie', 92.3);

SELECT count(*) as cnt FROM tci_truncate_test;
-- 结果: 3 条记录

TRUNCATE TABLE 清空表数据

TRUNCATE TABLE tci_truncate_test;

验证表已清空但结构保留

SELECT count(*) as cnt FROM tci_truncate_test;
-- 结果: 0 条记录

DESCRIBE tci_truncate_test;
-- 结构仍保留

TRUNCATE 后可继续写入

INSERT INTO tci_truncate_test
VALUES (10, 'NewUser', 99.9);

SELECT * FROM tci_truncate_test;
-- 新数据可正常读取