Horje
Can a Primary Key be a Foreign Key in Two Different Tables?

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.




Reffered: https://www.geeksforgeeks.org


DBMS

Related
How To Identify Key Attributes in DBMS? How To Identify Key Attributes in DBMS?
How DBMS Performance is Improved? How DBMS Performance is Improved?
How DBMS Stores Data? How DBMS Stores Data?
What Can be a Foreign Key in Another Table? What Can be a Foreign Key in Another Table?
Database Languages in DBMS Database Languages in DBMS

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13