No cache version.

Caching disabled. Default setting for this page:enabled (code DEF204)
If the display is too slow, you can disable the user mode to view the cached version.

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.


Find a PHP function

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

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

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.

Contents Haut