Removing NOT NULL restriction from column in MySQL?
Answers
We can remove NOT NULL constraint from a column of an existing table by using ALTER TABLE statement.
Example
Suppose we have a table ‘test123’ having a NOT NULL constraint on column ‘ID’ as follows:
mysql> DESCRIBE test123;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| ID | int(11) | NO | | NULL | |
| Date | date | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.04 sec)
Now if we want to remove the NOT NULL constraint then we can use ALTER TABLE statement as follows:
mysql> ALTER TABLE test123 MODIFY ID INT NULL;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESCRIBE test123;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------ +---------+------+-----+---------+-------+
| ID | int(11) | YES | | NULL | |
| Date | date | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.06 sec)
The above result set shows that NOT NULL constraint on column ‘ID’ has been removed.
In the query above, the keyword NULL after keyword MODIFY is optional. The following query will also produce the same result as above:
mysql> ALTER TABLE test123 MODIFY ID INT;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
Comments Edit Answer Report
Would you like to add a better answer?
Advertisements
img img img img img img
Ask Question
FOLLOW
Tutorials Point
© Copyright 2019. All Rights Reserved.
NEWSLETTER
Enter email for newsletter
go
Cookies Policy FAQ's Helping Contact
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.