
- 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 it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?
Declaring a column ‘NOT NULL’ means that this column would not accept NULL values but zero (0) and an empty string is itself a value. Hence there would be no issue if we want to insert zero or an empty string into a MySQL column which is defined as NOT NULL. Following comparisons of 0 and empty string with NULL would make it clear −
mysql> Select 0 IS NULL, 0 IS NOT NULL; +-----------+---------------+ | 0 IS NULL | 0 IS NOT NULL | +-----------+---------------+ | 0 | 1 | +-----------+---------------+ 1 row in set (0.00 sec)
The above result set shows that zero (0) is not NULL. It means zero (0) is a value itself because as we know that NULL means NO VALUE.
mysql> Select '' IS NULL, '' IS NOT NULL; +------------+----------------+ | '' IS NULL | '' IS NOT NULL | +------------+----------------+ | 0 | 1 | +------------+----------------+ 1 row in set (0.00 sec)
The above result set shows that empty string (‘’) is not NULL. It means empty string (‘’) is a value itself because as we know that NULL means NO VALUE.
Example
mysql> create table test(id int NOT NULL, Name Varchar(10)); Query OK, 0 rows affected (0.19 sec) mysql> Insert into test6(id, name) values('1', 'Gaurav'),('0','Rahul'),('','Aarav'); Query OK, 3 rows affected, 1 warning (0.08 sec) Records: 3 Duplicates: 0 Warnings: 1 Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 3 mysql> Select * from test; +----+--------+ | id | Name | +----+--------+ | 1 | Gaurav | | 0 | Rahul | | 0 | Aarav | +----+--------+ 3 rows in set (0.00 sec)
From the result sets above, it can be observed that we can insert zero(0) an empty string(‘’) into a column which is declared as NOT NULL.
- Related Articles
- Why it shows 0 instead of empty string whenever I insert an empty string into a MySQL column which is declared as NOT NULL?
- What role data type plays when I insert an empty string into a MySQL column which is declared as NOT NULL?
- Which one is better to insert NULL or empty string in MySQL?
- Insert default into not null column if value is null in MySQL?
- Empty string in not-null column in MySQL?
- Which one is better in MySQL - NULL or empty string?
- How do I check if a column is empty or null in MySQL?
- How to test String is null or empty?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- Treat a MySQL column field as NULL if it is blank?
- Is it possible to make an insert or an update in the same MySQL query?
- Insert NULL value into INT column in MySQL?
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- Split a string and insert it as individual values into a MySQL table?
