Home > Database > Mysql Tutorial > How to insert zero or empty string into a MySQL column defined as NOT NULL?

How to insert zero or empty string into a MySQL column defined as NOT NULL?

WBOY
Release: 2023-09-19 13:57:14
forward
1562 people have browsed it

如何将零或空字符串插入到定义为 NOT NULL 的 MySQL 列中?

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)
Copy after login

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)
Copy after login

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.

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template