TRUNCATE TABLE
Aplica-se a: SQL do Databricks Runtime do Databricks
Remove todas as linhas de uma tabela ou partição (ões). A tabela não deve ser uma exibição ou uma tabela externa ou temporária. Para truncar várias partições de uma vez, especifique as partições em partition_spec
. Se nenhuma partition_spec
estiver especificada, removerá todas as partições na tabela.
Observação
O Delta Lake não dá suporte a 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 dependentes que se referem a ela. O cache será preenchido de forma ociosa quando a tabela ou os dependentes forem acessados da próxima vez.
Sintaxe
TRUNCATE TABLE table_name [ PARTITION clause ]
Parâmetros
-
O nome da tabela a ser truncada. O nome não deve incluir uma especificação temporal ou especificação de opções. Se a tabela não puder ser encontrada, o Azure Databricks gerará um erro TABLE_OR_VIEW_NOT_FOUND.
-
Especificação opcional de uma partição. Sem suporte para Delta Lake.
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
---- ------ ---