This is because inserting an empty string means we are inserting some value instead of NULL. The empty string obviously maps to zero as an integer. In other words, we can say that by inserting the empty string, we are providing MySQL with an integer value represented as INT 0. Consider the following example where we have inserted an empty string and it is mapped as 0 by MySQL.
mysql> create table test(id int NOT NULL, Name Varchar(10)); Query OK, 0 rows affected (0.19 sec) mysql> Insert into test(id, name) values('1', 'Gaurav'),('0','Rahul'),('','Aarav'); Query OK, 3 rows affected, 1 warning (0.08 sec) Records: 3 Duplicates: 0 Warnings: 1 mysql> Select * from test; +----+--------+ | id | Name | +----+--------+ | 1 | Gaurav | | 0 | Rahul | | 0 | Aarav | +----+--------+ 3 rows in set (0.00 sec)
The above is the detailed content of Why whenever I insert an empty string into a MySQL column declared NOT NULL, it displays 0 instead of the empty string?. For more information, please follow other related articles on the PHP Chinese website!