Apache Derby 简明教程

Apache Derby - Create Table

CREATE TABLE 语句用于在 Derby 数据库中创建新表。

Syntax

以下是 CREATE 语句的语法。

CREATE TABLE table_name (
   column_name1 column_data_type1 constraint (optional),
   column_name2 column_data_type2 constraint (optional),
   column_name3 column_data_type3 constraint (optional)
);

在 Apache Derby 中创建表的另一种方式是您可以通过查询指定列名和数据类型。语法如下所示 −

CREATE TABLE table_name AS SELECT * FROM desired_table WITH NO DATA;

Example

以下 SQL 语句创建了一个名为 Student 的表,其中包含四列,其中 id 是主键并且自动生成。

ij> CREATE TABLE Student (
   Id INT NOT NULL GENERATED ALWAYS AS IDENTITY,
   Age INT NOT NULL,
   First_Name VARCHAR(255),
   last_name VARCHAR(255),
   PRIMARY KEY (Id)
);
> > > > > > > 0 rows inserted/updated/deleted

DESCRIBE 命令通过列出列及其详细信息(如果表存在)来描述指定的表。您可以使用此命令验证表是否创建。

ij> DESCRIBE Student;
COLUMN_NAME |TYPE_NAME |DEC&|NUM&|COLUM&|COLUMN_DEF|CHAR_OCTE&|IS_NULL&
------------------------------------------------------------------------------
ID |INTEGER |0 |10 |10 |AUTOINCRE&|NULL |NO
AGE |INTEGER |0 |10 |10 |NULL |NULL |NO
FIRST_NAME |VARCHAR |NULL|NULL|255 |NULL |510 |YES
LAST_NAME |VARCHAR |NULL|NULL|255 |NULL |510 |YES
4 rows selected

Create a Table using JDBC Program

本节介绍如何使用 JDBC 应用程序在 Apache Derby 数据库中创建表。

如果您想使用网络客户端请求 Derby 网络服务器,请确保服务器已启动并正在运行。网络客户端驱动的类名为 org.apache.derby.jdbc.ClientDriver ,URL 为 jdbc:derby://localhost:1527/DATABASE_NAME;create=true;user=USER_NAME;passw ord=PASSWORD。

按照以下步骤在 Apache Derby 中创建表 −

Step 1: Register the driver

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() 创建连接对象。使用此方法来创建连接。

Step 3: Create a statement object

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

Step 4: Execute the query

创建该语句后,你需要执行它。类 Statement 提供了多个方法来执行查询,如方法 execute() 以执行返回多个结果集的语句。方法 executeUpdate() 执行诸如 INSERT、UPDATE、DELETE 的查询。方法 executeQuery() 返回返回数据的结果等。使用其中一种方法并执行之前创建的语句。

Example

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

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTable {
   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 = "CREATE TABLE Employees( "
         + "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255), "
         + "PRIMARY KEY (Id))";
         stmt.execute(query);
         System.out.println("Table created");
   }
}

Output

执行上述程序后,您将获得以下输出

Table created