TRUNCATE TABLE

Berlaku untuk:centang ditandai ya Databricks SQL centang ditandai ya Databricks Runtime

Menghapus semua baris dari tabel atau partisi. Tabel tidak boleh berupa tampilan atau tabel eksternal atau sementara. Untuk memotong beberapa partisi sekaligus, tentukan partisi di partition_spec. Jika tidak ada partition_spec yang ditentukan, menghapus semua partisi dalam tabel.

Catatan

Delta Lake tidak mendukung klausul partisi untuk TRUNCATE.

Jika tabel di-cache, perintah menghapus data tabel yang di-cache dan semua dependennya yang merujuk ke tabel tersebut. Cache akan diisi secara bertahap ketika tabel atau dependen diakses saat berikutnya.

Sintaks

TRUNCATE TABLE table_name [ PARTITION clause ]

Parameter-parameternya

Contoh

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