Home > Software design >  why I am getting error trying to add Foreign key to table
why I am getting error trying to add Foreign key to table

Time:04-08

I am having trouble adding foreign key to tbl1 referencing tbl2 (Desc), here are the two tables : please notice Tbl1 includes all values,tbl2 Has more values that doesn't necessarily exists in tbl1

tbl1: Primary key(Desc)

Desc
Astrogator
Geologist
Technician

tbl2:

Primary key (ID,Desc) -Table level primary key

ID Desc
1001 Astrogator
1001 Geologist
1001 Technician
2003 Biochemist

thank you for the help,

CodePudding user response:

you cant make Desc that at table tbl2 as a FK while it contains values not exists at the table tbl1

CodePudding user response:

Adding a foreign key is different depending on the flavor of SQL your using. For sql server you would bring up the parent db in SSMS using table design. Select Foreign key relationships and add a new relationship. From there select other table and column which has foreign key. Keep in mind that there needs to exist a foreign key for each Primary record or you will get an error.

  • Related