mysql - 无法在 MySQL 中创建外键约束

标签 mysql phpmyadmin foreign-keys

我在 Ubuntu 中使用 phpMyAdmin 3.5.8.1deb1 创建了我的 MySQL 数据库。

不是我所有的表都是 InnoDB,我不能添加外键,这是一个例子:

ALTER TABLE element ADD CONSTRAINT FK_element_id_user FOREIGN KEY (id_user) REFERENCES user(id) ON DELETE SET NULL ON UPDATE CASCADE;

当我运行这个脚本时,我得到这个错误:

#1005 - Can't create table 'tpw.#sql-4d8_e2' (errno: 150) (Details...)

当我点击详细信息时,我得到了这个:

InnoDB Documentation Supports transactions, row-level locking, and foreign keys

我尝试在关系 View 中手动添加外键

最佳答案

这里可能有几件事。以下是一些需要注意的事项:

  • 表间各字段的数据类型是否匹配?
  • 两个表都使用相同的 MySQL 引擎吗?

Here是帮助您进一步调试此问题的好资源。

摘自上面链接的资源:

1) The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same.

2) One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field.

3) The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this.

4) One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.

5) You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL. You can fix this by either changing your cascade or setting the field to allow NULL values.

6) Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns.

7) You have a default value (ie default=0) on your foreign key column.

8) One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint.

9) You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship.

10) The name of your foreign key exceeds the max length of 64 chars.

关于mysql - 无法在 MySQL 中创建外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20585592/

相关文章:

MySQL/PHPMyAdmin - 使用 512MB RAM 导入 15,000 行

mysql - 外键已接受但不起作用

sql - 从外键引用获取结果

php - 使用嵌套 AJAX 函数将新数据插入 MYSQL

Mysql 查询涉及多个表的 group by 和 order by

mysql - 使 Redshift 的 DATEDIFF 表现得像 MySQL 的 TIMESTAMPDIFF

php - MySQL/phpmyadmin 无法导入 CSV

php - 使用终端在MySQL中创建的数据库与PhpMyAdmin中显示的数据库是否不同?

ruby-on-rails - Rails 中的参照完整性

php遍历数组中的对象数组