FosAvance
FosAvance

Reputation: 2469

MySQL 1 foreign key referencing 2 primary

First part of my question:

table1
ID (PRIMARY)

table2
ID (PRIMARY)

table3
IDleglo (FOREIGN KEY)

Here is my situation. I have 2 IDs from different tables and they are Primary Keys, Auto incremented. On third table (table3) I have IDleglo as Foreign key that is referencing to ID from table1 and table2.

Problem If I alter tables and make:

FOREIGN KEY (IDleglo) REFERENCES table1(ID)
        ON UPDATE CASCADE
        ON DELETE CASCADE
FOREIGN KEY (IDleglo) REFERENCES table2(ID)
        ON UPDATE CASCADE
        ON DELETE CASCADE

How will database know which value in IDleglo is from ID(table1) and which from ID(table2). Cause I could update ID (table1) and it would update IDleglo where ID=IDleglo, but it could affect values that IDleglo got from ID (table2).

Second part of my question is: What does this mean, for example ADD CONSTRAINT FK_borrowed

Upvotes: 0

Views: 106

Answers (1)

Joni
Joni

Reputation: 111259

A column has only one value. If you change the id in table1 the change is cascaded to table3. If the new id value is not already present in table2 you will get an error because the foreign key is violated.

The second part of your question, you can read about constraints here: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Upvotes: 1

Related Questions