MySQL 8.4Lab-verified · 2026-11-30

MySQL ERROR 1138 (22004): Invalid use of NULL value

ALTER TABLE tried to make a column NOT NULL while some rows still have NULL in it.

MySQL 8.4.13
1138 (22004): Invalid use of NULL value

Jump to a cause

  1. 01MODIFY column NOT NULL while NULL rows exist
  2. 02ADD PRIMARY KEY on a column with NULL

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4

ERROR 1138 appears on ALTER TABLE when the new definition does not allow NULL but existing rows contain NULL. It is not about the statement syntax; the data blocks the change. The message does not say which rows, so find them with WHERE col IS NULL first.

Below are two cases reproduced on MySQL 8.4 with the fixes that were actually run.

01MODIFY column NOT NULL while NULL rows exist

customers.phone had NULL for Lee and Park. ALTER TABLE customers MODIFY phone VARCHAR(20) NOT NULL failed with ERROR 1138.

SELECT id, name FROM customers WHERE phone IS NULL found the 2 rows. After UPDATE customers SET phone = '' WHERE phone IS NULL (2 rows changed), the same ALTER succeeded.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(30), phone VARCHAR(20));
INSERT INTO customers VALUES (1, 'Kim', '010-1111-2222'), (2, 'Lee', NULL), (3, 'Park', NULL);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
ALTER TABLE customers MODIFY phone VARCHAR(20) NOT NULL;
✕ ERROR
ERROR 1138 (22004): Invalid use of NULL value
Checkrun as lab_a
SELECT id, name FROM customers WHERE phone IS NULL;
OUTPUT
+----+------+
| id | name |
+----+------+
|  2 | Lee  |
|  3 | Park |
+----+------+
2 rows in set (0.00 sec)
Fixrun as lab_a
UPDATE customers SET phone = '' WHERE phone IS NULL;
OUTPUT
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

Pick the replacement value for your data (empty string, a default, or a real value). If the NULLs are meaningful, keep the column nullable instead.

After the fixrun as lab_a
ALTER TABLE customers MODIFY phone VARCHAR(20) NOT NULL;
SELECT id, name, phone FROM customers;
✓ SUCCESS
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

+----+------+---------------+
| id | name | phone         |
+----+------+---------------+
|  1 | Kim  | 010-1111-2222 |
|  2 | Lee  |               |
|  3 | Park |               |
+----+------+---------------+
3 rows in set (0.00 sec)

02ADD PRIMARY KEY on a column with NULL

Primary key columns are always NOT NULL. order_items had a row with line_no NULL (order 101, paper), so ALTER TABLE order_items ADD PRIMARY KEY (order_id, line_no) failed with ERROR 1138.

After setting that row to line_no = 1, the ALTER succeeded and SHOW KEYS listed PRIMARY on order_id and line_no.

Setup
Setuprun as lab_a
CREATE TABLE order_items (order_id INT, line_no INT, item VARCHAR(20));
INSERT INTO order_items VALUES (100, 1, 'pen'), (100, 2, 'ink'), (101, NULL, 'paper');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
ALTER TABLE order_items ADD PRIMARY KEY (order_id, line_no);
✕ ERROR
ERROR 1138 (22004): Invalid use of NULL value
Checkrun as lab_a
SELECT order_id, line_no, item FROM order_items WHERE order_id IS NULL OR line_no IS NULL;
OUTPUT
+----------+---------+-------+
| order_id | line_no | item  |
+----------+---------+-------+
|      101 |    NULL | paper |
+----------+---------+-------+
1 row in set (0.00 sec)
Fixrun as lab_a
UPDATE order_items SET line_no = 1 WHERE order_id = 101 AND line_no IS NULL;
OUTPUT
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
After the fixrun as lab_a
ALTER TABLE order_items ADD PRIMARY KEY (order_id, line_no);
SHOW KEYS FROM order_items WHERE Key_name = 'PRIMARY';
✓ SUCCESS
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| order_items |          0 | PRIMARY  |            1 | order_id    | A         |           2 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| order_items |          0 | PRIMARY  |            2 | line_no     | A         |           3 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.00 sec)

FAQ

What does MySQL Invalid use of NULL value mean?

An ALTER TABLE needs a column to be NOT NULL (MODIFY ... NOT NULL or a primary key), but some rows have NULL in that column.

How do I find the rows causing ERROR 1138?

Run SELECT ... WHERE column IS NULL for each column in the change. In the primary key case, check every key column.

Can I add NOT NULL with a DEFAULT to fix it?

Adding DEFAULT does not change existing NULL rows. In both cases the ALTER succeeded only after UPDATE filled the NULLs.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13