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:///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
廣告