外键约束不会在MySQL表上创建

时间:2013-02-25 23:51:15

标签: mysql foreign-keys

好的,我的两张表有冲突:

CREATE TABLE visit_physician (
   visit_id INT(11) UNSIGNED NOT NULL,
   physician_id INT(11) UNSIGNED NOT NULL,
   physician_role INT(11) UNSIGNED NOT NULL,
   PRIMARY KEY(visit_id, physician_id)
);

CREATE TABLE physician_role (
   id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT KEY,
   name VARCHAR(50) NOT NULL,
   UNIQUE(name)
);

ALTER TABLE visit_physician
ADD CONSTRAINT FK_roleid
FOREIGN KEY (physician_role) REFERENCES physician_role(id)
ON UPDATE CASCADE
ON DELETE CASCADE;

physician_role和id是相同的数据类型和大小以及所有内容..那么为什么不创建?我的其他外键没有问题..

2 个答案:

答案 0 :(得分:2)

我在MySQL 5.5.29上测试了你的语句,它们运行正常。请注意,两个表都必须使用InnoDB存储引擎来支持外键。如果它们不是,MySQL将解析但忽略这些约束的声明。

在我的实例上,默认存储引擎是InnoDB。您的CREATE TABLE语句不指定存储引擎,因此它们将使用默认值。检查环境中的默认存储引擎:

mysql> show global variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+

使用SHOW TABLE STATUS LIKE '%physician%'检查表格使用的存储引擎。查看输出的Engine列:

+-----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| Name            | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation         | Checksum | Create_options | Comment |
+-----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| physician_role  | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        16384 |         0 |              1 | 2013-02-25 15:56:26 | NULL        | NULL       | latin1_swedish_ci |     NULL |                |         |
| visit_physician | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |        16384 |         0 |           NULL | 2013-02-25 15:56:26 | NULL        | NULL       | latin1_swedish_ci |     NULL |                |         |
+-----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+

如果引擎是MyISAM或其他存储引擎,则可能无法创建约束。

如果要将存储引擎更改为InnoDB,可以执行以下操作:

mysql> ALTER TABLE visit_physician ENGINE=InnODB;
mysql> ALTER TABLE physician_role ENGINE=InnODB;

然后您应该能够重新执行约束的创建。但是,如果表格中的数据不满足约束条件,则可能会导致问题。

但在进行任何此类更改之前,我建议您测试您的应用程序是否仍适用于不同的存储引擎。它很可能会正常工作,但测试总是一个好主意。


尝试添加外键时会发生什么?如果它出现如下错误:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint 
fails (`test`.`#sql-c67_52`, CONSTRAINT `FK_roleid` FOREIGN KEY (`physician_role`) 
REFERENCES `physician_role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

然后你可以在表中已经有一些不满足约束的值。

关于存储引擎和数据类型的其他可能性也可能导致错误,但我们已经排除了这一点,因为您说这些表都是InnoDB并且数据类型看起来是相同的,并且它在我测试时有效。

答案 1 :(得分:0)

我认为它可能与一个表上的两个外键约束有关,但是我在创建表的WHILE中添加了它们,所以它读了这个:

CREATE TABLE visit_physician (
   visit_id INT(11) UNSIGNED NOT NULL,
   physician_id INT(11) UNSIGNED NOT NULL,
   physician_role INT(11) UNSIGNED NOT NULL,
   PRIMARY KEY(visit_id, physician_id),
   CONSTRAINT FOREIGN KEY (physician_id) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
   CONSTRAINT FOREIGN KEY (physician_role) REFERENCES physician_role (id) ON DELETE CASCADE ON UPDATE CASCADE
);

以防万一有人遇到我遇到的奇怪问题......