Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2: Referential Integrity (Foreign Keys)

Lesson 20 of 28 in the free Database Management Systems notes on Siksha Sarovar, written by Rohit Jangra.

20.1 Referential Integrity Defined

Referential Integrity ensures that the relationship between tables remains consistent. Specifically, it ensures that a value in the Foreign Key column of one table matches an existing value in the Primary Key column of the referenced (parent) table.

20.2 The FOREIGN KEY Constraint

A Foreign Key is a field in one table that refers to the Primary Key in another table.

Parent vs. Child Table

  • Parent Table (Referenced): Contains the primary key being referred to (e.g., DEPARTMENT).
  • Child Table (Referencing): Contains the foreign key (e.g., EMPLOYEE).

20.3 Referential Actions

What happens if you try to delete or update a record in the Parent table that is being referred to by a Child table?

ActionDescription
RESTRICT / NO ACTIONThe deletion/update is blocked. (Default).
CASCADEIf the parent row is deleted/updated, the child rows are also deleted/updated.
SET NULLThe child row's foreign key is set to NULL if the parent row is deleted.
SET DEFAULTThe child row's foreign key is set to its default value.

20.4 Example Implementation

CREATE TABLE Orders (
    OrderID int PRIMARY KEY,
    CustomerID int,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
    ON DELETE CASCADE
);