In MySQL, 在MySQL中,InnoDB tables support checking of foreign key constraints. InnoDB表支持检查外键约束。See Chapter 15, The InnoDB Storage Engine, and Section 1.7.2.3, “FOREIGN KEY Constraint Differences”.请参阅第15章“InnoDB存储引擎”和第1.7.2.3节,“FOREIGN KEY约束差异”。
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. tbl_name(col_name)InnoDB以外的存储引擎,在定义列时,可以使用REFERENCES tbl_name(col_name)子句,该子句没有实际效果,仅作为您当前定义的列旨在引用另一个表中的列的备忘录或注释。It is extremely important to realize when using this syntax that:在使用此语法时,必须认识到:
MySQL does not perform any sort of check to make sure that MySQL不执行任何类型的检查来确保col_name actually exists in tbl_name (or even that tbl_name itself exists).col_name确实存在于tbl_name中(甚至tbl_name本身也存在)。
MySQL does not perform any sort of action on MySQL不会对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. tbl_name执行任何类型的操作,例如删除行以响应对您正在定义的表中的行所执行的操作;换句话说,此语法不会引发任何ON DELETE或ON UPDATE行为。(Although you can write an (尽管可以将ON DELETE or ON UPDATE clause as part of the REFERENCES clause, it is also ignored.)ON DELETE或ON UPDATE子句作为REFERENCES子句的一部分编写,但它也会被忽略。)
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:您可以使用这样创建的列作为联接列,如下所示:
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
INSERT INTO person VALUES (NULL, 'Antonio Paz');
SELECT @last := LAST_INSERT_ID();
INSERT INTO shirt VALUES
(NULL, 'polo', 'blue', @last),
(NULL, 'dress', 'white', @last),
(NULL, 't-shirt', 'blue', @last);
INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');
SELECT @last := LAST_INSERT_ID();
INSERT INTO shirt VALUES
(NULL, 'dress', 'orange', @last),
(NULL, 'polo', 'red', @last),
(NULL, 'dress', 'blue', @last),
(NULL, 't-shirt', 'white', @last);
SELECT * FROM person;
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
SELECT * FROM shirt;
+----+---------+--------+-------+
| 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 |
+----+---------+--------+-------+
SELECT s.* FROM person p INNER JOIN shirt s
ON s.owner = p.id
WHERE p.name LIKE 'Lilliana%'
AND s.color <> 'white';
+----+-------+--------+-------+
| 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:REFERENCES子句不会显示在SHOW CREATE TABLE或DESCRIBE的输出中:
SHOW CREATE TABLE shirt\G
*************************** 1. row ***************************
Table: shirt
Create Table: CREATE TABLE `shirt` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`style` enum('t-shirt','polo','dress') NOT NULL,
`color` enum('red','blue','orange','white','black') NOT NULL,
`owner` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`)
) 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.REFERENCES作为列定义中的注释或“提醒”可用于MyISAM表。