- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 apply a NOT NULL constraint to a column of an existing MySQL table?
We can apply the NOT NULL constraint to a column of an existing MySQL table with the help of ALTER TABLE statement.
Syntax
ALTER TABLE table_name MODIFY colum_name datatype NOT NULL;
Example
mysql> Create table test123(ID INT, Date DATE); Query OK, 0 rows affected (0.19 sec) mysql> Describe test123; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | Date | date | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.04 sec) mysql> ALTER TABLE test123 MODIFY ID INT NOT NULL; Query OK, 0 rows affected (0.54 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> Describe test123; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | ID | int(11) | NO | | NULL | | | Date | date | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.06 sec)
- Related Articles
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we apply UNIQUE constraint to the field 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 can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How to add not null constraint to existing column in MySQL?
- What happens when we apply NOT NULL constraint, with ALTER TABLE statement, to a column contains NULL values?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- Change a MySQL column to have NOT NULL constraint
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- Adding a new NOT NULL column to an existing table with records
- How to add NOT NULL constraint to an already created MySQL column?
- How can we put comments in a column of existing MySQL table?
- What is MySQL UNIQUE constraint and how can we apply it to the field of a table?

Advertisements