TRUNCATE TABLE

适用于:check marked yes Databricks SQL check marked yes Databricks Runtime

从表或分区中删除所有行。 表不能是视图,也不能是外部表或临时表。 若要同时截断多个分区,请在 partition_spec 中指定分区。 如果未指定 partition_spec,则删除表中的所有分区。

注意

Delta Lake 不支持 TRUNCATE 的分区子句。

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

语法

TRUNCATE TABLE table_name [ PARTITION clause ]

参数

示例

-- 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

-- Remove all rows from the table in the specified partition
> 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

-- Remove all rows from the table from all partitions
> TRUNCATE TABLE Student;

> SELECT * FROM Student;
 name rollno age
 ---- ------ ---