In databases, foreign keys are like links connecting two tables. They make sure that data in one table matches data in another. But a common question is whether a foreign key can be NULL.
In this article, We will explain what NULL values mean for foreign keys, how they work with the help of examples and so on.
What is Foreign Key?
- A foreign key is a column (or a set of columns) in one table that uniquely identifies rows in another table.
- It establishes a link between the tables, ensuring that the data remains consistent.
- In MySQL, foreign keys are used to enforce referential integrity, meaning that they ensure the values in the foreign key column match the values in the primary key column of the referenced table.
Syntax-
CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
What is a NULL Value?
In databases, NULL means no data or an unknown value. It's different from an empty string or a zero. When a foreign key has a NULL, it means there is no link to a record in the other table.
How NULL Values Affect Foreign Keys
- Optional Relationships:
NULLallows for optional links. For example, an order might not have a customer assigned yet, or a product might not have a supplier. - Referential Integrity: Foreign key rules ensure that any non-
NULLvalue must match a record in the other table.NULLvalues are allowed because they don’t need to match anything. - Queries and Joins: When we query tables or join them based on foreign keys, we need to handle
NULLvalues carefully.NULLvalues will not match with any other value unless specifically handled.
Example 1: Customer and Orders
Let’s say we have two tables which are Customers and Orders.
CREATE TABLE Customers (
CustomerID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT PRIMARY KEY,
OrderDate DATE,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Output:
TABLE CREATED SUCCESFULLYInsert Data into Customers Table -
INSERT INTO Customers (Name) VALUES ('John Doe'), ('Jane Smith');Verify Data in Customers Table -
SELECT * FROM Customers;Output:

Insert Data into Orders Table -
INSERT INTO Orders (OrderDate, CustomerID) VALUES ('2024-07-31', 1), ('2024-07-31', NULL);Verify Data in Orders Table -
SELECT * FROM Orders;Output:

Explanation of the output -
- The first order is associated with
John Doe(CustomerID = 1). - The second order has
NULLforCustomerID, indicating that it is not linked to any customer.
Example 2: Product and Supplier
Consider two tables -Suppliers and Products.
Create the Tables
CREATE TABLE Suppliers (
SupplierID INT AUTO_INCREMENT PRIMARY KEY,
SupplierName VARCHAR(100)
);
CREATE TABLE Products (
ProductID INT AUTO_INCREMENT PRIMARY KEY,
ProductName VARCHAR(100),
SupplierID INT,
FOREIGN KEY (SupplierID) REFERENCES Suppliers(SupplierID)
);
Output:
TABLE CREATED SUCCESFULLYInsert Data into Suppliers Table -
INSERT INTO Suppliers (SupplierName) VALUES ('Acme Corp'), ('Global Supplies');Verify Data in Suppliers Table -
SELECT * FROM Suppliers;Output:

Insert Data into Products -
INSERT INTO Products (ProductName, SupplierID) VALUES ('Laptop', 1), ('Mouse', NULL);Verify Data in Products Table -
SELECT * FROM Products ;Output:

Explanation of the output -
- The
Laptopproduct is linked toAcme Corp(SupplierID = 1). - The
Mouseproduct hasNULLforSupplierID, indicating no current supplier.
Implications of Allowing NULL Foreign Keys
Allowing NULL values in foreign keys can impact your database in several ways:
NULLvalues don’t violate rules but can affect how data is interpreted. Clearly define whatNULLrepresents in your database to avoid confusion.NULLvalues indicate optional or missing links. Ensure your application handles these cases correctly to avoid errors.- Queries involving
NULLcan be more complex. Ensure your SQL queries handleNULLvalues properly for accurate results.
Performance Considerations
Foreign keys with NULL values typically don’t affect performance significantly, but it affect -
- Index foreign key columns to speed up queries and joins. Proper indexing can enhance performance.
- Foreign key constraints add overhead during data changes, but this isn’t affected by
NULLvalues. Good indexing and schema design can help mitigate performance issues. - In large databases with many
NULLvalues, performance may be impacted. Optimize your queries and database design to handle large data volumes effectively.
Conclusion
In MySQL, foreign keys can include NULL values, allowing for flexible table relationships. Understanding and managing NULL values helps in designing effective databases and maintaining data integrity.