Imagine you are working on an e-commerce application.

Whenever a customer places an order, the database needs to store the order ID, customer ID and order date.
But how do you make sure that every order belongs to an existing customer?
What happens if a developer tries to insert an order using a customer ID that does not exist in the Customers table?
This is where the FOREIGN KEY constraint in SQL becomes important.
In this tutorial, we will understand FOREIGN KEY constraints using a simple e-commerce example. We will also look at CREATE TABLE, INSERT statements, parent and child tables, referential integrity and common SQL interview questions.
- What Is a FOREIGN KEY in SQL?
A FOREIGN KEY is a constraint used to establish a relationship between two tables.
It ensures that a value in one table matches an existing value in the referenced table.
For example, consider an e-commerce database with two tables:
- Customers: Stores customer information.
- Orders: Stores customer orders.
The Customers table is the parent table, and the Orders table is the child table.
The "customer_id" column in the Orders table references the "customer_id" column in the Customers table.
This relationship helps maintain referential integrity.
- Understand the Customers Table
Suppose the Customers table already contains the following records:
customer_id| customer_name
101| Rahul
102| Priya
103| Amit
Here, "customer_id" uniquely identifies each customer.
Let's assume that "customer_id" is the PRIMARY KEY of the Customers table.
- Create an Orders Table Using a FOREIGN KEY
Now, we need to create an Orders table that references the Customers table.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
);
Let's understand the columns:
- "order_id": Uniquely identifies each order.
- "customer_id": Stores the customer associated with the order.
- "order_date": Stores the date of the order.
- "FOREIGN KEY (customer_id)": Defines the foreign key column.
- "REFERENCES customers(customer_id)": Establishes the relationship with the Customers table.
The referenced column must be a PRIMARY KEY or have a UNIQUE constraint.
- Insert a Valid Record Into the Orders Table
Let's insert an order for customer ID 101.
INSERT INTO orders (
order_id,
customer_id,
order_date
)
VALUES (1001, 101, DATE '2026-11-29');
Will this INSERT statement execute successfully?
Yes.
Customer ID 101 already exists in the Customers table.
Therefore, the FOREIGN KEY constraint allows this record to be inserted.
Expected Orders table:
order_id| customer_id| order_date
1001| 101| 2026-11-29
- What Happens When the Customer ID Does Not Exist?
Now, let's try inserting another order using customer ID 105.
INSERT INTO orders (
order_id,
customer_id,
order_date
)
VALUES (1002, 105, DATE '2026-11-29');
What will happen?
The INSERT statement will fail because customer ID 105 does not exist in the Customers table.
The FOREIGN KEY constraint prevents the database from inserting an order that refers to a non-existing customer.
The database reports a foreign key constraint violation.
This is how a FOREIGN KEY helps maintain referential integrity between related tables.
- Why Do We Need FOREIGN KEY Constraints?
Imagine an e-commerce application where orders can be created using customer IDs that do not exist.
You may end up with orders that cannot be associated with any customer.
This can create problems in:
- Customer order history
- Sales reports
- Customer purchase analysis
- Billing and payment systems
- E-commerce applications
A FOREIGN KEY helps prevent these invalid relationships.
Here are some common examples:
Parent table| Child table| Foreign key
Customers| Orders| customer_id
Departments| Employees| department_id
Students| Enrollments| student_id
Products| Order_Details| product_id
- What Happens When You Delete a Parent Record?
This is another important concept in FOREIGN KEY constraints.
Suppose customer ID 101 has already placed three orders.
Now, a developer executes:
DELETE FROM customers
WHERE customer_id = 101;
By default, if related records exist in the Orders table, the DELETE statement will fail.
Why?
Because deleting the customer would leave the existing orders referring to a customer who no longer exists.
The FOREIGN KEY constraint prevents this operation unless an appropriate referential action, such as "ON DELETE CASCADE", has been defined.
- SQL Interview Challenge
Let's test your understanding.
The Customers table contains:
customer_id| customer_name
101| Rahul
102| Priya
103| Amit
The Orders table has a FOREIGN KEY referencing "customers(customer_id)".
A developer executes:
INSERT INTO orders (
order_id,
customer_id,
order_date
)
VALUES (1001, 105, DATE '2026-11-29');
What will happen?
A. The new order will be inserted successfully because order_id 1001 is unique.
B. The INSERT statement will fail because customer_id 105 does not exist in the Customers table.
C. SQL will automatically create customer_id 105 in the Customers table.
D. The existing customer with ID 103 will be assigned to this order.
Correct Answer: B
Explanation:
The customer ID 105 does not exist in the parent Customers table.
Therefore, the FOREIGN KEY constraint rejects the INSERT statement.
SQL will not automatically create a missing customer or replace the invalid customer ID with another existing ID.
- Key Points to Remember
- A FOREIGN KEY establishes a relationship between two tables.
- The parent table contains the referenced PRIMARY KEY or UNIQUE key.
- The child table contains the FOREIGN KEY.
- A FOREIGN KEY prevents invalid references.
- It helps maintain referential integrity.
- It controls how related records are handled when parent records are updated or deleted, depending on the defined rules.
Conclusion
FOREIGN KEY is an important SQL constraint used in relational database design.
In real-world applications, tables are connected through relationships. Customers place orders, employees belong to departments, and students register for courses.
Understanding FOREIGN KEY constraints helps you maintain data consistency and design reliable relational databases.
If you are preparing for SQL interviews or working with relational databases, make sure you practise PRIMARY KEY and FOREIGN KEY constraints with real table examples.
Learn More About SQL DDL and Database Relationships
For more practical SQL examples, tutorials and interview questions covering CREATE TABLE, ALTER TABLE, PRIMARY KEY, FOREIGN KEY and other database constraints, explore:
"SQL DDL – Managing Tables and Relationships" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html)
Top comments (0)