

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we remove NOT NULL constraint from a column of an existing MySQL table?
We can remove a NOT NULL constraint from a column of an existing table by using the 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
- Related Questions & Answers
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How to add not null constraint to existing column in MySQL?
- How can we remove a column from MySQL table?
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- How can we apply UNIQUE constraint to the field of an existing MySQL table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can I drop an existing column from a MySQL table?
- How can we drop UNIQUE constraint from a MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How can we put comments in a column of existing MySQL table?
- Change a MySQL column to have NOT NULL constraint
- How can we create a table from an existing MySQL table in the database?
Advertisements