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 sementara. Tabel Delta Lake dapat dipotong baik dalam bentuk dikelola maupun eksternal. Untuk format tabel lain, seperti Parquet, CSV, atau ORC, tabel tidak boleh bersifat eksternal. Untuk memotong beberapa partisi sekaligus, tentukan partisi di partition_spec. Jika tidak ditentukan partition_spec , semua partisi dalam tabel akan dihapus.

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