Heim > Datenbank > MySQL-Tutorial > Hauptteil

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

WBOY
Freigeben: 2023-09-19 13:57:14
nach vorne
1355 人浏览过

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

将列声明为“NOT NULL”意味着该列不接受 NULL 值,但接受零 (0),并且空字符串本身就是一个值。因此,如果我们想将零或空字符串插入到定义为 NOT NULL 的 MySQL 列中,就不会有问题。下面将 0 和空字符串与 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)
Nach dem Login kopieren

以上结果集显示零 (0) 不为 NULL。这意味着零 (0) 本身就是一个值,因为我们知道 NULL 意味着没有值。

mysql> Select '' IS NULL, '' IS NOT NULL;
+------------+----------------+
| '' IS NULL | '' IS NOT NULL |
+------------+----------------+
|          0 |              1 |
+------------+----------------+
1 row in set (0.00 sec)
Nach dem Login kopieren

上面的结果集显示空字符串(‘’)不为NULL。这意味着空字符串 ('') 本身就是一个值,因为我们知道 NULL 意味着没有值。

示例

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)
Nach dem Login kopieren

从上面的结果集中可以看出,我们可以将零(0)一个空字符串(“”)插入到声明为 NOT NULL 的列中。

以上是如何将零或空字符串插入到定义为 NOT NULL 的 MySQL 列中?的详细内容。更多信息请关注PHP中文网其他相关文章!

Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!