
- 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
Removing NOT NULL restriction from column in MySQL?
To remove NOT NULL restriction from column in MySQL, use ALTER command. The syntax is as follows:
ALTER TABLE yourTableName MODIFY COLUMN yourColumnName dataType;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table NotNullDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20) NOT NULL, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.58 sec)
In the above table, if you insert the NULL value to the column ‘Name’ then MySQL will give an error of NOT NULL restriction. The query to insert NULL value is as follows:
mysql> insert into NotNullDemo(Name) values(NULL); ERROR 1048 (23000): Column 'Name' cannot be null
Here is the query to remove NOT NULL restriction from column ‘Name’:
mysql> alter table NotNullDemo MODIFY COLUMN Name varchar(20); Query OK, 0 rows affected (1.78 sec)
Records: 0 Duplicates: 0 Warnings: 0
Now you can insert NULL values as per your choice. The query to insert record in the table with NULL value:
mysql> insert into removeNotNulllDemo(Name) values(NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into removeNotNulllDemo(Name) values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into removeNotNulllDemo(Name) values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into removeNotNulllDemo(Name) values(NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into removeNotNulllDemo(Name) values(NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into removeNotNulllDemo(Name) values('John'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from removeNotNulllDemo;
The following is the output:
+----+------+ | Id | Name | +----+------+ | 1 | NULL | | 2 | Sam | | 3 | Mike | | 4 | NULL | | 5 | NULL | | 6 | John | +----+------+ 6 rows in set (0.00 sec)
- Related Articles
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- SELECT not null column from two columns in MySQL?
- Empty string in not-null column in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- Insert default into not null column if value is null in MySQL?
- How to add a NOT NULL column in MySQL?
- Set NOT NULL attribute to an existing column in MySQL
- Change a MySQL column to have NOT NULL constraint
- How to add not null constraint to existing column in MySQL?
- Update one column data to another column in MySQL if the second column is NOT NULL?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- Alter a table column from VARCHAR to NULL in MySQL
- Adding a column whose value is not null by default in MySQL?
- Working with NULL and IS NOT NULL in MySQL
- How to add NOT NULL constraint to an already created MySQL column?

Advertisements