Hsqldb 简明教程
HSQLDB - Create Table
创建表的基本强制性要求是表名、字段名和这些字段的数据类型。此外,您还可以向表提供键约束。
The basic mandatory requirements to create a table are table name, field names, and the data types to those fields. Optionally, you can also provide the key constraints to the table.
Syntax
看一下以下语法。
Take a look at the following syntax.
CREATE TABLE table_name (column_name column_type);
Example
我们创建一个名为 tutorials_tbl 的表,其字段名称为 id、title、author 和 submission_date。看一下以下查询。
Let us create a table named tutorials_tbl with the field-names such as id, title, author, and submission_date. Take a look at the following query.
CREATE TABLE tutorials_tbl (
id INT NOT NULL,
title VARCHAR(50) NOT NULL,
author VARCHAR(20) NOT NULL,
submission_date DATE,
PRIMARY KEY (id)
);
执行以上查询后,您将收到以下输出 −
After execution of the above query, you will receive the following output −
(0) rows effected
HSQLDB – JDBC Program
以下是用于在 HSQLDB 数据库中创建一个名为 tutorials_tbl 的表的 JDBC 程序。将程序保存到 CreateTable.java 文件中。
Following is the JDBC program used to create a table named tutorials_tbl into the HSQLDB database. Save the program into CreateTable.java file.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class CreateTable {
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("CREATE TABLE tutorials_tbl (
id INT NOT NULL, title VARCHAR(50) NOT NULL,
author VARCHAR(20) NOT NULL, submission_date DATE,
PRIMARY KEY (id));
");
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println("Table created successfully");
}
}
您可以使用以下命令启动数据库。
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 CreateTable.java
\>java CreateTable
在执行上述命令之后,您将收到以下输出−
After execution of the above command, you will receive the following output −
Table created successfully