让我们首先创建一个表-
mysql> create table OrderDemo -> ( -> OrderId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> OrderPrice int, -> OrderDatetime datetime -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into OrderDemo(OrderPrice,OrderDatetime) values(200,'2016-09-12'); mysql> insert into OrderDemo(OrderPrice,OrderDatetime) values(NULL,'2002-11-18'); mysql> insert into OrderDemo(OrderPrice,OrderDatetime) values(1000,'2017-12-28');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from OrderDemo;
输出结果
+---------+------------+---------------------+ | OrderId | OrderPrice | OrderDatetime | +---------+------------+---------------------+ | 1 | 200 | 2016-09-12 00:00:00 | | 2 | NULL | 2002-11-18 00:00:00 | | 3 | 1000 | 2017-12-28 00:00:00 | +---------+------------+---------------------+ 3 rows in set (0.00 sec)
这是在OrderPrice列中添加一行的查询,其中OrderPrice为NULL,即更新NULL-
mysql> update OrderDemo set OrderPrice = 6500 where OrderPrice IS NULL; Rows matched: 1 Changed: 1 Warnings: 0
现在再次检查表记录。查询如下-
mysql> select *from OrderDemo;
输出结果
+---------+------------+---------------------+ | OrderId | OrderPrice | OrderDatetime | +---------+------------+---------------------+ | 1 | 200 | 2016-09-12 00:00:00 | | 2 | 6500 | 2002-11-18 00:00:00 | | 3 | 1000 | 2017-12-28 00:00:00 | +---------+------------+---------------------+ 3 rows in set (0.00 sec)
NULL值已替换为6500。