Having two foreign keys into a single column
I have a problem in creating a table with a column that has two foreign Key relationships. Let me explain in detail.
I have three tables:
Table A
- columnsID (primary key), Name
Table B
- columnsID (primary key), Name
Table C
- columnsID, Name, Detail
In C.Detail
I have to store data from both other tables (A.ID & B.ID). So I tried to add two foreign key into the column C.Detail
. During insert operation in Table B
the following e开发者_StackOverflow社区rror occurs, and the same error message occurs while trying to insert data into Table A
.
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_C_A". The conflict occurred in database "X", table "dbo.A", column A.ID."
Please, can anyone help us to rectify this problem? We don't want to add two columns in table C
for two foreign keys.
Hopefully waiting for the reply.
I will suggest to introduce two new columns in Table C. (i.e AID and BID). Create Foregin key on this news columns.
I could be wrong, but I think the way to go about doing this is to create a "parent" table for A and B that has A_B_parent.id(primary_key) and then have A and B both have foreign keys on their id to the parent table. Then C can also have a foreign key to the parent table.
This obviously ends up being really complex, so the better solution might just be to programmatically enforce the constraint rather than using foreign keys and then put a comment on the table.
精彩评论