What is self referencing foreign key? Give an example.
What is self referencing foreign key? Give an example.
A self-referencing foreign key is a type of foreign key in a database table that establishes a relationship with the primary key of the same table. This is also known as a recursive foreign key. The purpose of a self-referencing foreign key is typically to model hierarchical or recursive relationships within the same table, such as organizational structures or parent-child relationships.
Consider a table named Employee that includes information about employees in a company. Each employee has a unique employee number (EmpID), a name (EmpName), and a manager's employee number (MgrID). The MgrID in this table acts as a foreign key that references the EmpID of another employee within the same Employee table. This setup allows the table to express the reporting relationships between employees and their managers.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.