共用方式為


TRUNCATE TABLE

適用於:核取記號為「是」Databricks SQL 核取記號為「是」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
 ---- ------ ---