Apache Derby 简明教程

Apache Derby - Delete Data

DELETE 语句用于删除表的行。就像 UPDATE 语句一样,Apache Derby 提供两种类型的 Delete(语法): searched delete 和 positioned delete。

The DELETE statement is used to delete rows of a table. Just like the UPDATE statement, Apache Derby provides two types of Delete (syntax): searched delete and positioned delete.

searched delete 语句删除表的所有指定列。

The searched delete statement deletes all the specified columns of a table.

Syntax

DELETE 语句的语法如下 −

The syntax of the DELETE statement is as follows −

ij> DELETE FROM table_name WHERE condition;

Example

我们假设我们有一个名为 employee 的表格,其中包含 5 条记录,如下所示 −

Let us suppose we have a table named employee with 5 records as shown below −

ID |NAME     |SALARY |LOCATION
----------------------------------------------------------------------------
1  |Amit     |30000  |Hyderabad
2  |Kalyan   |40000  |Vishakhapatnam
3  |Renuka   |50000  |Delhi
4  |Archana  |15000  |Mumbai
5  |Trupti   |45000  |Kochin
5 rows selected

以下 SQL DELETE 语句可删除名为 Trupti 的记录。

The following SQL DELETE statement deletes the record with name Trupti.

ij> DELETE FROM Employees WHERE Name = 'Trupti';
1 row inserted/updated/deleted

如果您获取 Employees 表格的内容,则可以看到只有四条记录,如下所示 −

If you get the contents of the Employees table, you can see only four records as shown below −

ID |NAME    |SALARY |LOCATION
----------------------------------------------------------------------------
1  |Amit    |30000  |Hyderabad
2  |Kalyan  |40000  |Vishakhapatnam
3  |Renuka  |50000  |Delhi
4  |Archana |15000  |Mumbai
4 rows selected

要删除表中的所有记录,请执行不带 where 子句的相同查询。

To delete all the records in the table, execute the same query without where clause.

ij> DELETE FROM Employees;
4 rows inserted/updated/deleted

现在,如果您尝试获取 Employee 表格的内容,您会得到一个空表格,如下所示 −

Now, if you try to get the contents of the Employee table, you will get an empty table as given below −

ij> select * from employees;
ID |NAME |SALARY |LOCATION
--------------------------------------------------------
0 rows selected

Delete Data using JDBC program

此章节说明了如何使用 JDBC 应用程序删除 Apache Derby 数据库中表的现有记录。

This section explains how to delete the existing records of a table in Apache Derby database using JDBC application.

如果你想使用网络客户端访问 Derby 网络服务器,请确保服务器正在运行。网络客户端驱动程序的类名为 org.apache.derby.jdbc.ClientDriver,URL 为 jdbc:derby://localhost:1527/ DATABASE_NAME;*create=true;user=*USER_NAME;*password=*PASSWORD "。

If you want to request the Derby network server using network client, make sure that the server is up and running. The class name for the Network client driver is org.apache.derby.jdbc.ClientDriver and the URL is jdbc:derby://localhost:1527/DATABASE_NAME;*create=true;user=*USER_NAME;*password=*PASSWORD".

按照以下步骤删除 Apache Derby 表中的现有记录:

Follow the steps given below to delete the existing records of a table in Apache Derby:

Step 1: Register the driver

首先,您需要注册该驱动程序才能与数据库通信。类 ClassforName() 方法接受一个表示类名的 String 值,将其加载到内存中,从而自动注册该类。使用此方法注册该驱动程序。

Firstly, you need to register the driver to communicate with the database. The forName() method of the class Class accepts a String value representing a class name loads it in to the memory, which automatically registers it. Register the driver using this method.

Step 2: Get the connection

通常,我们与数据库通信的第一步是与数据库连接。 Connection 类表示与数据库服务器的物理连接。您可以通过调用 DriverManager 类的 getConnection() 方法创建一个连接对象。使用此方法创建一个连接。

In general, the first step we do to communicate to the database is to connect with it. The Connection class represents physical connection with a database server. You can create a connection object by invoking the getConnection() method of the DriverManager class. Create a connection using this method.

Step 3: Create a statement object

您需要创建一个 StatementPreparedStatement or, CallableStatement 对象以将 SQL 语句发送到数据库。您可以分别使用 createStatement(), prepareStatement() and, prepareCall() 方法创建这些对象。使用相应的方法创建其中任何一个对象。

You need to create a Statement or PreparedStatement or, CallableStatement objects to send SQL statements to the database. You can create these using the methods createStatement(), prepareStatement() and, prepareCall() respectively. Create either of these objects using the appropriate method.

Step 4: Execute the query

创建语句后,您需要执行该语句。 Statement 类提供各种方法来执行查询,例如 execute() 方法用于执行返回多个结果集的语句。 executeUpdate() 方法执行诸如 INSERT、UPDATE、DELETE 的查询。 executeQuery() 方法返回结果,返回数据。使用其中任何一种方法并执行先前创建的语句。

After creating a statement, you need to execute it. The Statement class provides various methods to execute a query like the execute() method to execute a statement that returns more than one result set. The executeUpdate() method executes queries like INSERT, UPDATE, DELETE. The executeQuery() method results that returns data. Use either of these methods and execute the statement created previously.

Example

以下 JDBC 示例演示了如何使用 JDBC 程序删除 Apache Derby 中表的现有记录。在此,我们使用嵌入式驱动程序连接到名为 sampleDB 的数据库(如果不存在则创建)。

Following JDBC example demonstrates how to delete the existing records of a table in Apache Derby using JDBC program. Here, we are connecting to a database named sampleDB (will create if it does not exist) using the embedded driver.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteData {
   public static void main(String args[]) throws Exception {
      //Registering the driver
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      //Getting the Connection object
      String URL = "jdbc:derby:sampleDB;create=true";
      Connection conn = DriverManager.getConnection(URL);

      //Creating the Statement object
      Statement stmt = conn.createStatement();
      //Creating a table and populating it
      String query = "CREATE TABLE Employees("
         + "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
         + "Name VARCHAR(255), Salary INT NOT NULL, "
         + "Location VARCHAR(255), "
         + "PRIMARY KEY (Id))";
      String query = "INSERT INTO Employees("
         + "Name, Salary, Location) VALUES "
         + "('Amit', 30000, 'Hyderabad'), "
         + "('Kalyan', 40000, 'Vishakhapatnam'), "
         + "('Renuka', 50000, 'Delhi'), "
         + "('Archana', 15000, 'Mumbai'), "
         + "('Trupthi', 45000, 'Kochin'), "
         + "('Suchatra', 33000, 'Pune'), "
         + "('Rahul', 39000, 'Lucknow'), "
         + "('Trupthi', 45000, 'Kochin')";
      //Executing the query
      String query = "DELETE FROM Employees WHERE Name = 'Trupthi'";
      int num = stmt.executeUpdate(query);
      System.out.println("Number of records deleted are: "+num);
   }
}

Output

执行上述程序时,你将得到以下输出:

On executing the above program, you will get the following output −

Number of records deleted are: 1