MySQL 如何插入NULL值
要插入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)
阅读更多:MySQL 教程