
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Change a MySQL column to have NOT NULL constraint
To update the constraint, use the MODIFY command. Following is the syntax −
alter table yourTableName modify yourExistingColumnName yourExistingDataType NOT NULL;
Let us first create a table −
mysql> create table DemoTable -> ( -> UserId int NOT NULL AUTO_INCREMENT, -> UserFirstName varchar(100), -> UserLastName varchar(100), -> UserEmailId varchar(100), -> UserPassword varchar(100), -> PRIMARY KEY(UserId) -> ); Query OK, 0 rows affected (0.91 sec)
Following is the query to change the constraint of a column to NOT NULL −
mysql> alter table DemoTable modify UserFirstName varchar(100) NOT NULL; Query OK, 0 rows affected (2.13 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us use the desc command to check whether the column has NOT NULL constraint or not −
mysql> desc DemoTable;
Output
This will produce the following output. Now the UserFirstName column has NOT NULL constraint −
+---------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | | UserFirstName | varchar(100) | NO | | NULL | | | UserLastName | varchar(100) | YES | | NULL | | | UserEmailId | varchar(100) | YES | | NULL | | | UserPassword | varchar(100) | YES | | NULL | | +---------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.17 sec)
- Related Articles
- How to add not null constraint to existing column in MySQL?
- How to add NOT NULL constraint to an already created MySQL column?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- 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 to add a NOT NULL column in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?
- Check for NULL or NOT NULL values in a column in MySQL
- Empty string in not-null column in MySQL?
- Removing NOT NULL restriction from column in MySQL?
- Set NOT NULL attribute to an existing column in MySQL
- Insert default into not null column if value is null in MySQL?

Advertisements