Documentation Home
MySQL 9.1 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.4Mb
PDF (A4) - 40.5Mb
Man Pages (TGZ) - 259.5Kb
Man Pages (Zip) - 366.7Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.1 Reference Manual  / Data Types  /  Data Type Default Values

13.6 Data Type Default Values

Data type specifications can have explicit or implicit default values.

ADEFAULTvalue 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 forNOT NULL AUTO_INCREMENT UNIQUE.

Some aspects of explicitDEFAULT clause handling are version dependent, as described following.

Explicit Default Handling

The default value specified in aDEFAULT clause can be a literal constant or an expression. With one exception, enclose expression default values within parentheses to distinguish them from literal constant default values. Examples:

CREATE TABLE t1 (  -- literal defaults  i INT         DEFAULT 0,  c VARCHAR(10) DEFAULT '',  -- expression defaults  f FLOAT       DEFAULT (RAND() * RAND()),  b BINARY(16)  DEFAULT (UUID_TO_BIN(UUID())),  d DATE        DEFAULT (CURRENT_DATE + INTERVAL 1 YEAR),  p POINT       DEFAULT (Point(0,0)),  j JSON        DEFAULT (JSON_ARRAY()));

The exception is that, forTIMESTAMP andDATETIME columns, you can specify theCURRENT_TIMESTAMP function as the default, without enclosing parentheses. SeeSection 13.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

TheBLOB,TEXT,GEOMETRY, andJSON data types can be assigned a default value only if the value is written as an expression, even if the expression value is a literal:

  • This is permitted (literal default specified as expression):

    CREATE TABLE t2 (b BLOB DEFAULT ('abc'));
  • This produces an error (literal default not specified as expression):

    CREATE TABLE t2 (b BLOB DEFAULT 'abc');

Expression default values must adhere to the following rules. An error occurs if an expression contains disallowed constructs.

  • Literals, built-in functions (both deterministic and nondeterministic), and operators are permitted.

  • Subqueries, parameters, variables, stored functions, and loadable functions are not permitted.

  • An expression default value cannot depend on a column that has theAUTO_INCREMENT attribute.

  • An expression default value for one column can refer to other table columns, with the exception that references to generated columns or columns with expression default values must be to columns that occur earlier in the table definition. That is, expression default values cannot contain forward references to generated columns or columns with expression default values.

    The ordering constraint also applies to the use ofALTER TABLE to reorder table columns. If the resulting table would have an expression default value that contains a forward reference to a generated column or column with an expression default value, the statement fails.

Note

If any component of an expression default value depends on the SQL mode, different results may occur for different uses of the table unless the SQL mode is the same during all uses.

ForCREATE TABLE ... LIKE andCREATE TABLE ... SELECT, the destination table preserves expression default values from the original table.

If an expression default value refers to a nondeterministic function, any statement that causes the expression to be evaluated is unsafe for statement-based replication. This includes statements such asINSERT andUPDATE. In this situation, if binary logging is disabled, the statement is executed as normal. If binary logging is enabled andbinlog_format is set toSTATEMENT, the statement is logged and executed but a warning message is written to the error log, because replicas might diverge. Whenbinlog_format is set toMIXED orROW, the statement is executed as normal.

When inserting a new row, the default value for a column with an expression default can be inserted either by omitting the column name or by specifying the column asDEFAULT (just as for columns with literal defaults):

mysql> CREATE TABLE t4 (uid BINARY(16) DEFAULT (UUID_TO_BIN(UUID())));mysql> INSERT INTO t4 () VALUES();mysql> INSERT INTO t4 () VALUES(DEFAULT);mysql> SELECT BIN_TO_UUID(uid) AS uid FROM t4;+--------------------------------------+| uid                                  |+--------------------------------------+| f1109174-94c9-11e8-971d-3bf1095aa633 || f110cf9a-94c9-11e8-971d-3bf1095aa633 |+--------------------------------------+

However, the use ofDEFAULT(col_name) to specify the default value for a named column is permitted only for columns that have a literal default value, not for columns that have an expression default value.

Not all storage engines permit expression default values. For those that do not, anER_UNSUPPORTED_ACTION_ON_DEFAULT_VAL_GENERATED error occurs.

If a default value evaluates to a data type that differs from the declared column type, implicit coercion to the declared type occurs according to the usual MySQL type-conversion rules. SeeSection 14.3, “Type Conversion in Expression Evaluation”.

Implicit Default Handling

If a data type specification includes no explicitDEFAULT value, MySQL determines the default value as follows:

If the column can takeNULL as a value, the column is defined with an explicitDEFAULT NULL clause.

If the column cannot takeNULL as a value, MySQL defines the column with no explicitDEFAULT clause.

For data entry into aNOT NULL column that has no explicitDEFAULT clause, if anINSERT orREPLACE statement includes no value for the column, or anUPDATE statement sets the column toNULL, 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, the preceding rows are inserted.

  • If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.

Suppose that a tablet 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 becauseDEFAULT(i) cannot produce a value:

INSERT INTO t VALUES();INSERT INTO t VALUES(DEFAULT);INSERT INTO t VALUES(DEFAULT(i));

SeeSection 7.1.11, “Server SQL Modes”.

For a given table, theSHOW CREATE TABLE statement displays which columns have an explicitDEFAULT clause.

Implicit defaults are defined as follows: