Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

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 tbl_name(col_name) 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:

  • MySQL does not perform any sort of CHECK to make sure that col_name actually exists in tbl_name (or even that tbl_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 no ON DELETE or ON UPDATE behavior whatsoever. (Although you can write an ON DELETE or ON UPDATE clause as part of the REFERENCES 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:

  1. CREATE TABLE person (
  2.     name CHAR(60) NOT NULL,
  3.     PRIMARY KEY (id)
  4. );
  5.  
  6. CREATE TABLE shirt (
  7.     style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
  8.     color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
  9.     owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
  10.     PRIMARY KEY (id)
  11. );
  12.  
  13. INSERT INTO person VALUES (NULL, 'Antonio Paz');
  14.  
  15.  
  16. (NULL, 'polo', 'blue', @last),
  17. (NULL, 'dress', 'white', @last),
  18. (NULL, 't-shirt', 'blue', @last);
  19.  
  20. INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');
  21.  
  22.  
  23. (NULL, 'dress', 'orange', @last),
  24. (NULL, 'polo', 'red', @last),
  25. (NULL, 'dress', 'blue', @last),
  26. (NULL, 't-shirt', 'white', @last);
  27.  
  28. SELECT * FROM person;
  29. +----+---------------------+
  30. | id | name                |
  31. +----+---------------------+
  32. |  1 | Antonio Paz         |
  33. |  2 | Lilliana Angelovska |
  34. +----+---------------------+
  35.  
  36. SELECT * FROM shirt;
  37. +----+---------+--------+-------+
  38. | id | style   | color  | owner |
  39. +----+---------+--------+-------+
  40. |  1 | polo    | blue   |     1 |
  41. |  2 | dress   | white  |     1 |
  42. |  3 | t-shirt | blue   |     1 |
  43. |  4 | dress   | orange |     2 |
  44. |  5 | polo    | red    |     2 |
  45. |  6 | dress   | blue   |     2 |
  46. |  7 | t-shirt | white  |     2 |
  47. +----+---------+--------+-------+
  48.  
  49.  
  50. SELECT s.* FROM person p INNER JOIN shirt s
  51.    ON s.owner = p.id
  52.  WHERE p.name LIKE 'Lilliana%'
  53.    AND s.color <> 'white';
  54.  
  55. +----+-------+--------+-------+
  56. | id | style | color  | owner |
  57. +----+-------+--------+-------+
  58. |  4 | dress | orange |     2 |
  59. |  5 | polo  | red    |     2 |
  60. |  6 | dress | blue   |     2 |
  61. +----+-------+--------+-------+

When used in this fashion, the REFERENCES clause is not displayed in the output of SHOW CREATE TABLE or DESCRIBE:

  1. SHOW CREATE TABLE shirt\G
  2. *************************** 1. row ***************************
  3. Table: shirt
  4. `style` enum('t-shirt','polo','dress') NOT NULL,
  5. `color` enum('red','blue','orange','white','black') NOT NULL,
  6. PRIMARY KEY  (`id`)
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

The use of REFERENCES in this way as a comment or reminder in a column definition works with MyISAM tables.


Rechercher dans le manuel MySQL

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

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

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.

Table des matières Haut