Declaring a column as "NOT NULL" means that the column does not accept NULL values, but does accept zero (0), and the empty string itself is a value. So, if we want to insert zero or empty string into a MySQL column defined as NOT NULL, there will be no problem. This will become clear by comparing 0 and the empty string with NULL -
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. This means that zero (0) is itself a value, since 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 the empty string (‘’) is not NULL. This means that the empty string ('') is itself a value, since we know that NULL means no value.
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)
As can be seen from the above result set, we can insert zero (0) an empty string ("") into a statement declared NOT NULL in the column.
The above is the detailed content of How to insert zero or empty string into a MySQL column defined as NOT NULL?. For more information, please follow other related articles on the PHP Chinese website!