mysql - MySQL CREATE TABLE 语句的外键错误(err : 150)

标签 mysql sql sql-server mysql-error-1005

我觉得我已经在一对非常简单的创建表语句上尝试了所有可能的方法。

类型匹配,我尝试使用 ENGINE = InnoDB 等,但我很困惑为什么会出现外键错误。

我已经离开 SQL 一段时间了,所以这可能很简单。

mysql> CREATE TABLE foo_ent(yyy_no VARCHAR(80),
    -> zoo VARCHAR(80),
    -> PRIMARY KEY (yyy_no));
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE cat_ent(some_item INTEGER,
    -> yyy_no VARCHAR(80),
    -> apple DECIMAL(6,2),
    -> PRIMARY KEY (some_item),
    -> FOREIGN KEY (yyy_no) REFERENCES foo_ent);
ERROR 1005 (HY000): Can't create table 'test.cat_ent' (errno: 15
0)

对于糟糕的变量名感到抱歉,可以安全地覆盖公司的东西。

最佳答案

您没有引用字段,仅引用了表格,这是不正确的。

...
foreign key (yyy_no) references foo_ent(yyy_no)

而且根据你的错误号,MySQL文档中也有说明;

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

关于mysql - MySQL CREATE TABLE 语句的外键错误(err : 150),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5357936/

相关文章:

PHP+MySQL UPDATE 查询(多列)不起作用

asp.net - 在asp.net中将数据从excel表导入到mysql数据库

mysql - 比较行并选择最大值

sql - Postgresql:使用窗口函数从日期范围中选择最大值

sql-server - 未返回输出参数

mysql - ORDER BY datetime 使查询非常慢

php - mysql查询与2个表的一些计算

mysql - 如何检查 MYSQL_HISTIGNORE 中的当前字符串?

sql - 将 1 个随机记录从一个表传输到另一个表

sql - 死锁问题