Skip to main content

SQL FOREIGN KEY Constraint: Why Does an INSERT Statement Fail? (With Practical Example)

Updated 6 min readMarkdown

Imagine you are working on an e-commerce application.

SQL FOREIGN KEY constraint example showing Customers and Orders tables, CREATE TABLE statement, INSERT operation, referential integrity and SQL database constraints with a practical e-commerce scenario.

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 an order is always associated with an existing customer?

For example, what happens if someone 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, you will learn how a FOREIGN KEY works, how to create a relationship between two tables, and why SQL rejects an INSERT statement when the referenced customer does not exist.

  1. What Is a FOREIGN KEY in SQL?

A FOREIGN KEY is a database constraint that establishes a relationship between two tables.

It ensures that a value in one table refers to an existing value in another table.

Consider an e-commerce application with two tables:

  • Customers: Stores customer information.
  • Orders: Stores orders placed by customers.

The Customers table is called the parent table, while the Orders table is called 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 in a relational database.

  1. Understand the Existing Customers Table

Suppose the Customers table already contains these records:

customer_id| customer_name 101| Rahul 102| Priya 103| Amit

Here, each customer has a unique customer ID.

Let's assume that "customer_id" is the PRIMARY KEY of the Customers table.

  1. Create the 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 important parts:

  • "order_id INT PRIMARY KEY": Uniquely identifies each order.
  • "customer_id INT": Stores the customer ID associated with an order.
  • "order_date DATE": Stores the order date.
  • "FOREIGN KEY (customer_id)": Defines the foreign key column.
  • "REFERENCES customers(customer_id)": Connects the customer_id column to the parent table.

Important: The referenced column in the parent table must be a PRIMARY KEY or have a UNIQUE constraint.

  1. Insert a Valid Record Into the Orders Table

Let's try inserting an order for customer ID 101.

INSERT INTO orders ( order_id, customer_id, order_date ) VALUES (1001, 101, DATE '2026-11-29');

What happens?

The INSERT statement executes successfully.

Why?

Because customer ID 101 already exists in the Customers table.

The FOREIGN KEY constraint allows the insertion because the referenced customer exists.

Expected Orders table:

order_id| customer_id| order_date 1001| 101| 2026-11-29

  1. 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');

Will this INSERT statement execute successfully?

No.

The INSERT statement fails because customer ID 105 does not exist in the Customers table.

The FOREIGN KEY constraint prevents the database from inserting an order that references a non-existing customer.

The database reports a foreign key constraint violation.

This is exactly how referential integrity protects relationships between tables.

  1. Why Is the FOREIGN KEY Constraint Important?

Consider what could happen if a database allowed orders to reference customer IDs that did not exist.

You might find orders that cannot be linked to 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

  1. Can You Delete a Customer Who Has Existing Orders?

There is another important point to understand.

Suppose customer ID 101 has already placed three orders.

What happens if you execute:

DELETE FROM customers WHERE customer_id = 101;

By default, when matching child records exist and no cascading delete rule is defined, the DELETE statement is rejected because deleting the parent record would leave related child records without a valid parent.

This is another way a FOREIGN KEY helps maintain referential integrity.

  1. SQL Interview Challenge: Test Your Understanding

Let's check whether you understand how a FOREIGN KEY works.

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" value 105 does not exist in the parent Customers table.

Therefore, the FOREIGN KEY constraint rejects the INSERT statement.

Remember, a FOREIGN KEY does not automatically create missing parent records or replace an invalid customer ID with an existing one.

  1. 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 inserting non-matching references.
  • It helps maintain referential integrity.
  • A FOREIGN KEY can also control what happens when referenced parent records are updated or deleted, depending on the defined referential actions.

Conclusion

FOREIGN KEY is one of the most important constraints in SQL database design.

In real-world applications, tables rarely work independently. Customers have orders, employees belong to departments, and students register for courses.

FOREIGN KEY constraints help maintain these relationships and prevent invalid references between related tables.

If you are learning SQL, understanding PRIMARY KEY and FOREIGN KEY together will help you design better relational databases and solve practical SQL interview questions.

Learn More About SQL DDL and Database Constraints

For more practical SQL examples, explanations and interview questions covering CREATE TABLE, ALTER TABLE, PRIMARY KEY, FOREIGN KEY and other database concepts, explore the following learning resource:

"SQL DDL – Managing Tables and Relationships" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html)

You can use these examples to practise SQL table creation, database relationships and constraints step by step.


Tags: SQL, Database, SQL Constraints, Foreign Key, SQL Tutorial

No comments yet. Be the first to comment.