Default Values
Syntax
- DEFAULT default_expression: Defines a default value for a newly added column. If the value of this column is not specified in INSERT, UPDATE, or MERGE operations, this default value will be used automatically. For data rows that existed before the column was added, the column will be filled with null. Supports non-deterministic functions such as (current_date\random\current_timestamp\context functions) and constant values.
Difference Between Generated Columns and Default Values
-
Partition Column Support:
- Default Values: Currently, setting default values for partition columns is not supported.
- Generated Columns: Supports generating values for partition columns.
-
Function Support:
- Default Values: Supports the use of non-deterministic functions, such as
current_timestamp()
. - Generated Columns: Does not support non-deterministic functions, only deterministic scalar functions can be used.
- Default Values: Supports the use of non-deterministic functions, such as
-
Value Specification in Insert Operations:
- Default Values: When inserting data, a static value can be specified for the column. If not specified, the default value is used.
- Generated Columns: When inserting data, values cannot be specified for generated columns; their values are entirely determined by the generation expression.
-
Source of Column Values:
- Default Values: Does not support column values derived from the transformation of other columns.
- Generated Columns: Supports column values derived from the transformation of other columns, i.e., values can be calculated based on other columns.
-
Handling of Existing Data Rows:
- Default Values: When adding a column with a default value to an existing table, the column in existing data rows will be filled with null.
- Generated Columns: For existing data rows, the value of the generated column will be converted according to the generation expression and display the converted data.
Usage Restrictions
- Setting default values for partition columns is not supported.
- Supports batch interface writing, including batch import in Studio data integration. Default values will not take effect when writing through real-time interfaces, as all columns must be specified when calling real-time interfaces.
- Does not support functions similar to generated columns that can reference other columns.
Inserting Data
Specify Default Values When Adding Fields
Syntax
- DEFAULT default_expression: Defines a default value for the newly added column. If the value of this column is not specified in INSERT, UPDATE, or MERGE operations, this default value will be automatically used. For data rows that existed before the column was added, the column will be filled with null.
Usage Restrictions
- Adding a default value to an existing column is not supported
Examples
Add a column and specify a default value
In this example, new_col
will be added after existing_col
, and new_col
for all existing data rows will be set to the default value null.