我有這樣的SQL代碼:「外鍵約束的格式不正確」
CREATE TABLE IF NOT EXISTS `deputy`.`Votings` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`VRSetId` INT UNSIGNED NOT NULL,
`LNSetIdOfMeeting` INT UNSIGNED NOT NULL,
`VRSetIdOfMeeting` INT UNSIGNED NOT NULL,
`meetingId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`, `VRSetId`),
CONSTRAINT `fk_Votings_VotingRulesSets1`
FOREIGN KEY (`VRSetId`)
REFERENCES `deputy`.`VRSets` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Votings_Meetings1`
FOREIGN KEY (`LNSetIdOfMeeting` , `VRSetIdOfMeeting` , `meetingId`)
REFERENCES `deputy`.`Meetings` (`LNSetId` , `VRSetId` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
有由兩個領域的主鍵:「ID」和「VRSetId」; 「VRSetId」也是外鍵,因爲「VRSets」表與「Votings」識別1對1關係。 「Meetings」表中還有第二個外鍵,除了「id」字段之外,它本身具有使用兩個外鍵創建的主鍵......「Meetings」表與「Votings」具有一對多關係,因此「Votings」最終得到了許多密鑰......這很好,因爲我用它們以許多不同的方式獲取我的數據。問題是,既然我已經添加了自動incremenent所有「ID」領域 - 我得到這樣的錯誤:
ERROR: Error 1005: Can't create table `deputy`.`Votings` (errno: 150 "Foreign key constraint is incorrectly formed")
沒有與類似格式化鍵這個「會議」表,它的正常工作:
CREATE TABLE IF NOT EXISTS `deputy`.`Meetings` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`LNSetId` INT UNSIGNED NOT NULL,
`VRSetId` INT UNSIGNED NOT NULL,
`LNSetIdOfCSet` INT UNSIGNED NOT NULL,
`CSetId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`, `LNSetId`, `VRSetId`),
CONSTRAINT `fk_Meetings_LegalNumbersSets1`
FOREIGN KEY (`LNSetId`)
REFERENCES `deputy`.`LNSets` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Meetings_VotingRulesSets1`
FOREIGN KEY (`VRSetId`)
REFERENCES `deputy`.`VRSets` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Meetings_ConfigurationSets1`
FOREIGN KEY (`LNSetIdOfCSet` , `CSetId`)
REFERENCES `deputy`.`CSets` (`LNSetId` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
以及與「分組表決」最後的表(我的所有表中刪除數據字段,使它看起來更清晰):之前的一些細微的變化
CREATE TABLE IF NOT EXISTS `deputy`.`VRSets` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
數據庫是工作的罰款我做了現在我不知道發生了什麼......整個事情是由MySQL Workbench自動生成的,我正在MariaDB上運行腳本。
現場工作,你可以添加'創建要引用的表TABLE'語法?如果你只使用*一個外鍵約束,代碼是否工作? – BurninLeo 2014-09-25 11:30:40
請將upvote的答案如果有幫助:) – th3an0maly 2015-06-11 10:30:33