【问题标题】:Cannot add foreign key constraint - data types match无法添加外键约束 - 数据类型匹配
【发布时间】:2013-09-26 13:50:22
【问题描述】:

我正在尝试像这样改变

alter table user_position 
add constraint fk_user_position_user 
foreign key (id_user)
references users(id_user),
add constraint fk_user_position_waypoint 
foreign key (id_waypoint)
references waypoint(id_waypoint);

但我收到此错误:Error Code: 1215. Cannot add foreign key constraint

我检查列数据类型,它们都匹配 -

  • users.id_user int(11)= user_position.id_user int(11)
  • waypoint.id_waypoint int(11)=user_position.id_waypoint int(11)

当我尝试时,我分别在两个更改上都收到此错误

我在 MySql 数据库上运行,命令在 MySql Workbench 中执行

DDL for all tables:

    /*
Created     29. 7. 2013
Modified        30. 7. 2013
Project     
Model       
Company     
Author      
Version     
Database        mySQL 5 
*/


Create table Users (
    id_user Int NOT NULL,
    username Char(20),
    pass Char(20),
 Primary Key (id_user)) ENGINE = MyISAM;

Create table Waypoint (
    id_waypoint Int NOT NULL,
    id_user Int NOT NULL,
    name Char(20),
    description Char(20),
    priority Int,
    type_number Int,
 Primary Key (id_waypoint,id_user)) ENGINE = MyISAM;

Create table Message (
    text Text,
    id_message_type Int NOT NULL,
    shown Bool,
    id_message Int NOT NULL,
    id_user Int NOT NULL,
 Primary Key (id_message_type,id_message,id_user)) ENGINE = MyISAM;

Create table Message_Type (
    id_message_type Int NOT NULL,
    description Char(20),
 Primary Key (id_message_type)) ENGINE = MyISAM;

Create table User_group (
    id_user_group Int NOT NULL,
    description Char(20),
    id_user_rights Char(20) NOT NULL,
 Primary Key (id_user_group,id_user_rights)) ENGINE = MyISAM;

Create table User_Rights (
    id_user_rights Char(20) NOT NULL,
    description Char(20),
    kod Char(20),
 Primary Key (id_user_rights)) ENGINE = MyISAM;

Create table Permisions (
    id_permision Char(20) NOT NULL,
    description Char(20),
 Primary Key (id_permision)) ENGINE = MyISAM;

Create table UserRightPermisions (
    id_user_rights Char(20) NOT NULL,
    id_permision Char(20) NOT NULL,
 Primary Key (id_permision)) ENGINE = MyISAM;

Create table User_Set_Of_Groups (
    id_user_group Int NOT NULL,
    id_user_rights Char(20) NOT NULL,
    id_user Int NOT NULL,
    is_main Bool,
 Primary Key (id_user_group,id_user_rights,id_user)) ENGINE = MyISAM;


Alter table Waypoint add Foreign Key (id_user) references Users (id_user) on delete  restrict on update  restrict;
Alter table Message add Foreign Key (id_user) references Users (id_user) on delete  restrict on update  restrict;
Alter table User_Set_Of_Groups add Foreign Key (id_user) references Users (id_user) on delete  restrict on update  restrict;
Alter table Message add Foreign Key (id_message_type) references Message_Type (id_message_type) on delete  restrict on update  restrict;
Alter table User_Set_Of_Groups add Foreign Key (id_user_group,id_user_rights) references User_group (id_user_group,id_user_rights) on delete  restrict on update  restrict;
Alter table User_group add Foreign Key (id_user_rights) references User_Rights (id_user_rights) on delete  restrict on update  restrict;
Alter table UserRightPermisions add Foreign Key (id_user_rights) references User_Rights (id_user_rights) on delete  restrict on update  restrict;
Alter table UserRightPermisions add Foreign Key (id_permision) references Permisions (id_permision) on delete  restrict on update  restrict;

【问题讨论】:

  • 您应该发布所有相关表的实际表定义。否则 cmets 基本上会是“你确定吗?仔细检查”
  • 也许您的 user_position 表在参考表中没有的字段中有值。
  • 现有数据是否违反约束?

标签: mysql sql constraints alter-table


【解决方案1】:
  • 外键必须引用主键(或唯一键)
  • 列数据类型必须完全相同(检查 NOT NULL、UNSIGNED 等)

【讨论】:

猜你喜欢
  • 2020-09-26
  • 2020-09-12
  • 2017-07-16
  • 2013-03-10
  • 2021-11-05
  • 2018-02-13
  • 2018-05-16
相关资源
最近更新 更多