TRUNCATE TABLE
Description
The TRUNCATE TABLE
command is used to quickly delete all records in a table, but the table structure and its attributes will remain unchanged. Unlike the DROP TABLE
command, TRUNCATE TABLE
does not completely delete the table, but only deletes the data in the table.
Syntax
Required Parameters
table_name
: Specify the name of the table to clear data from.
Optional Parameters
IF EXISTS
: If the specified table does not exist, no error is reported and other operations continue.
Example
- Clear all records from the table named
employees
: - Clear the table
employees
, do not report an error if the table does not exist:
TRUNCATE TABLE PARTITION
Function
The TRUNCATE TABLE PARTITION
command is used to clear the data of specified partitions in a partitioned table. It supports clearing partition data through conditional filtering. If you want to delete one or more partitions that meet a certain rule condition at once, you can use an expression to specify the filtering condition, match the partitions through the filtering condition, and batch clear the partition data.
Syntax
- Specify partition deletion:
- Specify partition filter conditions:
Parameter Description
-
table_name
: Specifies the name of the table. -
IF EXISTS
: If the table does not exist, executing this command with this parameter will not result in an error. -
pt_spec
: Required. The partition whose data is to be cleared. When there are multiple partition fields, the format is(partition_col1 = partition_col_value1, partition_col2 = partition_col_value2, ...)
.partition_col
is the partition field, andpartition_col_value
is the partition value. Partition fields are case-insensitive, while partition values are case-sensitive. Supports specifying multiple partition values separated by commas. -
partition_expression
: Can include the following forms:- Comparison operations based on partition columns:
-
<partition_col>
: The name of the partition column in the partition table.<relational_operators>
: Relational operators, such as=
,<
,>
,<=
,>=
,<>
.<partition_col_value>
: The value to be compared with the partition column.- The following example
- Partition Column Comparison Based on Scalar Functions:
-
The
scalar
function is a function that returns a single value and is used to convert the values of partition columns into scalar values. Currently, the partition scalar functions supported by the lakehouse include time functions, JSON functions, data functions, encryption functions, regular expression functions, and string functions.- The following example
- The following example
- Composite Conditions Based on Partition Filters:
<partition_filter>
: Can be any valid partition filter condition.AND|OR
: Logical operators used to combine multiple filter conditions.- Example as follows
- Negative Conditions Based on Partition Filters:
NOT
: Logical NOT operator, used to negate a filter condition.- Example as follows
- Example as follows
- List of Multiple Partition Filters:
- Allows you to list multiple partition filter conditions, separated by commas.
Usage Example
- Specify partition deletion
- Use filter conditions to delete and clear all partitions in the
sales
table that meet the partition conditionsyear < 2024 AND quarter = 1
: