HyperSQL教程

HSQLDB 空值

SQL NULL 是用于表示缺失值的术语。表中的 NULL 值是字段中显示为空白的值。每当我们尝试给出一个条件,将字段或列值与 NULL 进行比较时,它无法正常工作。
我们可以通过使用这三样东西来处理 NULL 值。
IS NULL-如果列值为 NULL,则运算符返回 true。 IS NOT NULL-如果列值为 NOT NULL,则运算符返回 true。 <=>-运算符比较值,即使对于两个 NULL 值也是如此(与 = 运算符不同)。
要查找 NULL 或 NOT NULL 的列,请分别使用 IS NULL 或 IS NOT NULL。

示例

让我们考虑一个例子,其中有一个表 tcount_tbl 包含两列,author 和 tutorial_count。我们可以为 tutorial_count 提供 NULL 值,表示作者甚至没有发布过一篇教程。因此,相应作者的 tutorial_count 值为 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 列不为空的记录,以下是查询。
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 程序

这是从表 tcount_tbl 中分别检索记录的 JDBC 程序,其中 tutorial_count 为 NULL,tutorial_count 为 NOT NULL。将以下程序保存到 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://localhost/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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4