There are no primary or candidate keys in the table referenced

1

Hi, I have a problem with

asked by EmiliOrtega 13.07.2017 в 05:08
source

1 answer

2

The problem you are having is the following, in the foreign key of "BODEGAS" next:

ALTER TABLE BODEGAS ADD CONSTRAINT FK_BODEGAS1 FOREIGN KEY(ID_FABRICA) REFERENCES ESTANCOS(ID_FABRICA) ON DELETE CASCADE;

You are referring to a column in the "ESTANCOS" table that is not the PRIMARY KEY, since the PRIMARY KEY of this table is defined as a set of columns:

ALTER TABLE ESTANCOS ADD CONSTRAINT PK_ESTANCOS PRIMARY KEY(ID_FABRICA,NUM_EXPENDIDURIA,CODIGO_DEL_ESTANCO);

So if you want to make a FOREIGN KEY to a column that is not PRIMARY KEY you should make that column be UNIQUE as indicated in the documentation :

  

A FOREIGN KEY constraint does not have to be linked to PRIMARY only   KEY constraint in another table; it can also be defined to reference   the columns of a UNIQUE constraint in another table.

Translation:

  

A FOREIGN KEY constraint does not have to be linked to only one   PRIMARY KEY constraint in another table; It can also be defined for   refer to columns with a UNIQUE constraint in another   table.

    
answered by 13.07.2017 / 08:41
source