Can a Primary Key be a Foreign Key in Two Different Tables?
Last Updated :
04 Apr, 2024
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 key BookID
.
- The
Authors
the table has a primary key AuthorID
.
- The
BookAuthors
table, linking books to authors, uses BookID
as a foreign key referencing Books
and AuthorID
as a foreign key referencing Authors
.
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.
Share your thoughts in the comments
Please Login to comment...