TRUNCATE TABLE

描述

TRUNCATE TABLE 语句用于删除表或分区中的所有行。该表不能是视图或外部/临时表。为了同时截断多个分区,用户可以在 partition_spec 中指定分区。如果没有指定 partition_spec,它将删除表中的所有分区。

如果表已缓存,则该命令会清除该表及其所有引用它的依赖项的缓存数据。下次访问该表或其依赖项时,缓存将被延迟填充。

语法

TRUNCATE TABLE table_identifier [ partition_spec ]

参数

示例

-- Create table Student with partition
CREATE TABLE Student (name STRING, rollno INT) PARTITIONED BY (age INT);

SELECT * FROM Student;
+----+------+---+
|name|rollno|age|
+----+------+---+
| ABC|     1| 10|
| DEF|     2| 10|
| XYZ|     3| 12|
+----+------+---+

-- Removes all rows from the table in the partition specified
TRUNCATE TABLE Student partition(age=10);

-- After truncate execution, records belonging to partition age=10 are removed
SELECT * FROM Student;
+----+------+---+
|name|rollno|age|
+----+------+---+
| XYZ|     3| 12|
+----+------+---+

-- Removes all rows from the table from all partitions
TRUNCATE TABLE Student;

SELECT * FROM Student;
+----+------+---+
|name|rollno|age|
+----+------+---+
+----+------+---+