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 that
col_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.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-example-foreign-keys.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.