Apache Derby 简明教程

Apache Derby - Drop Table

DROP TABLE 语句用于删除现有表,包括其所有触发器、约束和权限。

The DROP TABLE statement is used to remove an existing table including all its triggers, constraints, permissions.

Syntax

Following is the Syntax of the DROP TABLE statement. ij> DROP TABLE table_name;

Example

假设你在数据库中有一个名为 Student 的表。以下 SQL 语句删除了一个名为 Student 的表。

Suppose you have a table named Student in the database. The following SQL statement deletes a table named Student.

ij> DROP TABLE Student;
0 rows inserted/updated/deleted

因为我们已经删除了表,所以如果我们尝试描述它,我们会收到如下错误:

Since we have removed the table if we try to describe it, we will get an error as follows

ij> DESCRIBE Student;
IJ ERROR: No table exists with the name STUDENT

Drop Table using JDBC program

本部分教你如何使用 JDBC 应用程序删除 Apache Derby 数据库中的表。

This section teaches you how to drop 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 ;passw ord= 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;passw ord=PASSWORD"

按照以下步骤在 Apache Derby 中删除表:

Follow the steps given below to drop a table in Apache Derby

Step 1: Register the driver

要与数据库通信,首先需要注册驱动程序。类 ClassforName() 方法接受表示类名称的 String 值,将其加载到内存中,这会自动注册它。使用此方法注册驱动程序。

To communicate with the database, first of all, you need to register the driver. 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 the 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

您需要创建一个 StatementPreparedStatementCallableStatement 对象才能向数据库发送 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 execute queries like INSERT, UPDATE, DELETE. The executeQuery() method to results that returns data etc. Use either of these methods and execute the statement created previously.

Example

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

Following JDBC example demonstrates how to drop 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.Statement;
public class DropTable {
   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();

      //Executing the query
      String query = "DROP TABLE Employees";
      stmt.execute(query);
      System.out.println("Table dropped");
   }
}

Output

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

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

Table dropped