更新时间:2024-06-14 GMT+08:00
分享

TRUNCATE TABLE

语法

TRUNCATE [TABLE] table_name [PARTITION partition_spec];

partition_spec:

: (partition_column = partition_col_value, partition_column = partition_col_value, ...)

描述

从表或分区中移除所有行。用户可以通过partition_spec一次性删除分区表的多个分区,如果不指定就一次清除分区表的所有分区。当表属性“auto.purge”采用默认值“false”时,被删除的数据行将保存到文件系统的回收站,否则,当“auto.purge”设置为“true”时,数据行将被直接删除。

限制

目标表必须是管控表(表属性external=false),否则执行语句将报错。

示例

-- 删除原生/管控表
Create table simple(id int, name string);
 
Insert into simple values(1,'abc'),(2,'def');
 
select * from simple;
 id | name
----|------
  1 | abc
  2 | def
(2 rows)
 
Truncate table simple;
 
select * from simple;
 id | name
----|------
(0 rows)
 
--删除表分区
Create table tb_truncate_part (id int, name string) partitioned by (age int, state string);
 
Insert into tb_truncate_part values (1,'abc',10,'ap'),(2,'abc',10,'up'),(3,'abc',20,'ap'),(4,'abc',20,'up');
 
select * from tb_truncate_part;
 id | name | age | state
----|------|-----|-------
  2 | abc  |  10 | up
  3 | abc  |  20 | ap
  1 | abc  |  10 | ap
  4 | abc  |  20 | up
(4 rows
 
Truncate table tb_truncate_part partition (state = 'ap', age = 10);
 
select * from tb_truncate_part;
id | name | age | state
----|------|-----|-------
  4 | abc  |  20 | up
  2 | abc  |  10 | up
  3 | abc  |  20 | ap
(3 rows)
分享:

    相关文档

    相关产品