今天兩次我已經碰到了一個問題,我的外鍵,以及一些我不斷收到錯誤代碼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');
我無法複製您的錯誤。當您嘗試添加這些外鍵時,表中是否有數據?如果你在talent.skillName中不存在的requierments.sname中有某些東西,那麼你應該得到那個錯誤。 –
@FilipeSilva我已編輯帖子,所以你可以看到數據,我已經輸入,並且我已經改變了值,所以他們匹配的外鍵和主鍵 – Santelices
我猜他是的,一個人物與Homeworld = dark_heresy但world_type表中沒有匹配的條目。 – OTTA