search

Home  >  Q&A  >  body text

数据库 - Mysql 外键为什么不能多对多啊

如下语句
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

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

高洛峰高洛峰2799 days ago776

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 13:00:22

    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

    reply
    0
  • Cancelreply