mysqldude

Remove NOT NULL constraint from a table

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
ALTER TABLE ... MODIFY col ... NULL (original)
original post
✓✓✓✓✓✓✓✓✓
Back to NOT NULL when no NULLs exist (original)
original post
✓✓✓✓✓✓✓✓✓
MODIFY ... NOT NULL while NULLs exist (5.6 non-strict silently turns NULL into '' with warning 1265)
version check
≠✓ 1138✓ 1138✓ 1138✓ 1138✓ 1265✓ 1265✓ 1265✓ 1265
MODIFY restates the whole column: an omitted DEFAULT/COMMENT is lost
version check
✓✓✓✓✓✓✓✓✓
ALTER TABLE ... MODIFY col ... NULL (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Back to NOT NULL when no NULLs exist (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
MODIFY ... NOT NULL while NULLs exist (5.6 non-strict silently turns NULL into '' with warning 1265) version check
MySQL 5.6≠5.7✓ 11388.0✓ 11388.4✓ 11389✓ 1138
MariaDB 10.6✓ 126510.11✓ 126511.4✓ 126511.8✓ 1265
MODIFY restates the whole column: an omitted DEFAULT/COMMENT is lost version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (1)
  • MODIFY ... NOT NULL while NULLs exist (5.6 non-strict silently turns NULL into '' with warning 1265) (MySQL 5.6): expected error [1138, 1265] did not occur

Related errors: 1138, 1265

The original article

First published 2012-02-27. The text below is the original; the table above shows how it behaves on current versions.

MySQL allows table columns to be created with the NOT NULL constraint. This implies that the field needs to be populated with a value that cannot be the NULL value.

In some cases we may need to remove this constraint. This is achieved using the following command:

ALTER TABLE table MODIFY field datatype NULL;

For example lets image we have a table which contains a NOT NULL field and we wish to convert this to NULL. The table name in question is youtube and the field videourl:

mysql> desc youtube;
+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| id       | int(11)       | NO   | PRI | NULL    | auto_increment |
| videourl | varchar(1024) | NO   |     |         |                |
| thumburl | varchar(1024) | YES  |     | NULL    |                |
| title    | varchar(1024) | YES  |     | NULL    |                |
| content  | varchar(1024) | YES  |     | NULL    |                |
+----------+---------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> ALTER TABLE youtube MODIFY videourl VARCHAR(1024) NULL;
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> desc youtube;
+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| id       | int(11)       | NO   | PRI | NULL    | auto_increment |
| videourl | varchar(1024) | YES  |     | NULL    |                |
| thumburl | varchar(1024) | YES  |     | NULL    |                |
| title    | varchar(1024) | YES  |     | NULL    |                |
| content  | varchar(1024) | YES  |     | NULL    |                |
+----------+---------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

The reverse operation may also be performed, changing from a NULL to NOT NULL constraint. However, if the field contains a NULL value then this operation will fail with an error. To determine whether a NULL value is in the field issue the following command:

mysql> select * from youtube where videourl is null;
Empty set (0.00 sec)

We are now sure that the operation will happen correctly so we now issue the ALTER TABLE command:

mysql> ALTER TABLE youtube MODIFY videourl VARCHAR(1024) NOT NULL;
Query OK, 7 rows affected (0.06 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> desc youtube;
+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| id       | int(11)       | NO   | PRI | NULL    | auto_increment |
| videourl | varchar(1024) | NO   |     |         |                |
| thumburl | varchar(1024) | YES  |     | NULL    |                |
| title    | varchar(1024) | YES  |     | NULL    |                |
| content  | varchar(1024) | YES  |     | NULL    |                |
+----------+---------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)