📖
Theory
Understand the concept step by step
A foreign key is a column or set of columns that references a key in another table.
It is commonly used to establish relationships between related tables and maintain referential integrity.
💻
Example
See how the concept works in SQL
SQL
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT,
FOREIGN KEY (user_id)
REFERENCES users(id)
);📤
Output
Understand what the query produces
user_id references the id column in the users table.
📝
Practice
Test your understanding
Create orders and users tables connected using a foreign key.
❓
MCQs
Check your SQL knowledge
What is the main concept covered in Foreign Key?
A.Creates a relationship between tables
B.Sorts records
C.Deletes records
D.Creates duplicate rows
💼
Interview Questions
Prepare for SQL interviews
1
Interview Question
What is a foreign key?
2
Interview Question
Why are foreign keys used?
3
Interview Question
What is referential integrity?