Skip to content
You're viewing the beta version. Looking for legacy docs? Click here.

Delta default column values

Delta enables the specification of default expressions for columns in Delta tables. When users write to these tables without explicitly providing values for certain columns, or when they explicitly use the DEFAULT SQL keyword for a column, Delta automatically generates default values for those columns.

This information is stored in the StructField corresponding to the column of interest.

How to enable Delta Lake default column values

Section titled “How to enable Delta Lake default column values”

You can enable default column values for a table by setting delta.feature.allowColumnDefaults to enabled:

ALTER TABLE <table_name> SET TBLPROPERTIES (
'delta.feature.allowColumnDefaults' = 'enabled'
)

How to use default columns in SQL commands

Section titled “How to use default columns in SQL commands”
  • For SQL commands that perform table writes, such as INSERT, UPDATE, and MERGE commands, the DEFAULT keyword resolves to the most recently assigned default value for the corresponding column (or NULL if no default value exists). For instance, the following SQL command will use the default value for the second column in the table: INSERT INTO t VALUES (16, DEFAULT);

  • It is also possible for INSERT commands to specify lists of fewer columns than the target table, in which case the engine will assign default values for the remaining columns (or NULL for any columns where no defaults yet exist).

  • The ALTER TABLE ... ADD COLUMN command that introduces a new column to an existing table may not specify a default value for the new column. For instance, the following SQL command is not supported in Delta Lake: ALTER TABLE t ADD COLUMN c INT DEFAULT 16;

  • It is permissible, however, to assign or update default values for columns that were created in previous commands. For example, the following SQL command is valid: ALTER TABLE t ALTER COLUMN c SET DEFAULT 16;