Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Error: Can't create table (errno: 121 "Duplicate key on write or update")

I use MySQL Workbench to generate diagram and code for database.

enter image description here

When I put code to phpMyAdmin, it show error (#1005 - Can't create table wypozyczalnia.wypozyczenie (errno: 121 "Duplicate key on write or update"))

Where is the problem? :( Foreign Keys - table wypozyczenie

enter image description here

Error is from this part of code:

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[...]
like image 225
Kamil Kostka Avatar asked Feb 06 '23 15:02

Kamil Kostka


1 Answers

As the error message says: you must have used the same constraint name twice. The constraint may even be in a different table, since the constraint name 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.

like image 196
Shadow Avatar answered Feb 08 '23 16:02

Shadow