一、描述
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|
+----+------+---+
+----+------+---+
835

被折叠的 条评论
为什么被折叠?



