Home  >  Article  >  Database  >  Why whenever I insert an empty string into a MySQL column declared NOT NULL, it displays 0 instead of the empty string?

Why whenever I insert an empty string into a MySQL column declared NOT NULL, it displays 0 instead of the empty string?

WBOY
WBOYforward
2023-08-30 08:01:14963browse

为什么每当我将空字符串插入声明为 NOT NULL 的 MySQL 列时,它显示 0 而不是空字符串?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete