MySQL在创建表时悄悄忽略了"references"关键字

3
mysql> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> use test;
Database changed
mysql> create table one (id int not null primary key);
Query OK, 0 rows affected (0.03 sec)

mysql> -- here is the problem
mysql> create table two (oneid int not null references one(id));
Query OK, 0 rows affected (0.02 sec)

mysql> -- here are the first signs of issues!!!!
mysql> show create table two;
+-------+----------------------------------------------------------------------------------------+
| Table | Create Table                                                                           |
+-------+----------------------------------------------------------------------------------------+
| two   | CREATE TABLE `two` (
  `oneid` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- here is the issue: an insert with no reference 
mysql> insert into two values (-12);
Query OK, 1 row affected (0.01 sec)

mysql> select * from two;
+-------+
| oneid |
+-------+
|   -12 |
+-------+
1 row in set (0.00 sec)
mysql> -- if you want to know:
mysql> SHOW Variables WHERE Variable_name='foreign_key_checks';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| foreign_key_checks | ON    |
+--------------------+-------+
1 row in set (0.00 sec)

经过我的研究,这里唯一的问题是:为什么MySql没有因为表2的无效语法而拒绝创建,并在没有外键引用的情况下悄悄地创建了该表?
只为完整性起见,这里是MySql的正确语法。
mysql> create table three(oneid int not null, CONSTRAINT whatEverName FOREIGN KEY (oneid) REFERENCES one(id));
Query OK, 0 rows affected (0.04 sec)
1个回答

2

MySQL解析但忽略内联引用规范(如SQL标准所定义),其中引用被定义为列规范的一部分。只有在指定作为单独的FOREIGN KEY规范的一部分时,MySQL才接受REFERENCES子句。

您可以在这里自行阅读更多内容...


1
您所提到的页面是我澄清自己的方式。我仍然不理解的部分是为什么它“解析但忽略内联REFERENCES规范”?它还解析但忽略了什么? - Saic Siquot
"如SQL标准中所定义的"......也许你应该查看一下SQL标准,我相信他们在文档中加入这个是有原因的。 - BK435

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接