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 there is one important rule: an order should not refer to a customer who does not exist in the database.
This is where a FOREIGN KEY constraint becomes useful.
Let's understand this with a simple SQL example.
- Understanding the Customers Table
Suppose your application already has a "customers" table containing the following records:
customer_id| customer_name
101| Rahul
102| Priya
103| Amit
Here, "customer_id" uniquely identifies each customer.
- Creating an Orders Table Using a FOREIGN KEY
Now, you need to create an "orders" table.
Each order should be associated with an existing customer.
You can create the table using the following SQL statement:
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 important parts:
- "order_id INT PRIMARY KEY" uniquely identifies each order.
- "customer_id INT" stores the customer ID associated with the order.
- "FOREIGN KEY (customer_id)" defines the foreign key constraint.
- "REFERENCES customers(customer_id)" connects the column to the customer ID in the customers table.
The "customers" table is the parent table, and the "orders" table is the child table.
- Trying to Insert an Order With a Non-Existing Customer ID
Now, imagine a developer tries to insert a new order using customer ID 105.
Here is the INSERT statement:
INSERT INTO orders (
order_id,
customer_id,
order_date
)
VALUES (1001, 105, '2026-11-29');
But look at the existing customer records.
The customers table contains only these IDs:
- 101 – Rahul
- 102 – Priya
- 103 – Amit
There is no customer with ID 105.
What do you think will happen?
The INSERT statement will fail because customer ID 105 does not exist in the referenced customers table.
The FOREIGN KEY constraint prevents this invalid relationship from being stored.
The exact error message depends on the database system, but the reason for the failure is the same.
- SQL FOREIGN KEY Interview Challenge
Let's check your understanding.
What will happen when this INSERT statement is executed?
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.
Your turn: Which option is correct – A, B, C or D?
Think about how a FOREIGN KEY works before checking the answer.
- How Can You Make the INSERT Successful?
If customer ID 105 is a valid new customer, first insert that customer into the parent table.
For example:
INSERT INTO customers (
customer_id,
customer_name
)
VALUES (105, 'Sneha');
Now, try inserting the order again:
INSERT INTO orders (
order_id,
customer_id,
order_date
)
VALUES (1001, 105, '2026-11-29');
This time, the INSERT statement can succeed because customer ID 105 now exists in the customers table.
- Important Things to Remember About FOREIGN KEY
Here are a few points worth remembering:
- A FOREIGN KEY connects records between two related tables.
- It helps maintain referential integrity in a database.
- A FOREIGN KEY value must match an existing referenced key value, unless it is NULL and the column allows NULL.
- A FOREIGN KEY does not automatically create missing records in the parent table.
A table can contain multiple foreign keys referencing different tables.
PRIMARY KEY vs FOREIGN KEY
Both constraints are important when designing relational databases, but they serve different purposes.
PRIMARY KEY| FOREIGN KEY
Uniquely identifies a record.| Connects records between tables.
Does not allow duplicate values.| Can contain repeated values.
Does not allow NULL values.| Can allow NULL values if the column permits them.
Identifies a record in its own table.| References a key in another or the same table.
Practical example:
In an e-commerce database:
- "customers.customer_id" can be a PRIMARY KEY.
- "orders.order_id" can be a PRIMARY KEY.
- "orders.customer_id" can be a FOREIGN KEY referencing "customers.customer_id".
This relationship helps ensure that orders are associated with valid customers.
Conclusion
A FOREIGN KEY is not just another database constraint. It helps keep related data consistent across tables.
Whenever you design a database involving customers and orders, employees and departments, or students and courses, understanding PRIMARY KEY and FOREIGN KEY constraints is important.
If you are learning SQL DDL, table creation, table relationships and other database constraints, explore these practical examples and explanations:
"SQL DDL – Managing Tables and Relationships: Practical Examples and Interview Questions" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html)
Have you ever faced a FOREIGN KEY constraint error while inserting records? Share your experience or questions in the comments.

Top comments (0)