MySQL ERROR 1005: Can't create table (errno: 150)

别等时光非礼了梦想. 提交于 2019-12-05 04:07:39

You can check the status of InnoDB (SHOW ENGINE INNODB STATUS) to determine the exact reason why the constraints are failing. The other option is to add the foreign key constraints after creating the table.

In your case, it appears that you're missing the engine type. The column types must also match. The primary key's on the referenced tables are most likely NOT NULL, and they are not so in messaInScena.

create table spazio
  (
    nome             varchar(20) NOT NULL primary key, 
    indirizzo        varchar(40) not null,
    pianta           varchar(20),
    capienza         smallint
  ) ENGINE=InnoDB;

create table spettacolo
  (
    titolo             varchar(40) NOT NULL primary key,  
    descrizione        LONGBLOB,
    annoProduzione     char(4)
  ) ENGINE=InnoDB;

create table messaInScena
  (
    data               date,  
    ora                time,
    spazio             varchar(20) NOT NULL,
    spettacolo         varchar(40) NOT NULL,
    postiDisponibili   smallint,
    prezzoIntero       decimal(5,2),
    prezzoRidotto      decimal(5,2),
    prezzoStudenti     decimal(5,2),
    primary key (data, ora, spazio),
    foreign key (spazio) references spazio(nome) 
on update cascade on delete set null,
    foreign key (spettacolo) references spettacolo(titolo) 
on update cascade on delete set null,
    constraint RA3_1 check (postiDisponibili >= 0)     
  ) ENGINE=InnoDB;

you are a genious! first of all I checked the status of InnoDB, and the reason of the proble was that i tried to set null the fk on delete so i changed the query in this way


create table messaInScena
  (
    data               date,  
    ora                time,
    spazio             varchar(20),
    spettacolo         varchar(40),
    postiDisponibili   smallint,
    prezzoIntero       decimal(5,2),
    prezzoRidotto      decimal(5,2),
    prezzoStudenti     decimal(5,2),
    primary key (data, ora, spazio),
    foreign key (spazio) references spazio(nome)
on update cascade on delete cascade,
    foreign key (spettacolo) references spettacolo(titolo) 
on update cascade on delete cascade,
    constraint RA3_1 check (postiDisponibili >= 0)     
  ) ;
  

and execute it. now it's work. anyway now I'll carry the changes that you suggest me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!