TRUNCATE TABLE
Aplica-se a: Databricks SQL Databricks Runtime
Remove todas as linhas de uma tabela ou partições. A tabela não pode ser uma vista ou uma tabela externa ou temporária. Para truncar várias partições ao mesmo tempo, especifique as partições em partition_spec
. Se não partition_spec
for especificado, remove todas as partições na tabela.
Nota
O Delta Lake não suporta cláusulas de partição para TRUNCATE
.
Se a tabela estiver em cache, o comando limpa os dados em cache da tabela e todos os respetivos dependentes que se referem à mesma. A cache será preenchida preguiçosamente quando a tabela ou os dependentes forem acedidos da próxima vez.
Sintaxe
TRUNCATE TABLE table_name [ PARTITION clause ]
Parâmetros
-
O nome da tabela a truncar. O nome não pode incluir uma especificação temporal. Se não for possível encontrar a tabela, o Azure Databricks gera um erro de TABLE_OR_VIEW_NOT_FOUND .
-
Especificação opcional de uma partição. Não suportado para o 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
---- ------ ---