Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one table that's primary key is an identity. I am adding this PK as a foreign key in a different table but wandered if i still had to set this foreign key column to identity?

What I have tried:

I have looked to see if this was necessary in other forums but cannot find an answer.
Posted
Updated 13-Apr-18 6:15am

Absolutely NOT !

1 - The foreign key is used for a one-to-many relationship. It ties together a parent record with any amount of child records.
2 - The foreign key is declared to prevent any records from being in the child table that have no reference (primary key) in the parent table. This helps insure referential integrity.

If you need to force only a single child record for each parent record but still maintain the foreign key constraint the add a UNIQUE constraint to the column, as well.


 
Share this answer
 
v2
Agree with Balboos. Your foreign key column must have the same value as the primary key column it's referencing in the other table. If your foreign key column were an IDENTITY column then it would be creating new values for every record. Those values would not necessarily match the values in the primary key column you're referencing. That would contradict the purpose of creating the foreign key.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900