In MySQL, a **foreign key constraint is not enforced if any column in the foreign key is `NULL`**.
That means **if at least one column of a composite key is `NULL`**, MySQL **ignores the foreign key check** and allows the insert.
---
### Example: Composite Primary Key with Foreign Key
```sql
-- Parent table with a composite primary key
CREATE TABLE tab1 (
id INT,
name VARCHAR(20),
cc VARCHAR(20),
PRIMARY KEY (id, name)
);
DESCRIBE tab1;
------------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
| ----- | ----------- | ---- | --- | ------- | ----- |
| id | int | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| cc | varchar(20) | YES | | NULL | |
------------------------------------------------------
-- Child table referencing the composite key of tab1
CREATE TABLE tab2 (
id INT,
name VARCHAR(25),
tid INT PRIMARY KEY,
FOREIGN KEY (id, name) REFERENCES tab1(id, name)
);
DESCRIBE tab2;
------------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
| ----- | ----------- | ---- | --- | ------- | ----- |
| id | int | YES | MUL | NULL | |
| name | varchar(25) | YES | | NULL | |
| tid | int | NO | PRI | NULL | |
------------------------------------------------------
-- Inserting NULL for both foreign key columns
INSERT INTO tab2 VALUES (NULL, NULL, 35);
SELECT * FROM tab2;
---------------------
| id | name | tid |
| ---- | ---- | --- |
| NULL | NULL | 35 |
---------------------
-- Inserting NULL for one column
INSERT INTO tab2 VALUES (1, NULL, 36);
INSERT INTO tab2 VALUES (10, NULL, 39);
SELECT * FROM tab2;
---------------------
| id | name | tid |
| ---- | ---- | --- |
| NULL | NULL | 35 |
| 1 | NULL | 36 |
| 10 | NULL | 39 |
---------------------
-- Inserting a fully non-null foreign key that doesn't exist in parent table
INSERT INTO tab2 VALUES (10, 'HELLO', 40);
-- ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
-- Inserting with one NULL column bypasses the foreign key check
INSERT INTO tab2 VALUES (NULL, 'HELLO', 40);
SELECT * FROM tab2;
----------------------
| id | name | tid |
| ---- | ----- | --- |
| NULL | NULL | 35 |
| NULL | HELLO | 40 |
| 1 | NULL | 36 |
| 10 | NULL | 39 |
----------------------
```
NULL in Foreign Key: MySQL allows NULL values in foreign key columns. If any column in a composite foreign key is NULL, the foreign key constraint is ignored for that row.
Composite Keys: All columns must match a parent key to enforce the constraint. If one is NULL, the check is skipped.
Enforcement Happens Only When Fully Non-NULL: Only when all columns of a composite foreign key are non-NULL does MySQL check the existence of the referenced parent row.
In MySQL, a **foreign key constraint is not enforced if any column in the foreign key is `NULL`**.
That means **if at least one column of a composite key is `NULL`**, MySQL **ignores the foreign key check** and allows the insert.
---
### Example: Composite Primary Key with Foreign Key
```sql
-- Parent table with a composite primary key
CREATE TABLE tab1 (
id INT,
name VARCHAR(20),
cc VARCHAR(20),
PRIMARY KEY (id, name)
);
DESCRIBE tab1;
------------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
| ----- | ----------- | ---- | --- | ------- | ----- |
| id | int | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| cc | varchar(20) | YES | | NULL | |
------------------------------------------------------
-- Child table referencing the composite key of tab1
CREATE TABLE tab2 (
id INT,
name VARCHAR(25),
tid INT PRIMARY KEY,
FOREIGN KEY (id, name) REFERENCES tab1(id, name)
);
DESCRIBE tab2;
------------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
| ----- | ----------- | ---- | --- | ------- | ----- |
| id | int | YES | MUL | NULL | |
| name | varchar(25) | YES | | NULL | |
| tid | int | NO | PRI | NULL | |
------------------------------------------------------
-- Inserting NULL for both foreign key columns
INSERT INTO tab2 VALUES (NULL, NULL, 35);
SELECT * FROM tab2;
---------------------
| id | name | tid |
| ---- | ---- | --- |
| NULL | NULL | 35 |
---------------------
-- Inserting NULL for one column
INSERT INTO tab2 VALUES (1, NULL, 36);
INSERT INTO tab2 VALUES (10, NULL, 39);
SELECT * FROM tab2;
---------------------
| id | name | tid |
| ---- | ---- | --- |
| NULL | NULL | 35 |
| 1 | NULL | 36 |
| 10 | NULL | 39 |
---------------------
-- Inserting a fully non-null foreign key that doesn't exist in parent table
INSERT INTO tab2 VALUES (10, 'HELLO', 40);
-- ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
-- Inserting with one NULL column bypasses the foreign key check
INSERT INTO tab2 VALUES (NULL, 'HELLO', 40);
SELECT * FROM tab2;
----------------------
| id | name | tid |
| ---- | ----- | --- |
| NULL | NULL | 35 |
| NULL | HELLO | 40 |
| 1 | NULL | 36 |
| 10 | NULL | 39 |
----------------------
```
NULL in Foreign Key: MySQL allows NULL values in foreign key columns. If any column in a composite foreign key is NULL, the foreign key constraint is ignored for that row.
Composite Keys: All columns must match a parent key to enforce the constraint. If one is NULL, the check is skipped.
Enforcement Happens Only When Fully Non-NULL: Only when all columns of a composite foreign key are non-NULL does MySQL check the existence of the referenced parent row.