Maison  >  Questions et réponses  >  le corps du texte

数据库 - 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

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

高洛峰高洛峰2743 Il y a quelques jours735

répondre à tous(1)je répondrai

  • 怪我咯

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

    你所用的 todomodel_projects(user,name) 是复合主键或叫联合主键,如果另一个表想做外链键引用这个复合主键,其所用的字段类型必须与主键对应一致,你看看todomodel_tasks.name与todomodel_projects.projects这两个字段的类型是不是不一致,所以导致创建错误。
    另外,关于MySQL主键和外键的一些常识,可以简单看看 http://www.cnblogs.com/web-lover/archive/2012/02/21/2615940.html

    répondre
    0
  • Annulerrépondre