Data type specifications can have explicit or implicit default values.
*note data-type-defaults-explicit::
*note data-type-defaults-implicit::
Explicit Default Handling
A 'DEFAULT VALUE' clause in a data type specification explicitly indicates a default value for a column. Examples:
CREATE TABLE t1 (
i INT DEFAULT -1,
c VARCHAR(10) DEFAULT '',
price DOUBLE(16,2) DEFAULT '0.00'
);
'SERIAL DEFAULT VALUE' is a special case. In the definition of an integer column, it is an alias for 'NOT NULL AUTO_INCREMENT UNIQUE'.
With one exception, the default value specified in a 'DEFAULT' clause must be a literal constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as 'NOW()' or 'CURRENT_DATE'. The exception is that, for note 'TIMESTAMP': datetime. and note 'DATETIME': datetime. columns, you can specify 'CURRENT_TIMESTAMP' as the default. See *note timestamp-initialization::.
The note 'BLOB': blob, note 'TEXT': blob, 'GEOMETRY', and *note 'JSON': json. data types cannot be assigned a default value.
Implicit Default Handling
If a data type specification includes no explicit 'DEFAULT' value, MySQL 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 a value, MySQL defines the column with no explicit 'DEFAULT' clause.
For data entry into a 'NOT NULL' column that has no explicit 'DEFAULT' clause, if an note 'INSERT': insert. or note 'REPLACE': replace. statement includes no value for the column, or an *note 'UPDATE': update. statement sets the column to 'NULL', MySQL 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, but if this happens for the second or subsequent row of a multiple-row statement, any rows preceding the error have already been inserted.
If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
Suppose that a table 't' is defined as follows:
CREATE TABLE t (i INT NOT NULL);
In this case, 'i' has no explicit default, so in strict mode each of the following statements produce an error and no row is inserted. When not using strict mode, only the third statement produces an error; the implicit default is inserted for the first two statements, but the third fails because 'DEFAULT(i)' cannot produce a value:
INSERT INTO t VALUES();
INSERT INTO t VALUES(DEFAULT);
INSERT INTO t VALUES(DEFAULT(i));
See *note sql-mode::.
For a given table, the *note 'SHOW CREATE TABLE': show-create-table. statement displays which columns have an explicit 'DEFAULT' clause.
Implicit defaults are defined as follows:
For numeric types, the default is '0', with the exception that for integer or floating-point types declared with the 'AUTO_INCREMENT' attribute, the default is the next value in the sequence.
For date and time types other than note 'TIMESTAMP': datetime, the default is the appropriate 'zero' value for the type. This is also true for note 'TIMESTAMP': datetime. if the 'explicit_defaults_for_timestamp' system variable is enabled (see note server-system-variables::). Otherwise, for the first note 'TIMESTAMP': datetime. column in a table, the default value is the current date and time. See *note date-and-time-types::.
For string types other than note 'ENUM': enum, the default value is the empty string. For note 'ENUM': enum, the default is the first enumeration value.
File: manual.info.tmp, Node: storage-requirements, Next: choosing-types, Prev: data-type-defaults, Up: data-types