Mysql – ERROR: Error 1005: Can’t create table ‘progetto.museo’ (errno: 121)

constraintMySQLmysql-workbench

I've found on the web that:

You will get this message if you're trying to add a constraint with a name that's already used somewhere else

and

If the table you're trying to create includes a foreign key constraint, and you've provided your own name for that constraint, remember that it must be unique within the database. Run this query to see if that name is in use somewhere

BUT if you can see my script, many tables are created with the same name's INDEX… so what have i to change? (i've posted the first lines of the script to create the database, the file is much bigger)

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `PROGETTO` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `PROGETTO` ;

-- -----------------------------------------------------
-- Table `PROGETTO`.`SETTORE`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`SETTORE` (
  `Comune` CHAR NOT NULL ,
  `superficie` INT(11) NULL ,
  PRIMARY KEY (`Comune`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `PROGETTO`.`PAESE`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`PAESE` (
  `Nome-paese` CHAR NOT NULL COMMENT 'Il nome del paese' ,
  `Comune` CHAR NOT NULL COMMENT 'La provincia del paese' ,
  `num_abitanti` INT(11) NULL COMMENT 'Il numero degli abitanti del paese' ,
  `altitudine` INT(11) NULL COMMENT 'Altitudine espressa in metri' ,
  `IDpaese` INT(11) NOT NULL ,
  PRIMARY KEY (`Nome-paese`, `Comune`) ,
  INDEX `Comune` (`Comune` ASC) ,
  UNIQUE INDEX `idPAESE_UNIQUE` (`IDpaese` ASC) ,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `PROGETTO`.`SETTORE` (`Comune` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `PROGETTO`.`PERIODO`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`PERIODO` (
  `Settimana` INT(11) NOT NULL ,
  PRIMARY KEY (`Settimana`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `PROGETTO`.`TIPO-INIZIATIVA`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`TIPO-INIZIATIVA` (
  `Nome-tipo-iniziativa` CHAR NOT NULL ,
  PRIMARY KEY (`Nome-tipo-iniziativa`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `PROGETTO`.`INIZIATIVA`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`INIZIATIVA` (
  `Nome-tipo-iniziativa` CHAR NOT NULL COMMENT 'Il tipo di iniziativa: gastronomica, faunistica, sport, spettacoli ...' ,
  `Settimana` INT(11) NOT NULL COMMENT 'Il periodo in cui si svolge l\'iniziativa' ,
  `Nome-paese` CHAR NOT NULL COMMENT 'Il nome del paese in cui si svolge l\'inizativa' ,
  `Comune` CHAR NOT NULL COMMENT 'La provincia del paese in cui si svolge l\'iniziativa' ,
  `descrizione` VARCHAR(45) NULL COMMENT 'Una descrizione sull\'iniziativa' ,
  `costo_intero` FLOAT NULL COMMENT 'Il prezzo per vedere/partecipare all\'iniziativa' ,
  `costo_ridotto` FLOAT NULL ,
  `orario_apertura` TIME NULL COMMENT 'Orario di inizio all\'inizativa' ,
  `orario_chiusura` TIME NULL COMMENT 'Orario di fine dell\'iniziativa' ,
  PRIMARY KEY (`Nome-tipo-iniziativa`, `Settimana`, `Nome-paese`, `Comune`) ,
  INDEX `Nome-paese` (`Nome-paese` ASC) ,
  INDEX `Comune` (`Comune` ASC) ,
  INDEX `Settimana` (`Settimana` ASC) ,
  INDEX `Nome-tipo-iniziativa` (`Nome-tipo-iniziativa` ASC) ,
  CONSTRAINT `Nome-paese`
    FOREIGN KEY (`Nome-paese` )
    REFERENCES `PROGETTO`.`PAESE` (`Nome-paese` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `PROGETTO`.`PAESE` (`Comune` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Settimana`
    FOREIGN KEY (`Settimana` )
    REFERENCES `PROGETTO`.`PERIODO` (`Settimana` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Nome-tipo-iniziativa`
    FOREIGN KEY (`Nome-tipo-iniziativa` )
    REFERENCES `PROGETTO`.`TIPO-INIZIATIVA` (`Nome-tipo-iniziativa` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `PROGETTO`.`ALBERGO`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`ALBERGO` (

  `ID` INT(11) NOT NULL COMMENT 'identificativo dell\' albergo' ,
  `nome` VARCHAR(45) NULL COMMENT 'Il nome dell\'albergo' ,
  `viale` VARCHAR(45) NULL COMMENT 'Il viale in cui si trova    ' ,
  `num_civico` VARCHAR(5) NULL COMMENT 'Il numero civico che gli appartiene' ,
  `data_apertura` DATE NULL COMMENT 'Data di inizio apertura (inizio stagione)' ,
  `data_chiusura` DATE NULL COMMENT 'Data di chiusura (fine stagione)' ,
  `orario_apertura` TIME NULL COMMENT 'Orario di apertura' ,
  `orario_chiusura` TIME NULL COMMENT 'Orario di chiusura' ,
  `posti_liberi` INT(11) NULL COMMENT 'Disponiblità posti liberi ' ,
  `costo_intero` FLOAT NULL COMMENT 'Costo del prezzo intero' ,
  `costo_ridotto` FLOAT NULL COMMENT 'Costo del prezzo ridotto' ,
  `stelle` INT(11) NULL COMMENT 'Classificazione in base al criterio delle stelle' ,
  `telefono` VARCHAR(15) NULL COMMENT 'Recapito telefonico' ,
  `mail` VARCHAR(100) NULL COMMENT 'Recapito e-mail' ,
  `web` VARCHAR(100) NULL COMMENT 'Sito Web relativo all\'ente' ,
  `Nome-paese` CHAR NOT NULL COMMENT 'Identificativo del paese in cui si trova l\'albergo' ,
  `Comune` CHAR NOT NULL COMMENT 'Identificativo del comune in cui si trova l\'albergo' ,
  PRIMARY KEY (`ID`) ,
  INDEX `Nome-paese` (`Nome-paese` ASC) ,
  INDEX `Comune` (`Comune` ASC) ,
  CONSTRAINT `Nome-paese`
    FOREIGN KEY (`Nome-paese` )
    REFERENCES `PROGETTO`.`PAESE` (`Nome-paese` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `PROGETTO`.`PAESE` (`Comune` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `PROGETTO`.`MUSEO`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `PROGETTO`.`MUSEO` (
  `ID` INT(11) NOT NULL ,
  `nome` VARCHAR(45) NULL ,
  `viale` VARCHAR(45) NULL COMMENT 'Il viale in cui si trova il museo' ,
  `num_civico` VARCHAR(5) NULL COMMENT 'Il numero civico che gli appartiene' ,
  `data_apertura` DATE NULL COMMENT 'Data di apertura (inizio stagione)' ,
  `data_chiusura` DATE NULL COMMENT 'Data di chiusura (fine stagione)' ,
  `orario_apertura` TIME NULL COMMENT 'Orario di apertura' ,
  `orario_chiusura` TIME NULL COMMENT 'Orario di chiusura' ,
  `costo_intero` FLOAT NULL COMMENT 'il prezzo del biglietto intero' ,
  `costo_ridotto` FLOAT NULL COMMENT 'il prezzo del biglietto ridotto' ,
  `telefono` VARCHAR(15) NULL COMMENT 'Il recapito telefonico' ,
  `mail` VARCHAR(100) NULL COMMENT 'Il recapito e-mail' ,
  `web` VARCHAR(100) NULL ,
  `Nome-paese` CHAR NOT NULL ,
  `Comune` CHAR NOT NULL ,
  PRIMARY KEY (`ID`) ,
  INDEX `Nome-paese` (`Nome-paese` ASC) ,
  INDEX `Comune` (`Comune` ASC) ,
  CONSTRAINT `Nome-paese`
    FOREIGN KEY (`Nome-paese` )
    REFERENCES `PROGETTO`.`PAESE` (`Nome-paese` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `PROGETTO`.`PAESE` (`Comune` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;

Best Answer

There is no need to name your constraints, but if you do, then that is the name that must be unique within each schema. The error is telling you that You already have constraints with the same name elsewhere.

To see where, you can SHOW ENGINE INNODB STATUS;

------------------------
LATEST FOREIGN KEY ERROR
------------------------
130129 19:45:00 Error in foreign key constraint creation for table `test`.`baz`.
A foreign key constraint of name `test`.`foo_id`
already exists. 

But in this case, we don't need to look that up, since it's apparent from the script that you're reusing the same constraint names in multple table definitions.

If you remove lines like this...

CONSTRAINT `Comune`

... but leave the rest of your table definition intact...

  FOREIGN KEY (`Comune` )
  REFERENCES `PROGETTO`.`PAESE` (`Comune` )
  ON DELETE NO ACTION
  ON UPDATE CASCADE)

... you should not have a problem. You will find that InnoDB will generate names for your constraints, like PAESE_ibfk_1, PAESE_ibfk_2, etc.

Or, you can continue to declare the names of your constraints, remembering that whatever comes after the keyword CONSTRAINT has to be unique within each schema.

"If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically."

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html