⬅ Previous Topic
Primary Key vs Unique KeyNext Topic ⮕
SQL Performance Tuning⬅ Previous Topic
Primary Key vs Unique KeyNext Topic ⮕
SQL Performance TuningIn the real world, data is connected. Students enroll in subjects, teachers teach courses, and parents contact the school. In relational databases, these connections are managed using foreign keys. A foreign key links one table to another and keeps the relationships between data accurate and trustworthy.
A foreign key is a column (or set of columns) in one table that refers to the primary key of another table. It establishes a link between two tables and enforces referential integrity — meaning you can't insert unrelated or orphaned data.
students
tableCREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10)
);
marks
table with a foreign keyCREATE TABLE marks (
id INT AUTO_INCREMENT PRIMARY KEY,
roll_no INT,
subject VARCHAR(30),
marks INT,
FOREIGN KEY (roll_no) REFERENCES students(roll_no)
);
This means every row in marks
must match an existing roll_no
in students
.
INSERT INTO students VALUES (1, 'Aarav Sharma', '10A');
INSERT INTO marks (roll_no, subject, marks) VALUES (1, 'Maths', 85);
-- Error: roll_no 5 doesn't exist in students table
INSERT INTO marks (roll_no, subject, marks) VALUES (5, 'Science', 90);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
A student can have many marks entries, but each mark entry belongs to one student.
Multiple students can belong to the same class or be assigned to the same teacher.
Students can enroll in many subjects, and subjects can have many students.
CREATE TABLE enrollments (
roll_no INT,
subject_code VARCHAR(10),
PRIMARY KEY (roll_no, subject_code),
FOREIGN KEY (roll_no) REFERENCES students(roll_no),
FOREIGN KEY (subject_code) REFERENCES subjects(subject_code)
);
Foreign keys can respond to changes in the parent table using:
ON DELETE CASCADE
: Deletes dependent rows if parent is deletedON DELETE SET NULL
: Sets FK to NULL if parent is deletedON DELETE RESTRICT
: Prevents deletion if child rows existCREATE TABLE marks (
id INT AUTO_INCREMENT PRIMARY KEY,
roll_no INT,
subject VARCHAR(30),
marks INT,
FOREIGN KEY (roll_no) REFERENCES students(roll_no)
ON DELETE CASCADE
);
CASCADE
only when deletions should flow downForeign Keys are the glue that binds your tables together. They prevent meaningless or broken data and allow you to reflect real-world relationships — like a student's roll number tying all academic records together. In any relational database, using foreign keys smartly is the key to data consistency.
Coming up: SQL Best Practices — coding habits that make your queries cleaner, safer, and easier to maintain.
CREATE TABLE classes (
class_id INT PRIMARY KEY,
class_name VARCHAR(50)
);
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
class_id INT,
FOREIGN KEY (class_id) REFERENCES classes(class_id)
);
⬅ Previous Topic
Primary Key vs Unique KeyNext Topic ⮕
SQL Performance TuningYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.