
- 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
Empty string in not-null column in MySQL?
In PHP, the empty string equals to a NULL value, but in MySQL, the case is the different i.e. empty string is not equal to NULL value. To understand the above syntax, let us create a column with NOT NULL constraint while you can insert an empty string.
Let us create a table. The query to create a table is as follows −
mysql> create table EmptyStringNotNullDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(10) not null, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.83 sec)
Now you can insert some records in the table to check the empty string is null or not null. The query to insert record is as follows −
mysql> insert into EmptyStringNotNullDemo(Name) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into EmptyStringNotNullDemo(Name) values(''); Query OK, 1 row affected (0.43 sec) mysql> insert into EmptyStringNotNullDemo(Name) values('Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into EmptyStringNotNullDemo(Name) values(''); Query OK, 1 row affected (0.13 sec) mysql> insert into EmptyStringNotNullDemo(Name) values(''); Query OK, 1 row affected (0.16 sec) mysql> insert into EmptyStringNotNullDemo(Name) values('Larry'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from EmptyStringNotNullDemo;
The following is the output −
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | | | 3 | Carol | | 4 | | | 5 | | | 6 | Larry | +----+-------+ 6 rows in set (0.00 sec)
Now check the empty string is NULL or NOT NULL in MySQL. If you get a 0 that would mean the empty string is not equal to a NULL otherwise empty string is NULL. The query is as follows −
mysql> select Name, Name IS NULL as EmptyValueIsNotNULL from EmptyStringNotNullDemo;
The following is the output −
+-------+---------------------+ | Name | EmptyValueIsNotNULL | +-------+---------------------+ | John | 0 | | | 0 | | Carol | 0 | | | 0 | | | 0 | | Larry | 0 | +-------+---------------------+ 6 rows in set (0.00 sec)
You can achieve with the help of user-defined variable. The syntax is as follows −
SET @anyVariableName=” ”; UPDATE yourTableName SET yourColumnName= @anyVariableName;
Implement the above syntax for the given table. The query is as follows to declare and define a user variable.
mysql> set @emptyStringValue=""; Query OK, 0 rows affected (0.00 sec)
Update the table with an empty string. The query is as follows −
mysql> update EmptyStringNotNullDemo set Name=@emptyStringValue; Query OK, 3 rows affected (0.25 sec) Rows matched: 6 Changed: 3 Warnings: 0
Display all records from the table once again. The query is as follows −
mysql> select *from EmptyStringNotNullDemo;
The following is the output −
+----+------+ | Id | Name | +----+------+ | 1 | | | 2 | | | 3 | | | 4 | | | 5 | | | 6 | | +----+------+ 6 rows in set (0.00 sec)
The column ‘Name’ has been updated.
- 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?
- How to update empty string to NULL in MySQL?
- Check if a String is not empty ("") and not null in Java
- Removing NOT NULL restriction from column in MySQL?
- Which one is better in MySQL - NULL or empty string?
- Check for NULL or NOT NULL values in a column in MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Insert default into not null column if value is null in MySQL?
- SELECT not null column from two columns in MySQL?
- How to add a NOT NULL column in MySQL?
- What role data type plays when I insert an empty string into a MySQL column which is declared as NOT NULL?
- How it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?
- Which one is better to insert NULL or empty string in MySQL?
- How do I check if a column is empty or null in MySQL?
- Set NOT NULL attribute to an existing column in MySQL
