外键问题,错误代码1452

时间:2013-12-11 15:45:20

标签: mysql sql foreign-keys primary-key

我今天两次碰到了我的外键问题,对于一些我一直收到错误代码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;

在我完成此操作后,我尝试将来自Homeworld的foeign键在character_中添加到world_type中的Name_,这是我得到的消息。

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_`))

这张表也是如此,但结构有点不同,我的想法是我有一个复合键。在需求表中:

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到item中的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');

1 个答案:

答案 0 :(得分:0)

第一部分: 通过添加外键:

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

您告诉表格字符_HomeWorld中的值将始终存在于world_type.Name_中。

但是在插入world_type之前,你试图将它们插入character_中。这会马上给出错误。

更改顺序,它仍然会出错,因为您在Homeworld中仅使用了world_type.name_的部分内容。你可能应该使用一个ID代替。将一个ID放在world_type中,然后在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 );

在你的world_type列表中不存在Homeworld = Archivist中的一个字符。

然后在下面你有:

你在表格要求中说:

`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,但它正在创建和插入而没有错误。