嗨,我是在mysql phpMyAdmin中創建表時出現1022錯誤。 錯誤發生在最後一個表「usercoment」上:#1022 - 無法寫入;表中的重複鍵
解決方案編輯:好的,我已經有一個名爲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;
任何人都可以看到它?
需要查看引用的表定義。 – Kermit
您還應該給我們提供UserComent引用的其他表格結構 –
看起來問題不在於結構。我在創建表格和插入數據方面沒有問題。當你插入數據時,你是否100%確定問題不存在?重命名您的whichmovie數據庫,創建空的whichmovie數據庫,並只是在我的答案中導入這些查詢。在創建表時啓動這些查詢時是否真的看到錯誤? –