Keine Cache-Version


Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code DEF204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.

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.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-example-foreign-keys.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut