数据库 - Mysql 外键为什么不能多对多啊
高洛峰
高洛峰 2017-04-17 12:58:22
0
1
769

如下语句
mysql> alter table todomodel_tasks add foreign key(user,projects) references tod
omodel_projects(user,name);
在tododmoel_projects中 user和name 是一个组合,他们合起来是主键。
不是说外键只要对应其他表里的主键就好了吗。
然而会报下列错误
Cannot add foreign key constraint

问问各位大神们这是咋回事呢

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
刘奇

The todomodel_projects(user,name) you are using is a composite primary key or a joint primary key. If another table wants to reference this composite primary key as an external link key, the field type used must be consistent with the primary key. Take a look at todomodel_tasks. Are the types of the two fields name and todomodel_projects.projects inconsistent, causing a creation error?
In addition, for some common sense about MySQL primary keys and foreign keys, you can simply take a look at http://www.cnblogs.com/web-lover/archive/2012/02/21/2615940.html

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template