Rechercher dans le manuel MySQL
3.6.6 Using Foreign Keys
In MySQL, InnoDB
tables support checking of
foreign key constraints. See
Chapter 15, The InnoDB Storage Engine, and
Section 1.8.2.3, “Foreign Key Differences”.
A foreign key constraint is not required merely to join two
tables. For storage engines other than
InnoDB
, it is possible when defining a column
to use a REFERENCES
clause, which has no actual effect, and serves only as
a memo or comment to you that the column which you are currently
defining is intended to refer to a column in another
table. It is extremely important to realize when
using this syntax that:
tbl_name
(col_name
)
MySQL does not perform any sort of
CHECK
to make sure thatcol_name
actually exists intbl_name
(or even thattbl_name
itself exists).MySQL does not perform any sort of action on
tbl_name
such as deleting rows in response to actions taken on rows in the table which you are defining; in other words, this syntax induces noON DELETE
orON UPDATE
behavior whatsoever. (Although you can write anON DELETE
orON UPDATE
clause as part of theREFERENCES
clause, it is also ignored.)This syntax creates a column; it does not create any sort of index or key.
You can use a column so created as a join column, as shown here:
- );
- );
- +----+---------------------+
- | id | name |
- +----+---------------------+
- | 1 | Antonio Paz |
- | 2 | Lilliana Angelovska |
- +----+---------------------+
- +----+---------+--------+-------+
- | id | style | color | owner |
- +----+---------+--------+-------+
- | 1 | polo | blue | 1 |
- | 2 | dress | white | 1 |
- | 3 | t-shirt | blue | 1 |
- | 4 | dress | orange | 2 |
- | 5 | polo | red | 2 |
- | 6 | dress | blue | 2 |
- | 7 | t-shirt | white | 2 |
- +----+---------+--------+-------+
- ON s.owner = p.id
- +----+-------+--------+-------+
- | id | style | color | owner |
- +----+-------+--------+-------+
- | 4 | dress | orange | 2 |
- | 5 | polo | red | 2 |
- | 6 | dress | blue | 2 |
- +----+-------+--------+-------+
When used in this fashion, the REFERENCES
clause is not displayed in the output of
SHOW CREATE TABLE
or
DESCRIBE
:
- *************************** 1. row ***************************
- Table: shirt
The use of REFERENCES
in this way as a
comment or “reminder” in a column definition works
with MyISAM
tables.
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-example-foreign-keys.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.