0
0
SQLquery~10 mins

Foreign key linking mental model in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Foreign key linking mental model
Create Parent Table
Create Child Table with Foreign Key
Insert Data into Parent
Insert Data into Child
Foreign Key Checks: Does Child value exist in Parent?
Yes/No
Allow Insert
Referential Integrity Maintained
This flow shows how a foreign key links a child table to a parent table, ensuring child values exist in the parent to keep data consistent.
Execution Sample
SQL
CREATE TABLE Departments (
  DeptID INT PRIMARY KEY,
  DeptName VARCHAR(50)
);

CREATE TABLE Employees (
  EmpID INT PRIMARY KEY,
  EmpName VARCHAR(50),
  DeptID INT,
  FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)
);
Creates two tables: Departments (parent) and Employees (child) with a foreign key linking Employees.DeptID to Departments.DeptID.
Execution Table
StepActionTableValue InsertedForeign Key CheckResult
1Insert into DepartmentsDepartmentsDeptID=1, DeptName='HR'N/ASuccess
2Insert into DepartmentsDepartmentsDeptID=2, DeptName='Sales'N/ASuccess
3Insert into EmployeesEmployeesEmpID=101, EmpName='Alice', DeptID=1Check DeptID=1 in DepartmentsExists - Success
4Insert into EmployeesEmployeesEmpID=102, EmpName='Bob', DeptID=3Check DeptID=3 in DepartmentsDoes not exist - Fail
5Insert into EmployeesEmployeesEmpID=103, EmpName='Carol', DeptID=2Check DeptID=2 in DepartmentsExists - Success
💡 Step 4 fails because DeptID=3 does not exist in Departments, enforcing foreign key constraint.
Variable Tracker
TableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
DepartmentsEmpty[{DeptID:1, DeptName:'HR'}][{DeptID:1, DeptName:'HR'}, {DeptID:2, DeptName:'Sales'}][{DeptID:1, DeptName:'HR'}, {DeptID:2, DeptName:'Sales'}][{DeptID:1, DeptName:'HR'}, {DeptID:2, DeptName:'Sales'}][{DeptID:1, DeptName:'HR'}, {DeptID:2, DeptName:'Sales'}]
EmployeesEmptyEmptyEmpty[{EmpID:101, EmpName:'Alice', DeptID:1}][No change - insert failed][{EmpID:101, EmpName:'Alice', DeptID:1}, {EmpID:103, EmpName:'Carol', DeptID:2}]
Key Moments - 2 Insights
Why did the insert fail at step 4 even though the data looks correct?
Because the foreign key DeptID=3 does not exist in the parent Departments table, the database rejects the insert to keep data consistent. See execution_table row 4.
Does the foreign key column in Employees have to be a primary key?
No, the foreign key column in Employees references the primary key in Departments but does not have to be a primary key itself. It just must match existing values in Departments. See concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of inserting EmpID=102 with DeptID=3?
AFail, because DeptID=3 does not exist in Departments
BSuccess, because DeptID=3 is valid
CSuccess, because foreign keys are optional
DFail, because EmpID=102 already exists
💡 Hint
Check execution_table row 4 for foreign key check and result.
At which step does the Employees table first get a new row inserted?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table rows for Employees insert success.
If we insert a new Department with DeptID=3 before step 4, what would happen at step 4?
AInsert would still fail due to foreign key
BInsert would succeed because DeptID=3 exists
CInsert would fail because DeptID=3 is duplicate
DInsert would succeed but foreign key ignored
💡 Hint
Foreign key checks if referenced DeptID exists in Departments table.
Concept Snapshot
Foreign key links a child table column to a parent table's primary key.
It ensures child values exist in parent to keep data consistent.
Inserts fail if foreign key value not found in parent.
Foreign key column need not be primary key itself.
Used to maintain referential integrity between tables.
Full Transcript
This visual execution shows how foreign keys link two tables in a database. First, a parent table Departments is created with a primary key DeptID. Then a child table Employees is created with a DeptID column referencing Departments.DeptID as a foreign key. When inserting data, Departments accepts new rows freely. Employees can only insert rows if the DeptID value exists in Departments. For example, inserting an employee with DeptID=1 succeeds because Departments has DeptID=1. But inserting with DeptID=3 fails because Departments does not have that value. This enforces referential integrity, preventing orphaned child rows. The variable tracker shows how tables grow after each step. Key moments clarify why inserts fail and foreign key rules. The quiz tests understanding of these steps and constraints. This mental model helps beginners see how foreign keys keep data linked and consistent across tables.