要插入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) -> );
以下是使用insert命令在表中插入一些记录的查询-
mysql> insert into insertNullValue(ClientName,ClientCountryName) values('Larry','US'); mysql> insert into insertNullValue(ClientName,ClientCountryName) values('David','AUS'); mysql> insert into insertNullValue(ClientName,ClientCountryName) values('Bob','UK');
以下是使用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; 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)