- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 to add NOT NULL constraint to an already created MySQL column?
Achieve this using ALTER TABLE. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.86 sec)
Let us check the description of the table −
mysql> desc DemoTable;
This will produce the following output −
+-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(100) | YES | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)
Here is the query to add a NOT NULL constraint to the other column “StudentName”, which wasn’t set NOT NULL initially −
mysql> alter table DemoTable modify StudentName varchar(100) NOT NULL; Query OK, 0 rows affected (1.57 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of the table once again −
mysql> desc DemoTable;
This will produce the following output −
+-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(100) | NO | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to add not null constraint to existing column in MySQL?
- How to add a column using MySQL SELECT in an already created table?
- Change a MySQL column to have NOT NULL constraint
- How to add a NOT NULL column in MySQL?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- How to create and fill a new column in an already created MySQL table?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- Add a positive integer constraint to an integer column in MySQL?
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- How to insert auto_increment in an already created table in MySQL?
- MongoDB query to add a document in an already created collection
- Set NOT NULL attribute to an existing column in MySQL
- Does using SERIAL as column name already includes 'NOT NULL' in MySQL?
- How to randomize an already created vector in R?

Advertisements