2013-12-11 77 views
0

今天兩次我已經碰到了一個問題,我的外鍵,以及一些我不斷收到錯誤代碼1452,並且不知道該怎麼做:外鍵的問題,錯誤代碼1452

CREATE TABLE `character_` (
    `Name_` varchar(30) NOT NULL DEFAULT '', 
    `Class` varchar(30) DEFAULT NULL, 
    `Homeworld` varchar(30) DEFAULT NULL, 
    `Rank` char(15) DEFAULT NULL, 
    `Str` int(11) DEFAULT NULL, 
    `WS` int(11) DEFAULT NULL, 
    `BS` int(11) DEFAULT NULL, 
    `Fel` int(11) DEFAULT NULL, 
    `Per` int(11) DEFAULT NULL, 
    `Int_` int(11) DEFAULT NULL, 
    `Agi` int(11) DEFAULT NULL, 
    `WP` int(11) DEFAULT NULL, 
    PRIMARY KEY (`Name_`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 


CREATE TABLE `world_type` (
    `Name_` varchar(30) NOT NULL DEFAULT '', 
    `Skills` varchar(30) DEFAULT NULL, 
    `Bonus` varchar(30) DEFAULT NULL, 
    `Penalty` varchar(30) DEFAULT NULL, 
    `Description` varchar(90) DEFAULT NULL, 
    PRIMARY KEY (`Name_`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

後,我有做到這一點,我嘗試在world_type中添加從Homeworld中的character_到name_的foeign密鑰,這是我得到的消息。

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`dark_heresy`.`#sql-c20_2`, CONSTRAINT `#sql-c20_2_ibfk_1` FOREIGN KEY (`Homeworld`) REFERENCES `world_type` (`Name_`)) 

該表也是如此,但其結構有點不同,我的想法是,我有一個複合鍵。在requierment表:

CREATE TABLE `item` (
    `ID` int(11) NOT NULL DEFAULT '0', 
    `Name_` varchar(30) DEFAULT NULL, 
    `Weight` int(11) DEFAULT NULL, 
    `Value_` int(11) DEFAULT NULL, 
    `Availability` varchar(30) DEFAULT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 


CREATE TABLE `talents` (
    `SkillName` varchar(30) NOT NULL DEFAULT '', 
    `Bonus` varchar(30) DEFAULT NULL, 
    `Description` varchar(70) DEFAULT NULL, 
    `R_Str` int(11) DEFAULT NULL, 
    `R_WS` int(11) DEFAULT NULL, 
    `R_BS` int(11) DEFAULT NULL, 
    `R_Fel` int(11) DEFAULT NULL, 
    `R_Per` int(11) DEFAULT NULL, 
    `R_Int` int(11) DEFAULT NULL, 
    `R_Agi` int(11) DEFAULT NULL, 
    `R_WP` int(11) DEFAULT NULL, 
    `Talent_requiert` varchar(30) DEFAULT NULL, 
    PRIMARY KEY (`SkillName`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 


CREATE TABLE `requierments` (
    `item_ID` int(11) NOT NULL DEFAULT '0', 
    `SName` varchar(30) NOT NULL DEFAULT '', 
    PRIMARY KEY (`item_ID`,`SName`), 
    CONSTRAINT `requierments_ibfk_1` FOREIGN KEY (`item_ID`) REFERENCES `item` (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

由於其在requierments表中所示,從ITEM_ID的外鍵ID的項目已經成功,但是,當我嘗試做:

ALTER TABLE requierments 
ADD FOREIGN KEY (SName) 
REFERENCES talents (SkillName); 

相同的錯誤代碼我得到:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`dark_heresy`.`#sql-c20_2`, CONSTRAINT `#sql-c20_2_ibfk_2` FOREIGN KEY (`SName`) REFERENCES `talents` (`SkillName`)) 

請幫助我開始變得絕望。

在此先感謝

數據輸入:

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Atelus Flex', 'Psyker', 'Void', 'Sanctio nite', 30, 31, 29, 37, 31, 40, 32, 40); 

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Varg Rexxar', 'Assassin', 'Feral', 'Sell steel', 37, 40, 36, 20, 37, 28, 41, 30); 

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Marr', 'Adept', 'Void', 'Archivist', 32, 34, 42, 20, 51, 31, 24, 33); 

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Morrison', 'Tech Priest', 'Void', 'Technographer', 25, 30, 40, 33, 24, 34, 31, 32); 

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Krohn', 'Scum', 'Feral', 'Dreg', 24, 32, 28, 34, 24, 28, 39, 32); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Feral World', 'Tribal Dialect', 'Iron Stomach', 'Primitive', 'Feral Worlders are big, strong and tough'); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Hive World', 'accustomed to crowds', 'caves of steel', 'Hivebound', 'Hivers are fast talking, quick thinking individuals'); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Imperial World', 'Hagiography', 'superior origins', 'blessed ignorance', 'Imperial citizens comes from all sorts of different planets and cultures'); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Void Born', 'shipwise', 'charmed', 'ill omened', 'Void born are weirdly lucky and strong willed'); 


INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
01, 'Las Carbine', 3, 75, 'Common'); 

INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
02, 'Laspistol', 1, 50, 'Common'); 

INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
03, 'Shotgun', 5, 60, 'average'); 

INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
04, 'Heavy Bolter', 40, 2000, 'Very Rare'); 

INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
05, 'Needle pistol', 2, 1250, 'Very Rare'); 

INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
06, 'Chainsword', 6, 275, 'Rare'); 

INSERT INTO Item (ID, Name_, Weight, Value_, Availability) VALUES (
07, 'Power Sword', 4, 2500, 'Very Rare'); 


INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'ambidextrous', 0, 'use either hand equally well', 0, 0, 0, 0, 0, 0, 30, 0, null); 

INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'Frenzy', 0, 'enter psychotic rage to gain combat bonus', 0, 0, 0, 0, 0, 0, 0, 0, null); 

INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'battle rage', 0, 'parry while frenzied', 0, 0, 0, 0, 0, 0, 0, 0, 'Frenzy'); 

INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'Exotic Weapons', 0, 'Player is able to use exotic weapons', 0, 0, 0, 0, 0, 0, 0, 0, 'Basic weapons'); 

INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'Basic weapons', 0, 'Player is able to use Basic weapons', 0, 0, 0, 0, 0, 0, 0, 0, null); 

INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'Heavy weapon', 0, 'Player is able to use heavy weapons', 30, 0, 0, 0, 0, 0, 0, 0, null); 

INSERT INTO Talents (SkillName, Bonus, Description, R_Str, R_WS, R_BS, R_Fel, R_Per, R_Int, R_Agi, R_WP, Talent_requiert) VALUES (
'Power weapon', 0, 'Player is able to use power weapons ', 40, 30, 0, 0, 0, 0, 0, 0, null); 


INSERT INTO Requierments (item_ID, SName) VALUES ( 
01, 'Basic weapons'); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
02, null); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
03, null); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
04, 'Heavy weapon'); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
05, 'Exotic Weapons'); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
06, 'Heavy weapon'); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
07, 'Power weapon'); 


INSERT INTO learned (CName, SName) VALUES (
'Varg Rexxar','ambidextrous'); 

INSERT INTO learned (CName, SName) VALUES (
'Krohn','Frenzy'); 

INSERT INTO learned (CName, SName) VALUES (
'Marr','Basic weapons'); 

INSERT INTO learned (CName, SName) VALUES (
'Atelus Flex','Basic weapons'); 

INSERT INTO learned (CName, SName) VALUES (
'Morrison','Basic weapons'); 
+0

我無法複製您的錯誤。當您嘗試添加這些外鍵時,表中是否有數據?如果你在talent.skillName中不存在的requierments.sname中有某些東西,那麼你應該得到那個錯誤。 –

+0

@FilipeSilva我已編輯帖子,所以你可以看到數據,我已經輸入,並且我已經改變了值,所以他們匹配的外鍵和主鍵 – Santelices

+0

我猜他是的,一個人物與Homeworld = dark_heresy但world_type表中沒有匹配的條目。 – OTTA

回答

0

第一部分: 通過添加外鍵:

ALTER TABLE character_ 
ADD FOREIGN KEY (Homeworld) 
REFERENCES world_type (Name_); 

你說的是表character_在母星值將始終存在於world_type.Name_中。

但是,在插入world_type之前,您正試圖在character_中插入它們。這會馬上發生錯誤。

更改順序,它仍然給出錯誤,因爲您只使用Homeworld中的部分world_type.name_。您應該使用一個ID來代替。在World_type中放入一個ID,然後在Character中引用它。

如果沒有,你應該做的:

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Feral World', 'Tribal Dialect', 'Iron Stomach', 'Primitive', 'Feral Worlders are big, strong and tough'); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Hive World', 'accustomed to crowds', 'caves of steel', 'Hivebound', 'Hivers are fast talking, quick thinking individuals'); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Imperial World', 'Hagiography', 'superior origins', 'blessed ignorance', 'Imperial citizens comes from all sorts of different planets and cultures'); 

INSERT INTO World_type (Name_, skills, bonus, penalty, Description) VALUES (
'Void Born', 'shipwise', 'charmed', 'ill omened', 'Void born are weirdly lucky and strong willed'); 


INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Atelus Flex', 'Psyker', 'Void Born', 'Sanctio nite', 30, 31, 29, 37, 31, 40, 32, 40); 

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Varg Rexxar', 'Assassin', 'Feral World', 'Sell steel', 37, 40, 36, 20, 37, 28, 41, 30); 


INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Morrison', 'Tech Priest', 'Void Born', 'Technographer', 25, 30, 40, 33, 24, 34, 31, 32); 

INSERT INTO character_ (Name_, Class, Homeworld, Rank, Str, WS, BS, Fel, Per, Int_, Agi, WP) VALUES (
'Krohn', 'Scum', 'Feral World', 'Dreg', 24, 32, 28, 34, 24, 28, 39, 32); 

有在那裏與家園= Archivist一個character_不會在你的world_type列表中存在。

那麼下面你有:

你說表requierments:

`SName` varchar(30) NOT NULL DEFAULT '', 

但你這樣做:

INSERT INTO Requierments (item_ID, SName) VALUES ( 
02, null); 

INSERT INTO Requierments (item_ID, SName) VALUES ( 
03, null); 

這也不能發生。

sqlfiddle demo

這不是一個最終的結果。您必須正確放置world_types和Snames,但它會創建並插入而不會出錯。