我到处都看过这个错误,并阅读了大量可能的解决方案,但我仍然无法弄清楚我的脚本有什么问题。
在服务器
中执行SQL脚本错误:错误1215:无法添加外键约束
SQL代码:
-- -----------------------------------------------------
-- Table `mydb`.`MARINE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`MARINE` (
`EDIPI` DECIMAL(10) NOT NULL,
`LastName` VARCHAR(25) NOT NULL,
`FirstName` VARCHAR(25) NOT NULL,
`MiddleInitial` CHAR(1) NULL,
`Rank` VARCHAR(6) NOT NULL,
`MOS` DECIMAL(4) NOT NULL,
`BATTLE_SPACE_Company` CHAR(1) NOT NULL,
`BATTLE_SPACE_Section/Platoon` VARCHAR(2) NOT NULL,
PRIMARY KEY (`EDIPI`),
INDEX `fk_MARINE_BATTLE_SPACE1_idx` (`BATTLE_SPACE_Company` ASC, `BATTLE_SPACE_Section/Platoon` ASC),
CONSTRAINT `fk_MARINE_BATTLE_SPACE1`
FOREIGN KEY (`BATTLE_SPACE_Company` , `BATTLE_SPACE_Section/Platoon`)
REFERENCES `mydb`.`BATTLE_SPACE` (`Company` , `Section/Platoon`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL脚本执行完成:语句:7成功,1失败
以最终形式获取视图定义。 没什么可取的
CREATE TABLE `battle_space` (
`Company` char(1) NOT NULL,
`Location` varchar(20) NOT NULL,
`Section/Platoon` varchar(2) NOT NULL,
PRIMARY KEY (`Company`,`Location`,`Section/Platoon`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
答案 0 :(得分:0)
有几个问题。
username@mydb[temp]>CREATE TABLE battle_space (
Company char(1) NOT NULL,
Location varchar(20) NOT NULL,
Section varchar(2) NOT NULL,
PRIMARY KEY (Company,Location,Section)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
username@mydb[temp]>CREATE TABLE IF NOT EXISTS MARINE (
EDIPI DECIMAL(10) NOT NULL,
LastName VARCHAR(25) NOT NULL,
FirstName VARCHAR(25) NOT NULL,
MiddleInitial CHAR(1) NULL,
Rank VARCHAR(6) NOT NULL,
MOS DECIMAL(4) NOT NULL,
BATTLE_SPACE_Company CHAR(1) NOT NULL,
BATTLE_SPACE_Section VARCHAR(2) NOT NULL,
Location VARCHAR(20) NOT NULL,
PRIMARY KEY (EDIPI),
INDEX fk_MARINE_BATTLE_SPACE1_idx (BATTLE_SPACE_Company ASC , BATTLE_SPACE_Section ASC , Location),
CONSTRAINT fk_MARINE_BATTLE_SPACE1 FOREIGN KEY (BATTLE_SPACE_Company , Location , BATTLE_SPACE_Section)
REFERENCES battle_space (Company , Location , Section)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
Query OK, 0 rows affected (0.02 sec)