Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is referential integrity in a database?
Referential integrity means that relationships between tables stay correct. If one table points to another, the data must match so no broken links happen.
Click to reveal answer
beginner
How does a foreign key help enforce referential integrity?
A foreign key is a column that links to a primary key in another table. It makes sure you can only add values that exist in the linked table, preventing wrong or missing references.
Click to reveal answer
intermediate
What happens if you try to delete a row that other rows reference via a foreign key?
The database will block the delete unless you specify rules like CASCADE to delete related rows too, or SET NULL to clear the reference. This keeps data consistent.
Click to reveal answer
intermediate
Explain the difference between ON DELETE CASCADE and ON DELETE SET NULL.
ON DELETE CASCADE deletes all rows that reference the deleted row. ON DELETE SET NULL sets the foreign key to NULL instead, keeping the row but removing the link.
Click to reveal answer
beginner
Why is referential integrity important in real-life databases?
It prevents errors like orphan records or broken links, which can cause wrong reports or app crashes. It keeps data trustworthy and easy to manage.
Click to reveal answer
What does a foreign key in a table do?
ALinks to a primary key in another table to enforce data consistency
BCreates a unique identifier for each row
CStores large text data
DIndexes the table for faster search
✗ Incorrect
A foreign key links to a primary key in another table to keep data consistent and enforce referential integrity.
Which SQL clause enforces referential integrity when deleting a referenced row?
AON DELETE CASCADE
BGROUP BY
CORDER BY
DWHERE
✗ Incorrect
ON DELETE CASCADE automatically deletes rows that reference the deleted row, maintaining referential integrity.
If a foreign key constraint is violated, what will the database do?
ADelete all data in the table
BAutomatically fix the data
CIgnore the violation
DReject the operation to keep data consistent
✗ Incorrect
The database rejects changes that break referential integrity to avoid inconsistent data.
What does ON DELETE SET NULL do?
APrevents deletion of the referenced row
BSets the foreign key to NULL when the referenced row is deleted
CDeletes the referencing row
DUpdates the foreign key to a default value
✗ Incorrect
ON DELETE SET NULL clears the foreign key value instead of deleting the row, keeping the row but removing the link.
Which of these is NOT a benefit of enforcing referential integrity?
APreventing orphan records
BEnsuring data consistency
CAutomatically backing up data
DAvoiding broken links between tables
✗ Incorrect
Referential integrity does not handle backups; it ensures data consistency and valid links.
Describe what referential integrity means and how foreign keys help enforce it.
Think about how tables relate and how the database keeps those links correct.
You got /3 concepts.
Explain the difference between ON DELETE CASCADE and ON DELETE SET NULL in foreign key constraints.
Consider what happens to rows that reference a deleted row.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of referential integrity in a database?
easy
A. To speed up query execution
B. To ensure relationships between tables remain consistent
C. To store large amounts of data efficiently
D. To create backup copies of the database
Solution
Step 1: Understand referential integrity concept
Referential integrity ensures that foreign keys in one table correctly reference existing rows in another table.
Step 2: Identify the main purpose
This prevents orphan records and keeps data relationships consistent and safe.
Final Answer:
To ensure relationships between tables remain consistent -> Option B
Hint: Referential integrity means keeping table links correct [OK]
Common Mistakes:
Confusing referential integrity with performance optimization
Thinking it creates backups
Assuming it stores data efficiently
2. Which SQL statement correctly defines a foreign key with referential integrity enforcement?
easy
A. INSERT INTO Orders (OrderID, CustomerID) VALUES (1, 100);
B. CREATE TABLE Orders (OrderID INT, CustomerID INT PRIMARY KEY);
C. ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
D. SELECT * FROM Orders WHERE CustomerID = Customers.CustomerID;
Solution
Step 1: Identify foreign key syntax
The correct syntax to add a foreign key is using ALTER TABLE with ADD FOREIGN KEY referencing another table's column.
Step 2: Check each option
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); correctly adds a foreign key constraint. CREATE TABLE Orders (OrderID INT, CustomerID INT PRIMARY KEY); wrongly sets CustomerID as primary key without foreign key. INSERT INTO Orders (OrderID, CustomerID) VALUES (1, 100); is an insert, not a constraint. SELECT * FROM Orders WHERE CustomerID = Customers.CustomerID; is a select query, not a constraint definition.
Final Answer:
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); -> Option C
Hint: Foreign keys use ALTER TABLE ADD FOREIGN KEY ... REFERENCES [OK]
Common Mistakes:
Confusing primary key with foreign key syntax
Using INSERT or SELECT instead of constraint definition
Missing REFERENCES keyword
3. Given these tables:
CREATE TABLE Customers (CustomerID INT PRIMARY KEY, Name VARCHAR(50));
CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE CASCADE);
What happens if a customer with CustomerID = 5 is deleted?
medium
A. All orders with CustomerID = 5 are also deleted
B. The delete fails due to foreign key constraint
C. Orders with CustomerID = 5 remain unchanged
D. CustomerID in Orders is set to NULL for those orders
Solution
Step 1: Understand ON DELETE CASCADE
The ON DELETE CASCADE option means deleting a referenced row causes all related rows to be deleted automatically.
Step 2: Apply to the scenario
Deleting customer with CustomerID=5 will delete all orders linked to that customer in Orders table.
Final Answer:
All orders with CustomerID = 5 are also deleted -> Option A
Quick Check:
ON DELETE CASCADE = delete related rows [OK]
Hint: ON DELETE CASCADE deletes related rows automatically [OK]
Common Mistakes:
Thinking delete will fail due to constraint
Assuming related rows remain unchanged
Confusing CASCADE with SET NULL
4. You have this foreign key constraint:
FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ON DELETE SET NULL
Which error will occur if you try to delete a product that is referenced by an order, but the ProductID column in Orders is defined as NOT NULL?
medium
A. Delete fails due to NOT NULL constraint violation
B. Delete succeeds and sets ProductID to NULL
C. Delete succeeds and removes the order row
D. Delete succeeds without affecting Orders
Solution
Step 1: Understand ON DELETE SET NULL behavior
This option sets the foreign key column to NULL in referencing rows when the referenced row is deleted.
Step 2: Check NOT NULL constraint conflict
If the foreign key column is NOT NULL, setting it to NULL violates the column constraint, causing the delete to fail.
Final Answer:
Delete fails due to NOT NULL constraint violation -> Option A
Quick Check:
SET NULL + NOT NULL column = delete fails [OK]
Hint: SET NULL fails if foreign key column is NOT NULL [OK]
Common Mistakes:
Assuming delete succeeds and sets NULL anyway
Thinking delete removes referencing rows
Ignoring NOT NULL constraint on foreign key
5. You want to enforce referential integrity between Employees and Departments tables. When a department is deleted, you want all employees in that department to be reassigned to department ID 0 (which means 'Unassigned'). Which foreign key option should you use?
hard
A. FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE RESTRICT
B. FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE CASCADE
C. FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE SET NULL
D. FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE SET DEFAULT
Solution
Step 1: Understand ON DELETE SET DEFAULT
This option sets the foreign key column to its default value when the referenced row is deleted.
Step 2: Match requirement
Since you want employees reassigned to department ID 0, set DepartmentID column default to 0 and use ON DELETE SET DEFAULT to assign that value automatically.
Final Answer:
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE SET DEFAULT -> Option D
Quick Check:
Reassign on delete = ON DELETE SET DEFAULT [OK]
Hint: Use ON DELETE SET DEFAULT to assign default on delete [OK]
Common Mistakes:
Using CASCADE deletes employees instead of reassigning