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 NULLas a value, the column is defined with an explicitDEFAULT NULLclause.
- If the column cannot take NULLas the value, TiDB defines the column with no explicitDEFAULTclause.
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_INCREMENTattribute, 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. ForTIMESTAMP, the default value is the current date and time.
- For string types other than ENUM, the default value is the empty string. ForENUM, the default is the first enumeration value.