Hsqldb 简明教程

HSQLDB - Delete Clause

每当您希望从任何 HSQLDB 表中删除记录时,都可以使用 DELETE FROM 命令。

Whenever you want to delete a record from any HSQLDB table, you can use the DELETE FROM command.

Syntax

以下是 DELETE 命令用于从 HSQLDB 表中删除数据的通用语法。

Here is the generic syntax for DELETE command to delete data from a HSQLDB table.

DELETE FROM table_name [WHERE Clause]
  1. If WHERE clause is not specified, then all the records will be deleted from the given MySQL table.

  2. You can specify any condition using WHERE clause.

  3. You can delete records in a single table at a time.

Example

让我们考虑一个示例,该示例从名为 tutorials_tbl 的表中删除记录数据,其 ID 为 105 。以下是实现给定示例的查询。

Let us consider an example that deletes the record data from the table named tutorials_tbl having id 105. Following is the query that implements the given example.

DELETE FROM tutorials_tbl WHERE id = 105;

执行以上查询后,您将收到以下输出 −

After execution of the above query, you will receive the following output −

(1) rows effected

HSQLDB – JDBC Program

以下是实现给定示例的 JDBC 程序。将以下程序保存为 DeleteQuery.java

Here is the JDBC program that implements the given example. Save the following program into DeleteQuery.java.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DeleteQuery {

   public static void main(String[] args) {
      Connection con = null;
      Statement stmt = null;
      int result = 0;

      try {
         Class.forName("org.hsqldb.jdbc.JDBCDriver");
         con = DriverManager.getConnection(
            "jdbc:hsqldb:hsql://localhost/testdb", "SA", "");
         stmt = con.createStatement();
         result = stmt.executeUpdate(
            "DELETE FROM tutorials_tbl   WHERE id=105");
      } catch (Exception e) {

         e.printStackTrace(System.out);
      }
      System.out.println(result+" Rows effected");
   }
}

您可以使用以下命令启动数据库。

You can start the database using the following command.

\>cd C:\hsqldb-2.3.4\hsqldb
hsqldb>java -classpath lib/hsqldb.jar org.hsqldb.server.Server --database.0
file:hsqldb/demodb --dbname.0 testdb

使用以下命令编译并执行以上程序。

Compile and execute the above program using the following command.

\>javac DeleteQuery.java
\>java DeleteQuery

在执行上述命令之后,您将收到以下输出−

After execution of the above command, you will receive the following output −

1 Rows effected