Rechercher dans le manuel MySQL
13.1.20.7 CHECK Constraints
Prior to MySQL 8.0.16, CREATE
TABLE
permits only the following limited version of
table CHECK
constraint syntax, which is
parsed and ignored:
As of MySQL 8.0.16, CREATE TABLE
permits the core features of table and column
CHECK
constraints, for all storage engines.
CREATE TABLE
permits the
following CHECK
constraint syntax, for both
table constraints and column constraints:
The optional symbol
specifies a name
for the constraint. If omitted, MySQL generates a name from the
table name, a literal _chk_
, and an ordinal
number (1, 2, 3, ...). Constraint names have a maximum length of
64 characters. They are case sensitive, but not accent
sensitive.
expr
specifies the constraint
condition as a boolean expression that must evaluate to
TRUE
or UNKNOWN
(for
NULL
values) for each row of the table. If
the condition evaluates to FALSE
, it fails
and a constraint violation occurs. The effect of a violation
depends on the statement being executed, as described later in
this section.
The optional enforcement clause indicates whether the constraint is enforced:
If omitted or specified as
ENFORCED
, the constraint is created and enforced.If specified as
NOT ENFORCED
, the constraint is created but not enforced.
A CHECK
constraint is specified as either a
table constraint or column constraint:
A table constraint does not appear within a column definition and can refer to any table column or columns. Forward references are permitted to columns appearing later in the table definition.
A column constraint appears within a column definition and can refer only to that column.
Consider this table definition:
- (
- );
The definition includes table constraints and column constraints, in named and unnamed formats:
The first constraint is a table constraint: It occurs outside any column definition, so it can (and does) refer to multiple table columns. This constraint contains forward references to columns not defined yet. No constraint name is specified, so MySQL generates a name.
The next three constraints are column constraints: Each occurs within a column definition, and thus can refer only to the column being defined. One of the constraints is named explicitly. MySQL generates a name for each of the other two.
The last two contraints are table constraints. One of them is named explicitly. MySQL generates a name for the other one.
As mentioned, MySQL generates a name for any
CHECK
constraint specified without one. To
see the names generated for the preceding table definition, use
SHOW CREATE TABLE
:
- *************************** 1. row ***************************
- Table: t1
The SQL standard specifies that all types of constraints
(primary key, unique index, foreign key, check) belong to the
same namespace. In MySQL, each constraint type has its own
namespace per schema (database). Consequently,
CHECK
constraint names must be unique per
schema; no two tables in the same schema can share a
CHECK
constraint name. (Exception: A
TEMPORARY
table hides a
non-TEMPORARY
table of the same name, so it
can have the same CHECK
constraint names as
well.)
Beginning generated constraint names with the table name helps ensure schema uniqueness because table names also must be unique within the schema.
CHECK
condition expressions must adhere to
the following rules. An error occurs if an expression contains
disallowed constructs.
Nongenerated and generated columns are permitted, except columns with the
AUTO_INCREMENT
attribute and columns in other tables.Literals, deterministic built-in functions, and operators are permitted. A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user. Examples of functions that are nondeterministic and fail this definition:
CONNECTION_ID()
,CURRENT_USER()
,NOW()
.Stored functions and user-defined functions are not permitted.
Stored procedure and function parameters are not permitted.
Variables (system variables, user-defined variables, and stored program local variables) are not permitted.
Subqueries are not permitted.
Foreign key referential actions (ON UPDATE
,
ON DELETE
) are prohibited on columns used in
CHECK
constraints. Likewise,
CHECK
constraints are prohibited on columns
used in foreign key referential actions.
CHECK
constraints are evaluated for
INSERT
,
UPDATE
,
REPLACE
,
LOAD DATA
, and
LOAD XML
statements and an error
occurs if a constraint evaluates to FALSE
. If
an error occurs, handling of changes already applied differs for
transactional and nontransactional storage engines, and also
depends on whether strict SQL mode is in effect, as described in
Strict SQL Mode.
CHECK
constraints are evaluated for
INSERT IGNORE
,
UPDATE IGNORE
,
LOAD DATA ...
IGNORE
, and
LOAD XML ...
IGNORE
statements and a warning occurs if a constraint
evaluates to FALSE
. The insert or update for
any offending row is skipped.
If the constraint expression 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 type conversion fails or results in a loss of precision, an error occurs.
Constraint expression evaluation uses the SQL mode in effect at evaluation time. If any component of the expression 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.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-create-table-check-constraints.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.