查看MySQL创建外键约束失败详细原因的方法 (MySQL学习)
发布于 2008-12-10 12:24 1 阅读:20,477 评论:1 标签: 外键

      首先,目前MySQL的外键约束只适用于InnoDB数据表。

      创建外键约束时,如果把握不好,将无法创建,比如提示你:

以下是引用片段:

ERROR 1005 (HY000): Can't create table './dbname/tablename.frm' (errno: 150)

      为了查看更详细的信息,mysql提供了一个命令:

以下是引用片段:

show innodb status;

或者:

show engine innodb status;

      其中有一项“LATEST FOREIGN KEY ERROR”显示了最近的一次外键约束出错的详细信息,如:

以下是引用片段:

LATEST FOREIGN KEY ERROR
------------------------
081210 11:57:40 Error in foreign key constraint of table dbname/tablename:
foreign key (`gid`, `uid`) references `table2`(`gid`, `uid`)
) ENGINE = InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

     提问:不知道mysql其它的错误,有没有方法得到这样的提示?

展开全文  
收起全文