1
votes

Here there is the small part of the script (that are the first lines of that script): When i try to execute it says me:
Error Code: 1005. Can't create table 'test.paese' (errno: 150) 0.062 sec

And when i try to do "Forward Engineer" from MySQL , it replies me somehting about:
If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.

`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 `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `test` ;

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


-- -----------------------------------------------------
-- Table `test`.`PAESE`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `test`.`PAESE` (
  `Nome-paese` CHAR NOT NULL ,
  `Comune` CHAR NOT NULL ,
  `num_abitanti` INT(11) NULL ,
  `altitudine` INT(11) NULL ,
  `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 `test`.`SETTORE` (`Comune` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB;


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


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


-- -----------------------------------------------------
-- Table `test`.`INIZIATIVA`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `test`.`INIZIATIVA` (
  `Nome-tipo-iniziativa` CHAR NOT NULL ,
  `Settimana` INT(11) NOT NULL ,
  `Nome-paese` CHAR NOT NULL ,
  `Comune` CHAR NOT NULL ,
  `descrizione` VARCHAR(45) ,
  `costo_intero` FLOAT NULL ,
  `costo_ridotto` FLOAT NULL ,
  `orario_apertura` TIME NULL ,
  `orario_chiusura` TIME NULL ,
  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 `test`.`PAESE` (`Nome-paese` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `test`.`PAESE` (`Comune` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `Settimana`
    FOREIGN KEY (`Settimana` )
    REFERENCES `test`.`PERIODO` (`Settimana` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `Nome-tipo-iniziativa`
    FOREIGN KEY (`Nome-tipo-iniziativa` )
    REFERENCES `test`.`TIPO-INIZIATIVA` (`Nome-tipo-iniziativa` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB;`

So the DBMS first create the table SETTORE, but then while executing CREATE TABLE PAESE it stops. I can't find the error among these lines, please help me, i need it so much!!

2
Please post your sql code.N1tr0

2 Answers

3
votes

The constraint is failing because of your reference options.

... ON DELETE SET NULL

This is illegal because your column can't be NULL.

CREATE  TABLE IF NOT EXISTS `test`.`PAESE` (
  `Nome-paese` CHAR NOT NULL ,
  `Comune` CHAR NOT NULL ,  <----------------------------+
  `num_abitanti` INT(11) NULL ,                          |
  `altitudine` INT(11) NULL ,                            |
  `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 `test`.`SETTORE` (`Comune` )              |
    ON DELETE SET NULL  <--------------------------------+
    ON UPDATE CASCADE)
2
votes

That is because of conflicting constraints on the column Comune

Column Comune is not nullable in test.PAESE. You're making MySQL spin!

SHOW ENGINE INNODB STATUS;

------------------------
LATEST FOREIGN KEY ERROR
------------------------
130129 21:55:19 Error in foreign key constraint of table test/paese:
FOREIGN KEY (`Comune`)
        REFERENCES `test`.`SETTORE` (`Comune`)
        ON DELETE SET NULL ON UPDATE CASCADE
):
You have defined a SET NULL condition though some of the
columns are defined as NOT NULL.
------------