TRUNCATE TABLE

Aplica-se a:Marque Sim Databricks SQL Marque Sim Databricks Runtime

Remove todas as linhas de uma tabela ou partição(s). A tabela não pode ser uma vista ou uma tabela temporária. As tabelas Delta Lake podem ser truncadas, quer sejam geridas ou externas. Para outros formatos de tabela, como Parquet, CSV ou ORC, a tabela não pode ser externa. Para truncar múltiplas partições ao mesmo tempo, especifique as partições em partition_spec. Se não partition_spec for especificado, remove todas as partições da tabela.

Nota

Delta Lake não suporta cláusulas de partição para TRUNCATE.

Se a tabela estiver armazenada em cache, o comando limpará os dados armazenados em cache da tabela e todos os seus dependentes que se referem a ela. O cache será preenchido de forma preguiçosa quando a tabela ou os dependentes forem acessados na próxima vez.

Sintaxe

TRUNCATE TABLE table_name [ PARTITION clause ]

Parâmetros

Exemplos

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