我有一個關於主鍵或複合鍵的問題。我創建了一個只有兩個屬性並且都是主鍵的表。主鍵列不允許空值
的主鍵的名稱是item_ID̀ and
SName`
這兩個外鍵的另一個主鍵(對於其他兩個表),其中: item_ID --> ID
和SName --> SkillName
ITEM ID SName 1 Basic weapons 2 3 4 Heavy weapon 5 Exotic Weapons 6 Heavy weapon 7 Power weapon
和結果是這樣的:
身份證號碼2和3不需要技能名稱要求。出於這個原因,我已經這樣實現(因爲主鍵不允許爲空):
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');
這是否正確完成?還是有另一種方式來編碼該ID不需要特定的SName?
這裏是在3代表的代碼:
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;
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;
的想法是在表中的要求ITEM_ID --->在項目表,並在人才SNAME ---> SkillName ID。出於某種原因,我能夠從item_ID到ID的外鍵,但不能從SName到技巧名稱:
我已經添加到這些表中的值如下(OBS,在表中我需要設置ID 2和3「」,因爲不需要任何天賦可以選擇它。但我不確定我是否允許使用該方法?
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 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 Requierments (item_ID, SName) VALUES (
01, 'Basic weapons');
INSERT INTO Requierments (item_ID, SName) VALUES (
02, '');
INSERT INTO Requierments (item_ID, SName) VALUES (
03, '');
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');
每個表格只能有一個主鍵。你是說你的PK是一個複合鍵,它包含在'item_ID'和'sname'中? –
那是正確的,或者說這個想法。這裏是代碼: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; 我也試圖做一個forign鍵爲他們兩個分開的表,但有些原因,當我嘗試在SName它給了我一個錯誤代碼1452 – Santelices
因此,有三個表在你的模型?請提供他們的簡短說明。它看起來像'requirements.item_ID'和'requirements.SName'是同一'item'表的外鍵,但是你最後的評論讓我困惑(「外鍵(...)到兩個分離的表」)。 – RandomSeed