Referential integrity is a database rule that helps maintain consistency between related tables. It ensures that a foreign key refers to a valid record in the related table when the relationship requires a matching record.
Referential integrity maintains valid relationships between records in different tables. It helps prevent records from referring to parent records that do not exist.
Courses Table
| CourseID | CourseName |
|---|---|
| 1 | ADCA |
| 2 | Tally Prime |
| 3 | Web Development |
Students Table
| StudentID | StudentName | CourseID |
|---|---|---|
| 101 | Rahul Kumar | 1 |
| 102 | Priya Singh | 2 |
| 103 | Amit Kumar | 1 |
Every CourseID value in the Students table refers to a CourseID that exists in the Courses table.
An orphan record is a child record that refers to a parent record that does not exist.
For example, a student record containing CourseID 99 is an orphan record if CourseID 99 does not exist in the Courses table and the relationship requires a matching course.
A primary key uniquely identifies every record in a table. It cannot contain duplicate values or Null.
Courses
-------------------------
CourseID Primary Key
CourseName
A foreign key stores a value that refers to a key in another table.
Students
-------------------------
StudentID Primary Key
StudentName
CourseID Foreign Key
In this example, Students.CourseID refers to Courses.CourseID.
Referential integrity is applied to a relationship between tables. You must first create the relationship using the appropriate fields.
You can enable referential integrity in the Edit Relationships dialog box if the fields and existing records meet the requirements.
The Enforce Referential Integrity option tells Access to maintain the required relationship rules between the related tables.
Suppose the Courses table contains CourseID values 1, 2, and 3. Referential integrity is enforced between Courses and Students.
StudentID: 101
StudentName: Rahul Kumar
CourseID: 1
This record can be valid because CourseID 1 exists in the Courses table.
StudentID: 104
StudentName: Mohan Kumar
CourseID: 99
This record cannot be saved with CourseID 99 when that course does not exist and referential integrity requires a matching course.
A parent record is a record in the table that contains the referenced key. A child record contains the corresponding foreign key.
For example, Courses is the parent table and Students is the child table in our example.
If a course has related student records, Access normally prevents deleting that course while those related records remain, unless an applicable cascade-delete option is enabled.
CourseID 1 is used by Rahul and Amit. Deleting the ADCA course could leave their records referring to a nonexistent course.
Referential integrity protects against this situation unless the relationship is configured to handle the deletion through cascading.
Cascade Update Related Fields is an option available for supported relationships. It propagates changes to a referenced key to matching foreign-key values.
Suppose CourseID 10 is referenced by several student records. If the referenced key changes and Cascade Update is enabled, Access can update the related foreign-key values accordingly.
Cascade Delete Related Records allows Access to delete related child records when their parent record is deleted.
If CourseID 1 is used by several students and Cascade Delete is enabled, deleting that course can also delete the related student records.
Solution: Check whether the related fields have compatible data types and whether the parent field is a suitable key.
Solution: Check for unmatched foreign keys in the child table. Every non-null foreign-key value subject to the relationship must match a valid parent key.
Solution: Check whether child records reference the parent record. Decide whether to keep those records, update their references, or use a carefully considered cascade-delete rule.
Solution: Review the field definitions in both tables. For example, an AutoNumber key commonly uses a Number field with Long Integer size as its related foreign key.
Solution: Identify the invalid foreign keys and correct them by adding the appropriate parent records or changing the foreign-key values.
Create a Students and Courses database and enforce referential integrity between the tables.
1. What is the main purpose of referential integrity?
2. What is an orphan record?
3. What can Cascade Delete do?