- 最后登录
- 2018-9-14
- 在线时间
- 47 小时
- 威望
- 205
- 金钱
- 2327
- 注册时间
- 2011-10-13
- 阅读权限
- 150
- 帖子
- 90
- 精华
- 0
- 积分
- 205
- UID
- 26
|
3#
发表于 2016-9-9 13:10:20
本帖最后由 biotwang 于 2016-9-9 13:18 编辑
查看报错:- mysql> show engine innodb status;
复制代码 查看到报错原因分析:- ------------------------
- LATEST FOREIGN KEY ERROR
- ------------------------
- 2016-09-09 12:06:19 0x7f7788b11700 Error in foreign key constraint of table dbdao_po/degree_salary:
- foreign key(name) references degree_employee(name)):
- 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.
- Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
复制代码 感觉分析不是很合理,但是可能和索引有关,查文档:
http://dev.mysql.com/doc/refman/ ... ey-constraints.html
发现以下说明:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
因此重新清理测试表并修改语句,测试如下:- mysql> create table degree_employee(id int not null auto_increment primary key, name varchar(10), index (name));
- Query OK, 0 rows affected (0.06 sec)
- mysql> create table degree_salary(sa_id int auto_increment primary key, name varchar(10), foreign key(name) references degree_employee(name));
- Query OK, 0 rows affected (0.04 sec)
复制代码 问题解决。
总结:
说明MySQL外键被参考列必须为索引列。
|
|