
- 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
How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
It is quite possible to insert the NULL keyword as a value in a character type column having NOT NULL constraint because NULL is a value in itself. Following example will exhibit it −
Example
Suppose we have a table test2 having character type column ‘Name’ along with NOT NULL constraint on it. It can be checked from the DESCRIBE statement as follows −
mysql> Describe test2\G *************************** 1. row *************************** Field: id Type: int(11) Null: NO Key: Default: NULL Extra: *************************** 2. row *************************** Field: NAME Type: varchar(20) Null: NO Key: Default: NULL Extra: 2 rows in set (0.03 sec)
Now, with the help of the following query, we can insert NULL as a value in the column ‘Name’.
mysql> Insert into test2 values(2, 'NULL'); Query OK, 1 row affected (0.06 sec) mysql> select * from test2; +----+--------+ | id | NAME | +----+--------+ | 1 | Gaurav | | 2 | NULL | +----+--------+ 2 rows in set (0.00 sec)
To understand the difference between ‘NULL’ and ‘NULL as a value’ we can run the following two queries −
mysql> delete from test2 where name IS NULL; Query OK, 0 rows affected (0.00 sec)
The above query affects 0 rows it means there are no NULL rows. It can be checked from the SELECT query that no rows have been deleted.
mysql> select * from test2; +----+--------+ | id | NAME | +----+--------+ | 1 | Gaurav | | 2 | NULL | +----+--------+ 2 rows in set (0.00 sec) mysql> delete from test2 where name = 'NULL'; Query OK, 1 row affected (0.09 sec)
The above query affects 1 row it means there is a row having NULL as a value. It can be checked from SELECT query that row which had NULL as a value in the ‘NAME’ column has been deleted.
mysql> select * from test2; +----+--------+ | id | NAME | +----+--------+ | 1 | Gaurav | +----+--------+ 1 row in set (0.00 sec)
- Related Articles
- Change a MySQL column to have NOT NULL constraint
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- Insert default into not null column if value is null in MySQL?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How to add not null constraint to existing column in MySQL?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- How to add NOT NULL constraint to an already created MySQL column?
- What happens when we apply NOT NULL constraint, with ALTER TABLE statement, to a column contains NULL values?
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?
- Insert NULL value into INT column in MySQL?
- Insert NULL value into database field with char(2) as type in MySQL?
- How to add a NOT NULL column in MySQL?
- How do I insert a NULL value in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- How to check whether column value is NULL or having DEFAULT value in MySQL?
