1

我正在尝试像这样改变

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;
4

1 回答 1

0
  • 外键必须引用主(或唯一)键
  • 列数据类型必须完全相同(检查 NOT NULL、UNSIGNED 等)
于 2013-09-26T14:03:27.883 回答