im new on mysql workbench, and i tried so many things to put my script working but i simply cant... Ive got these tables:
CREATE TABLE Utilizador (email varchar(40) not null, nome varchar(50)
not null, dataNascimento date, profissao varchar(50) not null,
reputacao double(3,2) unsigned not null, constraint pk_Utilizador
primary key(email))
This is the first table created!
CREATE TABLE POI (email varchar(40) not null, designacaoPOI
varchar(10) not null, coordenadaX int, coordenadaY int,
descricaoPOI varchar(200), constraint pk_POI primary key(email,
designacaoPOI), constraint fk_POI foreign key(email) references
Utilizador(email) on delete cascade)
This is the second table created!
CREATE TABLE Utilizador_POI (email varchar(40) not null, designacaoPOI
varchar(10) not null, constraint pk_Utilizador_POI primary key(email,
designacaoPOI), constraint fk1_Utilizador_POI foreign key(email)
references Utilizador(email) on delete cascade, constraint
fk2_Utilizador_POI foreign key(designacaoPOI) references
POI(designacaoPOI) on delete cascade)
This table gives me the error: Error Code: 1215. Cannot add foreign key constraint
I did some tests and im almost sure that the problem is in the foreign key "designacaoPOI". The other FK ("email") dont give me any error, so maybe the problem is in the Table POI?
Thanks in advanced!
The problem is in your second table. Your primary key is (email,designacaoPOI), when you try to reference that in your table it gives you error because of this:
For it to work, either change the order of your second tale PRIMARY KEY :
sqlfiddle demo
or add an index for designacaoPOI:
sqlfiddle demo
Either of these solutions will let you create your third table without errors.
I think the problem is that
Utilizador_POI.email
referencesPOI.email
, which itself referencesUtilizador.email
. MySQL is probably upset at the double-linking.Also, since there seems to be a many-to-many relationship between
Utilizador
andPOI
, I think the structure ofUtilizador_POI
isn't what you really want. Instead,Utilizador_POI
should reference a primary key fromUtilizador
, and a matching primary key fromPOI
.The problem here is twofold:
1/ Use
ID
s forPRIMARY KEY
sYou should be using
ID
s for primary keys rather thanVARCHAR
s or anything that has any real-world "business meaning". If you want theemail
to be unique within theUtilizador
table, the combination ofemail
anddesignacaoPOI
to be unique in thePOI
table, and the same combination (email
anddesignacaoPOI
) to be unique inUtilizador_POI
, you should be usingUNIQUE KEY
constraints rather thanPRIMARY KEY
constraints.2/ You cannot
DELETE CASCADE
on aFOREIGN KEY
that doesn't reference thePRIMARY KEY
In your third table,
Utilizador_POI
, you have twoFOREIGN KEY
s referencesPOI
. Unfortunately, thePRIMARY KEY
onPOI
is a composite key, so MySQL has no idea how to handle aDELETE CASCADE
, as there is not a one-to-one relationship between theFOREIGN KEY
inUtilizador_POI
and thePRIMARY KEY
ofPOI
.If you change your tables to all have a
PRIMARY KEY
ofID
, as follows:Then you can reference each table by
ID
, and both yourFOREIGN KEY
s andDELETE CASCADE
s will work.