如何在 MySQL 中插入 NULL 值?

mysqlmysqli database更新于 2025/1/18 9:48:17

要插入 NULL 值,可以使用 UPDATE 命令。以下是语法 −

UPDATE yourTableName SET yourColumnName=NULL;

首先我们创建一个表 −

mysql> create table insertNullValue
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(100),
   -> ClientCountryName varchar(20)
   -> );
Query OK, 0 rows affected (0.54 sec)

下面是使用 insert 命令在表中插入一些记录的查询 −

mysql> insert into insertNullValue(ClientName,ClientCountryName) values('Larry','US');
Query OK, 1 row affected (0.19 sec)

mysql> insert into insertNullValue(ClientName,ClientCountryName) values('David','AUS');
Query OK, 1 row affected (0.09 sec)

mysql> insert into insertNullValue(ClientName,ClientCountryName) values('Bob','UK');
Query OK, 1 row affected (0.17 sec)

以下是使用 select 语句显示表中的所有记录的查询 −

mysql> select * from insertNullValue;

这将产生以下输出 −

+----+------------+-------------------+
| Id | ClientName | ClientCountryName |
+----+------------+-------------------+
| 1  | Larry      | US                |
| 2  | David      | AUS               |
| 3  | Bob        | UK                |
+----+------------+-------------------+
3 rows in set (0.00 sec)

以下是在列中插入 NULL 值的查询 −

mysql> update insertNullValue set ClientCountryName=NULL;
Query OK, 3 rows affected (0.19 sec)
Rows matched: 3 Changed: 3 Warnings: 0

让我们检查一下列"ClientCountryName"中是否插入了 NULL 值。以下是查询 −

mysql> select * from insertNullValue;

这将产生以下显示 NULL 值的输出 −

+----+------------+-------------------+
| Id | ClientName | ClientCountryName |
+----+------------+-------------------+
| 1  | Larry      | NULL              |
| 2  | David      | NULL              |
| 3  | Bob        | NULL              |
+----+------------+-------------------+
3 rows in set (0.00 sec)

相关文章


有用资源