Data type specifications can have explicit or implicit default values.数据类型规范可以有显式或隐式的默认值。
A 数据类型规范中的DEFAULT
clause in a data type specification explicitly indicates a default value for a column. value
DEFAULT
子句显式指示列的默认值。value
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. SERIAL DEFAULT VALUE
是一种特殊情况。In the definition of an integer column, it is an alias for 在整数列的定义中,它是NOT NULL AUTO_INCREMENT UNIQUE
.NOT NULL AUTO_INCREMENT UNIQUE
的别名。
Some aspects of explicit 显式DEFAULT
clause handling are version dependent, as described following.DEFAULT
子句处理的某些方面依赖于版本,如下所述。
The default value specified in a DEFAULT
clause can be a literal constant or an expression. DEFAULT
子句中指定的默认值可以是文字常量或表达式。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, for 例外情况是,对于TIMESTAMP
and DATETIME
columns, you can specify the CURRENT_TIMESTAMP
function as the default, without enclosing parentheses. TIMESTAMP
和DATETIME
列,您可以将CURRENT_TIMESTAMP
函数指定为默认值,而不使用括号。See Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.请参阅第11.2.5节,“时间戳和日期时间的自动初始化和更新”。
The BLOB
, TEXT
, GEOMETRY
, and JSON
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:BLOB
、TEXT
、GEOMETRY
和JSON
数据类型只能在值作为表达式写入时指定默认值,即使表达式值是文本:
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 the 表达式默认值不能依赖于具有AUTO_INCREMENT
attribute.AUTO_INCREMENT
属性的列。
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 of 排序约束也适用于使用ALTER TABLE
to reorder table columns. ALTER TABLE
对表列重新排序。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.如果结果表的表达式默认值包含对生成的列或具有表达式默认值的列的正向引用,则语句将失败。
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.如果表达式默认值的任何组件依赖于SQL模式,则表的不同使用可能会产生不同的结果,除非在所有使用过程中SQL模式相同。
For 对于CREATE TABLE ... LIKE
and CREATE TABLE ... SELECT
, the destination table preserves expression default values from the original table.CREATE TABLE ... LIKE
语句和CREATE TABLE ... SELECT
语句,目标表保留原始表中的表达式默认值。
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 as 这包括INSERT
and UPDATE
. INSERT
语句和UPDATE
等语句。In this situation, if binary logging is disabled, the statement is executed as normal. 在这种情况下,如果禁用二进制日志记录,则语句将正常执行。If binary logging is enabled and 如果启用了二进制日志记录,并且binlog_format
is set to STATEMENT
, the statement is logged and executed but a warning message is written to the error log, because replication slaves might diverge. binlog_format
设置为STATEMENT
,则会记录并执行该语句,但会向错误日志中写入警告消息,因为复制从属服务器可能会发生分歧。When 当binlog_format
is set to MIXED
or ROW
, the statement is executed as normal.binlog_format
设置为MIXED
或ROW
时,语句将正常执行。
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 as 插入新行时,可以通过省略列名或将列指定为DEFAULT
(just as for columns with literal defaults):DEFAULT
来插入表达式为默认值的列的默认值(与使用文字默认值的列相同):
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 of 但是,仅允许对具有文字默认值的列使用DEFAULT(
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.col_name
)DEFAULT(
来指定命名列的默认值,而不允许对具有表达式默认值的列使用它。col_name
)
Not all storage engines permit expression default values. 并非所有存储引擎都允许表达式默认值。For those that do not, an 对于那些不支持的,会发生一个ER_UNSUPPORTED_ACTION_ON_DEFAULT_VAL_GENERATED
error occurs.ER_UNSUPPORTED_ACTION_ON_DEFAULT_VAL_GENERATED
错误。
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. 如果默认值的计算结果是与声明的列类型不同的数据类型,则会根据通常的MySQL类型转换规则对声明的类型进行隐式强制。See Section 12.3, “Type Conversion in Expression Evaluation”.请参阅第12.3节“表达式求值中的类型转换”。
With one exception, the default value specified in a 除了一个例外,DEFAULT
clause must be a literal constant; it cannot be a function or an expression. DEFAULT
子句中指定的默认值必须是一个文本常量;它不能是函数或表达式。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
. NOW()
或CURRENT_DATE
。The exception is that, for 例外情况是,对于TIMESTAMP
and DATETIME
columns, you can specify CURRENT_TIMESTAMP
as the default. TIMESTAMP
和DATETIME
列,可以将CURRENT_TIMESTAMP
指定为默认值。See Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.请参阅第11.2.5节,“时间戳和日期时间的自动初始化和更新”。
The 不能为BLOB
, TEXT
, GEOMETRY
, and JSON
data types cannot be assigned a default value.BLOB
、TEXT
、GEOMETRY
和JSON
数据类型分配默认值。
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. 如果默认值的计算结果是与声明的列类型不同的数据类型,则会根据通常的MySQL类型转换规则对声明的类型进行隐式强制。See Section 12.3, “Type Conversion in Expression Evaluation”.请参阅第12.3节,“表达式求值中的类型转换”。
If a data type specification includes no explicit 如果数据类型规范不包含显式的DEFAULT
value, MySQL determines the default value as follows:DEFAULT
值,MySQL将按如下方式确定默认值:
If the column can take 如果该列可以将NULL
as a value, the column is defined with an explicit DEFAULT NULL
clause.NULL
作为值,则该列将使用显式的DEFAULT NULL
子句定义。
If the column cannot take 如果列不能将NULL
as a value, MySQL defines the column with no explicit DEFAULT
clause.NULL
作为值,MySQL将定义没有显式DEFAULT
子句的列。
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, or an UPDATE
statement sets the column to NULL
, MySQL handles the column according to the SQL mode in effect at the time:DEFAULT
子句的NOT NULL
列中的数据输入,如果INSERT
或REPLACE
语句不包含该列的值,或者UPDATE
语句将该列设置为NULL
,则MySQL将根据当时有效的SQL模式处理该列:
If strict SQL mode is enabled, an error occurs for transactional tables and the statement is rolled back. 如果启用了严格SQL模式,则事务表将发生错误并回滚该语句。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.如果未启用严格模式,MySQL将列设置为列数据类型的隐式默认值。
Suppose that a table 假设表t
is defined as follows:t
的定义如下:
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. i
没有显式的默认值,因此在严格模式下,下面的每个语句都会产生一个错误,并且不会插入任何行。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:DEFAULT(i)
不能生成值:
INSERT INTO t VALUES(); INSERT INTO t VALUES(DEFAULT); INSERT INTO t VALUES(DEFAULT(i));
See Section 5.1.11, “Server SQL Modes”.请参阅第5.1.11节,“服务器SQL模式”。
For a given table, the 对于给定的表,SHOW CREATE TABLE
statement displays which columns have an explicit DEFAULT
clause.SHOW CREATE TABLE
语句显示哪些列具有显式DEFAULT
子句。
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.0
,但对于使用AUTO_INCREMENT
属性声明的整数或浮点类型,默认值是序列中的下一个值。
For date and time types other than 对于TIMESTAMP
, the default is the appropriate “zero” value for the type. TIMESTAMP
以外的日期和时间类型,默认值是该类型的适当“零”值。This is also true for 如果启用了TIMESTAMP
if the explicit_defaults_for_timestamp
system variable is enabled (see Section 5.1.8, “Server System Variables”). explicit_defaults_for_timestamp
系统变量,则TIMESTAMP也是如此(请参阅第5.1.8节,“服务器系统变量”)。Otherwise, for the first 否则,对于表中的第一个TIMESTAMP
column in a table, the default value is the current date and time. TIMESTAMP
列,默认值为当前日期和时间。See Section 11.2, “Date and Time Data Types”.请参阅第11.2节,“日期和时间数据类型”。
For string types other than 对于ENUM
, the default value is the empty string. ENUM
以外的字符串类型,默认值为空字符串。For 对于ENUM
, the default is the first enumeration value.ENUM
,默认值是第一个枚举值。