I have a table Authors
in SQL Server with Author_ID
as primary key.
Authors
table structure
Author_ID | Author_Name
----------------------------
677 | Nuno Vasconcelos
1359 | Peng Shi
6242 | Z. Q. Shi
... | ...
... | ...
I have another table CoAuthors
with CoAuthor_ID
as primary key and with Author_ID
as foreign key.
CoAuthors
table structure
CoAuthor_ID | CoAuthor_Name | Author_ID
---------------------------------------
47 | Jim Bezdek | NULL
111 | Vishal Gupta | NULL
318 | Muaz A. Niazi | NULL
... | ... | ...
... | ... | ...
I have a another Author-CoAuthor mapping table Yearly_Author_CoAuthors
as:
Author_ID | CoAuthor_ID | Year
------------------------------
677 | 901706 | 2005
677 | 838459 | 2007
677 | 901706 | 2007
... | ... | ...
... | ... | ...
Now I have to insert foreign keys in CoAuthors
table getting from Authors
table. The issue is I may have multiple values of Author_ID
for CoAuthor_ID
as for instance executing this query:
SELECT
Author_ID, CoAuthor_ID, Year
FROM
Yearly_Author_CoAuthors
WHERE
CoAuthor = 901706
ORDER BY
Author_ID, Year, CoAuthor_ID
And I got this output:
Author_ID | CoAuthor_ID | Year
------------------------------
677 | 901706 | 2005
677 | 901706 | 2007
677 | 901706 | 2009
1683703 | 901706 | 2012
which is showing that CoAuthor_ID = 901706
is having two DISTINCT Author_ID
, so here:
How can I insert Author_ID
as foreign key constraint in CoAuthors
table?