|
Custom Search
| |
|
Foreign Key Relationships and Considerations Foreign key constraint defines how referential integrity is enforced between two tables. It relates two tables together by common columns in a relational database. If a foreign key in the referencing table (foreign key table) is not null, it must match a primary key in the referenced table (primary key table). This means that relationship between the primary key and the foreign key must always be maintained so that we will never have orphaned records in the referencing table. How foreign key relationship enforces referential integrity We use two examples to demonstrate the concept of foreign key relationship. Example One: foreign key relationship between two tables CategoryID (foreign key) in Products table references to CategoryID (primary key) in Categories table.
Categories table - CateoryID as primary key:
Products table - CategoryID as foreign key:
Integrity enforcement #1:
Integrity enforcement #2: Note that if CategoryID in Products table allows NULL, a new product with NULL CategoryID can be added to Products table. Example Two: self-referencing foreign key relationship Foreign key relationship can be created between two or more columns in the same table. For example, Employees table defines foreign key relationship between EmployeeID and ReportsTo column. Apparently, without looking at the Title column in the table below, we can tell Andrew Fuller (EmployeeID 2) is the boss. This is because his ReportsTo data is NULL which means he reports to no one. Nancy Davollo reports to Andrew Fuller as her ReportsTo value is 2 which is Andrew Fuller's EmployeeID. Ann Dodsworth reports to Steven Duchanan as her ReportsTo value is 5 which is Steven Duchanan's EmployeeID.
Integrity enforcement #1:
Integrity enforcement #2:
To sum up, if a foreign key in the referencing table (foreign key table) is not null, it must match a
primary key in the referenced table (primary key table). This means that relationship between the primary key and the foreign
key must always be maintained so that we will never have orphaned records in the referencing table.
To maintain the foreign key and primary key relationship, a database engine needs to make sure the following two scenarios will never happen: 1. A row/record in primary key table can't be deleted if it still has related records in the foreign key table. It's also true that the primary key can't be updated if there are related records in the foreign key table. For example, customer Wilman Kala (primary key CustomerID WILMK) can't be deleted from Customers table because the customer has placed 9 orders. The record in foreign key table is also known as child record. The record in primary key table is also known as master record. Database engines allow you to define how you want to update the foreign key in child records if you really need to update the primary key in master record. 2. When the foreign key value is not null for a record, the record can't be inserted into the foreign key table if it does not have matching record in primary key table. For example, we can't add an order to the Orders table if there is no customer exists for this order. Where to find more info about foreign key relationships Refer to article Make northwind a true relational database in MySQL for more practical info about:
Other tutorials in this category 1. How to Design Relational Database |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |