
18:19
Lec-12: Insert, Update & Delete from Foreign Key table | Referential Integrity
Gate Smashers
Overview
This video explains the concept of referential integrity in databases and how foreign keys enforce it. It details the operations of insertion, deletion, and updating on both referenced (parent) and referencing (child) tables. The video highlights potential violations of integrity during these operations and introduces solutions like ON DELETE CASCADE, ON DELETE SET NULL, and ON UPDATE CASCADE to manage data consistency and prevent data loss or inconsistencies.
How was this?
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- Integrity in a database means that data values are consistent and accurate across related tables.
- Unlike independent databases (e.g., different e-commerce sites), interconnected databases (like a university system) require strict data consistency.
- Referential integrity ensures that relationships between tables remain valid, preventing orphaned records or inconsistent data.
- A foreign key in one table references the primary key in another table, establishing and enforcing this relationship.
Maintaining referential integrity is crucial for the reliability and accuracy of a database, ensuring that relationships between data are always valid and preventing logical errors.
In a university database, if a student's record exists, their associated course enrollment must also be valid. If a student record is deleted, the system should handle the corresponding course enrollments to avoid showing that a non-existent student is enrolled in a course.
- Inserting data into the referenced table (the one with the primary key) generally causes no integrity issues, as it's the source of truth.
- Deleting data from the referenced table can cause violations if the record is still referenced by a foreign key in another table.
- Updating data in the referenced table can also cause violations if the primary key value is changed, and that change isn't reflected in the referencing tables.
- Solutions for deletion and update violations include ON DELETE CASCADE (automatically delete/update related records), ON DELETE SET NULL (set referencing foreign keys to NULL), and ON DELETE NO ACTION (prevent the operation and raise an error).
Understanding how operations on the parent table affect child tables is essential for preventing data corruption and ensuring that changes are propagated correctly or handled gracefully.
If a student's roll number (primary key) is deleted from the 'Students' table, and that roll number exists in the 'Courses' table (as a foreign key), deleting the student record without proper handling would create an inconsistency. Using ON DELETE CASCADE would automatically remove the student's course enrollment record.
- Inserting data into the referencing table (the one with the foreign key) can cause violations if the foreign key value does not exist in the referenced table's primary key.
- Deleting data from the referencing table typically does not cause integrity violations in the referenced table, as it only removes a reference, not the source data.
- Updating data in the referencing table can cause violations if the foreign key value is changed to a value that does not exist in the referenced table.
- By default, databases often prevent invalid insertions or updates in the referencing table to maintain referential integrity.
These rules ensure that data entered into a child table always has a valid corresponding entry in the parent table, preventing the creation of 'orphan' records that point to non-existent data.
Attempting to enroll a student with roll number '7' in a course when no student with roll number '7' exists in the main student table will result in an insertion error, thus maintaining referential integrity.
Key takeaways
- Foreign keys are essential for maintaining referential integrity, ensuring consistency between related database tables.
- Referential integrity prevents invalid data relationships, such as a course enrollment for a non-existent student.
- Operations on the referenced (parent) table, especially deletion and updates, require careful handling to avoid breaking relationships with referencing (child) tables.
- Referencing (child) tables cannot contain foreign key values that do not exist as primary keys in the referenced (parent) table.
- Database systems provide mechanisms like ON DELETE CASCADE, ON DELETE SET NULL, and ON UPDATE CASCADE to automate the management of data consistency during modifications.
- Understanding the impact of insert, delete, and update operations on both parent and child tables is critical for database management.
Key terms
Foreign KeyReferential IntegrityReferenced Table (Parent Table)Referencing Table (Child Table)Primary KeyON DELETE CASCADEON DELETE SET NULLON UPDATE CASCADEData ConsistencyData Violation
Test your understanding
- What is referential integrity and why is it important in a relational database?
- How can deleting a record from a referenced (parent) table potentially violate referential integrity?
- What are the different strategies (like ON DELETE CASCADE or SET NULL) for handling deletions in a referenced table, and what are their implications?
- Why does inserting a record into a referencing (child) table fail if the corresponding foreign key value doesn't exist in the referenced (parent) table?
- How does updating a primary key in the referenced table pose a risk to referential integrity, and what solutions exist?