Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code DEF204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

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.


Zoek in de MySQL-handleiding

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-example-foreign-keys.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:en Manuel MySQL : https://dev.mysql.com/

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut