2014-05-13 44 views
1

嗨,我是在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; 

任何人都可以看到它?

+0

需要查看引用的表定義。 – Kermit

+0

您還應該給我們提供UserComent引用的其他表格結構 –

+0

看起來問題不在於結構。我在創建表格和插入數據方面沒有問題。當你插入數據時,你是否100%確定問題不存在?重命名您的whichmovie數據庫,創建空的whichmovie數據庫,並只是在我的答案中導入這些查詢。在創建表時啓動這些查詢時是否真的看到錯誤? –

回答

3

只需重命名約束名稱不同,你的問題就解決了......

例子:

CONSTRAINT `IdComent_CONSTRAINT` 
    FOREIGN KEY (`IDComent`) 

現在你有一個從外鍵ONE

0

不同CONSTRAINT'NAME什麼版本你正在使用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設計器的視圖下方:View from phpmyadmin designer

相關問題