I am trying to create the following table
我正在尝试创建下表
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 set null,
foreign key (spettacolo) references spettacolo(titolo)
on update cascade on delete set null,
constraint RA3_1 check (postiDisponibili >= 0)
) ;
but I get the following error: Error Code: 1005 Can not create table 'teatrosql.messainscena' (errno: 150)
但我收到以下错误:错误代码:1005无法创建表'teatrosql.messainscena'(错误号:150)
The tables that are referenced by foreign keys are:
外键引用的表是:
create table spazio
(
nome varchar(20) primary key,
indirizzo varchar(40) not null,
pianta varchar(20),
capienza smallint
);
create table spettacolo
(
titolo varchar(40) primary key,
descrizione LONGBLOB,
annoProduzione char(4)
);
I have already verified that the fk are unique and that there are no typos (but given a control also you that you never know :D). As you can see the reference fields are primary keys. between fields and fk reference types and dimensions coincide ..
我已经验证了fk是独一无二的并且没有拼写错误(但是你也可以控制一个你永远不知道的控件:D)。如您所见,引用字段是主键。字段和fk引用类型和维度之间重合..
where am I wrong??
我哪里错了?
the the other tables of DB
DB的其他表
create table teatro
(
nome varchar(20) primary key,
telefono varchar(15),
fax varchar(15),
indirizzo varchar(40) not null,
email varchar(30),
url varchar(30)
);
create table biglietteria
(
nome varchar(20) primary key,
indirizzo varchar(40) not null,
email varchar(30),
telefono varchar(15),
teatro varchar(20),
foreign key (teatro) references teatro(nome)
on update cascade on delete set null
);
create table orario
(
biglietteria varchar(20),
giorno varchar(10),
inizio time,
fine time,
primary key(biglietteria, giorno, inizio),
foreign key (biglietteria) references biglietteria(nome)
on update cascade on delete cascade
);
create table notizia
(
data date,
ora time,
oggetto varchar(100),
testo LONGBLOB,
primary key(data, ora, oggetto)
);
create table newsletter
(
teatro varchar(20),
data date,
ora time,
oggetto varchar(100),
primary key(teatro, data, ora, oggetto),
foreign key (teatro) references teatro(nome)on update cascade on delete cascade,
foreign key (data, ora, oggetto) references notizia(data, ora, oggetto) on update cascade on delete cascade
);
create table dipendente
(
cf char(16) primary key,
nome varchar(20) not null,
cognome varchar(20) not null,
dataDiNascita date,
luogoDiNascita varchar(20),
residenza varchar(30),
telefonoFisso varchar(15),
telefonoMobile varchar(15),
email varchar(30)
);
create table lavoro
(
teatro varchar(20),
dipendente char(16),
dataAssunzione date,
ruolo varchar(5),
cda boolean,
primary key(teatro, dipendente),
foreign key (teatro) references teatro(nome) on update cascade on delete cascade,
foreign key (dipendente) references dipendente(cf) on update cascade on delete cascade,
constraint RA1 check (
cda = false or
(year(current_date) - year(dataAssunzione) > 10) or
(year(current_date) - year(dataAssunzione) = 10 and
month(current_date) > month(dataAssunzione)) or
(year(current_date) - year(dataAssunzione) = 10 and
month(current_date) = month(dataAssunzione) and
day(current_date) >= day(dataAssunzione))
),
check (ruolo in ('CA', 'POD', 'CUSRP', 'ACF'))
);
create table stipendio
(
dipendente char(16),
inizio date,
importo decimal(6,2),
primary key(dipendente, inizio),
foreign key (dipendente) references dipendente(cf) on update cascade on delete cascade
) ;
create table luogo
(
teatro varchar(20),
spazio varchar(20),
primary key(teatro, spazio),
foreign key (teatro) references teatro(nome) on update cascade on delete cascade,
foreign key (spazio) references spazio(nome) on update cascade on delete cascade
) ;
2 个解决方案
#1
12
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.
您可以检查InnoDB(SHOW ENGINE INNODB STATUS)的状态,以确定约束失败的确切原因。另一个选项是在创建表后添加外键约束。
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
.
在您的情况下,您似乎错过了引擎类型。列类型也必须匹配。引用表上的主键很可能不是NULL,而在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;
#2
0
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
你是一个天才!首先,我检查了InnoDB的状态,问题的原因是我试图在删除时将fk设置为null,所以我以这种方式更改了查询
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
并执行它。现在它的工作。无论如何,现在我将进行你建议我的改变
#1
12
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.
您可以检查InnoDB(SHOW ENGINE INNODB STATUS)的状态,以确定约束失败的确切原因。另一个选项是在创建表后添加外键约束。
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
.
在您的情况下,您似乎错过了引擎类型。列类型也必须匹配。引用表上的主键很可能不是NULL,而在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;
#2
0
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
你是一个天才!首先,我检查了InnoDB的状态,问题的原因是我试图在删除时将fk设置为null,所以我以这种方式更改了查询
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
并执行它。现在它的工作。无论如何,现在我将进行你建议我的改变