2016-11-27 73 views
1

我使用MySQL Workbench爲數據庫生成圖表和代碼。MySQL錯誤:無法創建表(錯誤:121「寫入或更新時出現重複鍵」)

enter image description here

當我把代碼到phpMyAdmin,它顯示錯誤(#1005 - 無法創建表wypozyczalniawypozyczenie(錯誤:121 「上寫或更新重複鍵」))

在哪裏問題是什麼? :( 外鍵 - 表wypozyczenie

enter image description here

錯誤是由這部分代碼:

CREATE TABLE IF NOT EXISTS `wypozyczalnia`.`wypozyczenie` (
`id_wyp` INT NOT NULL, 
`data_wyp` DATE NOT NULL, 
`data_odd` DATE NOT NULL, 
`id_samochod` INT NOT NULL, 
`id_klient` INT NOT NULL, 
`id_pracownik` INT NOT NULL, 
`id_plac_wyp` INT NOT NULL, 
`id_plac_odd` INT NULL, 
`cena` INT NULL, 
`kaucja` INT NULL, 
PRIMARY KEY (`id_wyp`), 
INDEX `id_klient_idx` (`id_klient` ASC), 
INDEX `id_samochod_idx` (`id_samochod` ASC), 
INDEX `id_placowka_idx` (`id_plac_odd` ASC), 
INDEX `id_pracownik_idx` (`id_pracownik` ASC), 
INDEX `id_plac_wyp_idx` (`id_plac_wyp` ASC), 
CONSTRAINT `id_klient_klient` 
FOREIGN KEY (`id_klient`) 
REFERENCES `wypozyczalnia`.`klient` (`id_klient`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION, 
CONSTRAINT `id_samochod_samochody` 
FOREIGN KEY (`id_samochod`) 
REFERENCES `wypozyczalnia`.`samochody` (`id_samochod`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION, 
CONSTRAINT `id_placowka_placowka` 
FOREI[...] 
+1

由於錯誤消息sa y:你必須兩次使用相同的約束名稱。約束甚至可能在不同的表格中。 – Shadow

回答

2

由於錯誤消息說:你必須使用相同的約束名稱兩次約束。因爲約束名稱must be unique in a database

CONSTRAINT symbol

If the CONSTRAINT symbol clause is given, the symbol value, if used, must be unique in the database. A duplicate symbol results in an error. If the clause is not given, or a symbol is not included following the CONSTRAINT keyword, a name for the constraint is created automatically.

相關問題