Hsqldb-null-values

提供:Dev Guides
移動先:案内検索

HSQLDB-NULL値

SQL NULLは、欠損値を表すために使用される用語です。 テーブルのNULL値は、空白のように見えるフィールドの値です。 フィールドまたは列の値をNULLと比較する条件を指定しようとすると、常に正しく機能しません。

3つのことを使用して、NULL値を処理できます。

  • IS NULL -列の値がNULLの場合、演算子はtrueを返します。
  • IS NOT NULL -列の値がNOT NULLの場合、演算子はtrueを返します。
  • <⇒ -演算子は値を比較し、(=演算子とは異なり)2つのNULL値に対しても真です。

NULLまたはNOT NULLの列を探すには、それぞれIS NULLまたはIS NOT NULLを使用します。

authorとtutorial_countの2つの列を含むテーブル tcount_tbl がある例を考えてみましょう。 tutorial_countにNULL値を指定すると、作成者がチュートリアルを1つも発行しなかったことを示します。 したがって、それぞれの作成者の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列が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プログラム

以下は、tutorial_ countがNULLで、tutorial_countがNOT NULLであるテーブルtcount_tblとは別にレコードを取得するJDBCプログラムです。 次のプログラムを 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