Answer: Yes, a primary key from one table can act as a foreign key in two different tables.
It is a common scenario in database design for maintaining referential integrity and establishing relationships between tables. The primary key uniquely identifies each row within its table, while the foreign key links data between tables, referencing the primary key of another table to ensure data consistency.
For instance, in a database modeling an online bookstore:
-
The
Books
the table has a primary keyBookID
. -
The
Authors
the table has a primary keyAuthorID
. -
The
BookAuthors
table, linking books to authors, usesBookID
as a foreign key referencingBooks
andAuthorID
as a foreign key referencingAuthors
.
In this example, BookID is a primary key in the Books table and acts as a foreign key in the BookAuthors table. Similarly, AuthorID is a primary key in the Authors table and also acts as a foreign key in the BookAuthors table. This design allows a book to be associated with multiple authors and an author with multiple books, demonstrating how a primary key can serve as a foreign key in more than one table.