TRUNCATE TABLE
說明
TRUNCATE TABLE
陳述式會移除資料表或分割區中的所有列。資料表不得為檢視或外部/暫時資料表。若要一次截斷多個分割區,使用者可以在 partition_spec
中指定分割區。如果未指定 partition_spec
,它會移除資料表中的所有分割區。
如果資料表已快取,指令會清除資料表的快取資料,以及所有參考該資料表的相依資料。下次存取資料表或相依資料時,快取會延遲填入。
語法
TRUNCATE TABLE table_identifier [ partition_spec ]
參數
-
table_identifier
指定資料表名稱,可以選擇使用資料庫名稱限定。
語法:
[ database_name. ] table_name
-
partition_spec
一個選用參數,指定分割區的逗號分隔清單,包含金鑰和值對。
語法:
PARTITION ( partition_col_name = partition_col_val [ , ... ] )
範例
-- 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|
+----+------+---+
+----+------+---+