Default Values

The DEFAULT value clause in a data type specification indicates a default value for a column. The default value must be a constant and cannot be a function or an expression. But for the time type, you can specify the NOW, CURRENT_TIMESTAMP, LOCALTIME, and LOCALTIMESTAMP functions as the default for TIMESTAMP and DATETIME columns.

The BLOB, TEXT, and JSON columns cannot be assigned a default value.

If a column definition includes no explicit DEFAULT value, TiDB determines the default value as follows:

  • If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.
  • If the column cannot take NULL as the value, TiDB defines the column with no explicit DEFAULT clause.

For data entry into a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, TiDB handles the column according to the SQL mode in effect at the time:

  • If strict SQL mode is enabled, an error occurs for transactional tables, and the statement is rolled back. For nontransactional tables, an error occurs.
  • If strict mode is not enabled, TiDB sets the column to the implicit default value for the column data type.

Implicit defaults are defined as follows:

  • For numeric types, the default is 0. If declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence.
  • For date and time types other than TIMESTAMP, the default is the appropriate "zero" value for the type. For TIMESTAMP, the default value is the current date and time.
  • For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.

Was this page helpful?