嗨,我在mysql phpMyAdmin中创建表时出现1022错误。 错误发生在最后一个表“usercoment”:
解决方案编辑:好的,我已经有一个名为IdMovie
的约束键,所以我重命名并且工作正常。
谢谢你们。
以下是表格:
CREATE TABLE IF NOT EXISTS `WhichMovie`.`UserComent` (
`IDComent` INT NOT NULL,
`IDUser` INT NOT NULL,
`IDMovie` INT NOT NULL,
`date` TIMESTAMP NOT NULL,
PRIMARY KEY (`IDComent`, `IDUser`, `IDMovie`, `date`),
INDEX `idUser_idx` (`IDUser` ASC),
INDEX `idMovie_idx` (`IDMovie` ASC),
CONSTRAINT `IdComent`
FOREIGN KEY (`IDComent`)
REFERENCES `WhichMovie`.`Coment` (`idComent`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `IiUser`
FOREIGN KEY (`IDUser`)
REFERENCES `WhichMovie`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `IdMovie`
FOREIGN KEY (`IDMovie`)
REFERENCES `WhichMovie`.`Movie` (`idMovie`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WhichMovie`.`Movie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `WhichMovie`.`Movie` (
`idMovie` INT NOT NULL AUTO_INCREMENT,
`Ttle` VARCHAR(45) NULL,
`Year` INT NULL,
`Country` VARCHAR(45) NULL,
`Poster` BLOB NULL,
`Rating` DECIMAL NULL,
PRIMARY KEY (`idMovie`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WhichMovie`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `WhichMovie`.`User` (
`idUser` INT NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(45) NOT NULL,
`Email` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NULL,
`Pass` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idUser`, `Username`, `Email`),
UNIQUE INDEX `idUser_UNIQUE` (`idUser` ASC),
UNIQUE INDEX `Email_UNIQUE` (`Email` ASC),
UNIQUE INDEX `Username_UNIQUE` (`Username` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WhichMovie`.`Coment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `WhichMovie`.`Coment` (
`idComent` INT NOT NULL AUTO_INCREMENT,
`NameComent` VARCHAR(45) NULL,
PRIMARY KEY (`idComent`),
UNIQUE INDEX `idComent_UNIQUE` (`idComent` ASC))
ENGINE = InnoDB;
有人能看到吗?
答案 0 :(得分:7)
根据:MySQL error 1022 when creating table您不能在整个模型上复制外键约束名称。
答案 1 :(得分:3)
只需重命名约束名称不同,您的问题就解决了......
示例:
CONSTRAINT `IdComent_CONSTRAINT`
FOREIGN KEY (`IDComent`)
现在你有一个与THE FOREIGN KEY ONE不同的CONSTRAINT'NAME
答案 2 :(得分:0)
您使用的是哪个版本的MySQL?
在我的本地主机上使用MySQL 5.6.12这段代码(与你的相同,但最后移动了UserComent的创建)可以正常工作:
-- -----------------------------------------------------
-- Table `WhichMovie`.`Movie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `WhichMovie`.`Movie` (
`idMovie` INT NOT NULL AUTO_INCREMENT,
`Ttle` VARCHAR(45) NULL,
`Year` INT NULL,
`Country` VARCHAR(45) NULL,
`Poster` BLOB NULL,
`Rating` DECIMAL NULL,
PRIMARY KEY (`idMovie`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WhichMovie`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `WhichMovie`.`User` (
`idUser` INT NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(45) NOT NULL,
`Email` VARCHAR(45) NOT NULL,
`Country` VARCHAR(45) NULL,
`Pass` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idUser`, `Username`, `Email`),
UNIQUE INDEX `idUser_UNIQUE` (`idUser` ASC),
UNIQUE INDEX `Email_UNIQUE` (`Email` ASC),
UNIQUE INDEX `Username_UNIQUE` (`Username` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WhichMovie`.`Coment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `WhichMovie`.`Coment` (
`idComent` INT NOT NULL AUTO_INCREMENT,
`NameComent` VARCHAR(45) NULL,
PRIMARY KEY (`idComent`),
UNIQUE INDEX `idComent_UNIQUE` (`idComent` ASC))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `WhichMovie`.`UserComent` (
`IDComent` INT NOT NULL,
`IDUser` INT NOT NULL,
`IDMovie` INT NOT NULL,
`date` TIMESTAMP NOT NULL,
PRIMARY KEY (`IDComent`, `IDUser`, `IDMovie`, `date`),
INDEX `idUser_idx` (`IDUser` ASC),
INDEX `idMovie_idx` (`IDMovie` ASC),
CONSTRAINT `IdComent`
FOREIGN KEY (`IDComent`)
REFERENCES `WhichMovie`.`Coment` (`idComent`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `IiUser`
FOREIGN KEY (`IDUser`)
REFERENCES `WhichMovie`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `IdMovie`
FOREIGN KEY (`IDMovie`)
REFERENCES `WhichMovie`.`Movie` (`idMovie`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
创建表,不会发生错误。
在5.6.15表上也创建没有问题。在phpmyadmin设计师的视图下方: