Rechercher dans le manuel MySQL
11.7 Data Type Default Values
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. Examples:
value
SERIAL DEFAULT VALUE
is a special case. In the
definition of an integer column, it is an alias for NOT
NULL AUTO_INCREMENT UNIQUE
.
Some aspects of explicit DEFAULT
clause
handling are version dependent, as described following.
Handling of Explicit Defaults as of MySQL 8.0.13
The default value specified in a DEFAULT
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:
- -- literal defaults
- -- expression defaults
- );
The exception is that, for
TIMESTAMP
and
DATETIME
columns, you can specify
the CURRENT_TIMESTAMP
function as
the default, without enclosing parentheses. See
Section 11.3.4, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.
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:
This is permitted (literal default specified as expression):
This produces an error (literal default not specified as expression):
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 user-defined functions are not permitted.
An expression default value cannot depend on a column that has the
AUTO_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 of
ALTER 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.
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.
For CREATE
TABLE ... LIKE
and
CREATE
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 as
INSERT
,
UPDATE
, and
ALTER TABLE
. 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. When
binlog_format
is set to
MIXED
or ROW
, the
statement is not executed and an error message is written to the
error log.
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):
- +--------------------------------------+
- | 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
)
Not all storage engines permit expression default values. For
those that do not, an
ER_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. See Section 12.2, “Type Conversion in Expression Evaluation”.
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 TIMESTAMP
and
DATETIME
columns, you can specify
CURRENT_TIMESTAMP
as the default.
See Section 11.3.4, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.
The BLOB
,
TEXT
,
GEOMETRY
, and
JSON
data types cannot be
assigned a default value.
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. See Section 12.2, “Type Conversion in Expression Evaluation”.
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. Exception: If the column is
defined as part of a PRIMARY KEY
but not
explicitly as NOT NULL
, MySQL creates it as a
NOT NULL
column (because PRIMARY
KEY
columns must be NOT NULL
).
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:
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 will have 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:
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:
See Section 5.1.11, “Server SQL Modes”.
For a given table, the 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 theAUTO_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. This is also true forTIMESTAMP
if theexplicit_defaults_for_timestamp
system variable is enabled (see Section 5.1.8, “Server System Variables”). Otherwise, for the firstTIMESTAMP
column in a table, the default value is the current date and time. See Section 11.3, “Date and Time Types”.For string types other than
ENUM
, the default value is the empty string. ForENUM
, the default is the first enumeration value.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-data-type-defaults.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.