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