Rechercher dans le manuel MySQL
15.6.1.5 InnoDB and FOREIGN KEY Constraints
How the InnoDB
storage engine handles foreign
key constraints is described under the following topics in this
section:
For foreign key usage information and examples, see Section 13.1.20.6, “Using FOREIGN KEY Constraints”.
Foreign Key Definitions
Foreign key definitions for InnoDB
tables are
subject to the following conditions:
InnoDB
permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.InnoDB
does not currently support foreign keys for tables with user-defined partitioning. This means that no user-partitionedInnoDB
table may contain foreign key references or columns referenced by foreign keys.InnoDB
allows a foreign key constraint to reference a nonunique key. This is anInnoDB
extension to standard SQL.
Referential actions for foreign keys of
InnoDB
tables are subject to the following
conditions:
While
SET DEFAULT
is allowed by the MySQL Server, it is rejected as invalid byInnoDB
.CREATE TABLE
andALTER TABLE
statements using this clause are not allowed for InnoDB tables.If there are several rows in the parent table that have the same referenced key value,
InnoDB
acts in foreign key checks as if the other parent rows with the same key value do not exist. For example, if you have defined aRESTRICT
type constraint, and there is a child row with several parent rows,InnoDB
does not permit the deletion of any of those parent rows.InnoDB
performs cascading operations through a depth-first algorithm, based on records in the indexes corresponding to the foreign key constraints.If
ON UPDATE CASCADE
orON UPDATE SET NULL
recurses to update the same table it has previously updated during the cascade, it acts likeRESTRICT
. This means that you cannot use self-referentialON UPDATE CASCADE
orON UPDATE SET NULL
operations. This is to prevent infinite loops resulting from cascaded updates. A self-referentialON DELETE SET NULL
, on the other hand, is possible, as is a self-referentialON DELETE CASCADE
. Cascading operations may not be nested more than 15 levels deep.Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows,
InnoDB
checksUNIQUE
andFOREIGN KEY
constraints row-by-row. When performing foreign key checks,InnoDB
sets shared row-level locks on child or parent records it has to look at.InnoDB
checks foreign key constraints immediately; the check is not deferred to transaction commit. According to the SQL standard, the default behavior should be deferred checking. That is, constraints are only checked after the entire SQL statement has been processed. UntilInnoDB
implements deferred constraint checking, some things are impossible, such as deleting a record that refers to itself using a foreign key.
A foreign key constraint on a stored generated column cannot use
ON UPDATE CASCADE
,ON DELETE SET NULL
,ON UPDATE SET NULL
,ON DELETE SET DEFAULT
, orON UPDATE SET DEFAULT
.A foreign key constraint cannot reference a virtual generated column.
Prior to MySQL 8.0, a foreign key constraint cannot reference a secondary index defined on a virtual generated column.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-innodb-foreign-key-constraints.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.