2013-09-26 30 views
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)= INT user_position.id_user(11)
  • waypoint.id_waypoint INT(11)= INT user_position.id_waypoint(11)

I'm讓雙方改變了這種錯誤separatelly當我嘗試它

我MySQL數據庫上運行,在MySQL工作臺命令執行

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; 
+0

您應該發佈所有涉及表的實際表定義。否則,評論基本上會是「你確定嗎?仔細檢查」 –

+0

也許你的user_position表在那些不在參考表中的字段中有值。 –

+0

現有數據是否違反約束條件? – Tom

回答

0
  • 外鍵必須引用到主要(或唯一)鍵
  • 列數據類型必須完全相同(檢查NOT NULL,UNSIGNED等)
+0

MySQL文檔說「InnoDB允許外鍵約束引用非唯一鍵,這是對標準SQL的InnoDB擴展。」 http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html – Tom

+0

Foregin鍵可以爲null –