
- HSQLDB 教程
- HSQLDB - 首页
- HSQLDB - 简介
- HSQLDB - 安装
- HSQLDB - 连接
- HSQLDB - 数据类型
- HSQLDB - 创建表
- HSQLDB - 删除表
- HSQLDB - 插入查询
- HSQLDB - 选择查询
- HSQLDB - WHERE 子句
- HSQLDB - 更新查询
- HSQLDB - DELETE 子句
- HSQLDB - LIKE 子句
- HSQLDB - 排序结果
- HSQLDB - 连接查询
- HSQLDB - 空值
- HSQLDB - 正则表达式
- HSQLDB - 事务
- HSQLDB - ALTER 命令
- HSQLDB - 索引
- HSQLDB 有用资源
- HSQLDB - 快速指南
- HSQLDB - 有用资源
- HSQLDB - 讨论
HSQLDB - 空值
SQL NULL 是一个用于表示缺失值的术语。表中的 NULL 值是指字段中看起来为空的值。每当我们尝试给出比较字段或列值与 NULL 的条件时,它都不能正常工作。
我们可以使用三种方法来处理 NULL 值。
IS NULL - 如果列值为 NULL,则该运算符返回 true。
IS NOT NULL - 如果列值不为 NULL,则该运算符返回 true。
<=> - 该运算符比较值,即使对于两个 NULL 值,它也返回 true(与 = 运算符不同)。
要查找为 NULL 或 NOT NULL 的列,请分别使用 IS NULL 或 IS NOT NULL。
示例
让我们考虑一个示例,其中有一个表 tcount_tbl 包含两列:作者和教程计数。我们可以为教程计数提供 NULL 值,表示作者甚至没有发表一篇教程。因此,该相应作者的教程计数值为 NULL。
执行以下查询。
create table tcount_tbl(author varchar(40) NOT NULL, tutorial_count INT); INSERT INTO tcount_tbl values ('Abdul S', 20); INSERT INTO tcount_tbl values ('Ajith kumar', 5); INSERT INTO tcount_tbl values ('Jen', NULL); INSERT INTO tcount_tbl values ('Bavya kanna', 8); INSERT INTO tcount_tbl values ('mahran', NULL); INSERT INTO tcount_tbl values ('John Poul', 10); INSERT INTO tcount_tbl values ('Sathya Murthi', 6);
使用以下命令显示tcount_tbl表中的所有记录。
select * from tcount_tbl;
执行上述命令后,您将收到以下输出。
+-----------------+----------------+ | author | tutorial_count | +-----------------+----------------+ | Abdul S | 20 | | Ajith kumar | 5 | | Jen | NULL | | Bavya kanna | 8 | | mahran | NULL | | John Poul | 10 | | Sathya Murthi | 6 | +-----------------+----------------+
要查找 tutorial_count 列为 NULL 的记录,以下是查询。
SELECT * FROM tcount_tbl WHERE tutorial_count IS NULL;
执行查询后,您将收到以下输出。
+-----------------+----------------+ | author | tutorial_count | +-----------------+----------------+ | Jen | NULL | | mahran | NULL | +-----------------+----------------+
要查找 tutorial_count 列不为 NULL 的记录,以下是查询。
SELECT * FROM tcount_tbl WHERE tutorial_count IS NOT NULL;
执行查询后,您将收到以下输出。
+-----------------+----------------+ | author | tutorial_count | +-----------------+----------------+ | Abdul S | 20 | | Ajith kumar | 5 | | Bavya kanna | 8 | | John Poul | 10 | | Sathya Murthi | 6 | +-----------------+----------------+
HSQLDB – JDBC 程序
这是一个 JDBC 程序,它分别从 tutorial_count 为 NULL 和 tutorial_count 不为 NULL 的 tcount_tbl 表中检索记录。将以下程序保存到 NullValues.java 中。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class NullValues { public static void main(String[] args) { Connection con = null; Statement stmt_is_null = null; Statement stmt_is_not_null = null; ResultSet result = null; try { Class.forName("org.hsqldb.jdbc.JDBCDriver"); con = DriverManager.getConnection( "jdbc:hsqldb:hsql://127.0.0.1/testdb", "SA", ""); stmt_is_null = con.createStatement(); stmt_is_not_null = con.createStatement(); result = stmt_is_null.executeQuery( "SELECT * FROM tcount_tbl WHERE tutorial_count IS NULL;"); System.out.println("Records where the tutorial_count is NULL"); while(result.next()){ System.out.println(result.getString("author")+" | "+result.getInt("tutorial_count")); } result = stmt_is_not_null.executeQuery( "SELECT * FROM tcount_tbl WHERE tutorial_count IS NOT NULL;"); System.out.println("Records where the tutorial_count is NOT NULL"); while(result.next()){ System.out.println(result.getString("author")+" | "+result.getInt("tutorial_count")); } } catch (Exception e) { e.printStackTrace(System.out); } } }
使用以下命令编译并执行上述程序。
\>javac NullValues.java \>Java NullValues
执行上述命令后,您将收到以下输出。
Records where the tutorial_count is NULL Jen | 0 mahran | 0 Records where the tutorial_count is NOT NULL Abdul S | 20 Ajith kumar | 5 Bavya kanna | 8 John Poul | 10 Sathya Murthi | 6
广告