Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (中文) » UltraLiteJ » 使用 UltraLiteJ » 开发 UltraLiteJ 应用程序

 

创建和更新数据库模式

SQL 语句可用于创建和更新数据库模式。可以使用这些语句创建或更新数据库的表、索引、外键和发布。使用 prepareStatement 和 execute 方法将模式更新应用于数据库。

下面的示例演示如何在创建表时使用 SQL 语句:



static String stmt_1 = "CREATE TABLE Department("
    + "id int PRIMARY KEY, "
    + "name char(50) NOT NULL)";

static String stmt_2 = "CREATE TABLE Employee("
    + "id int PRIMARY KEY, "
    + "last_name char(50) NOT NULL, "
    + "first_name char(50) NOT NULL, "
    + "dept_id int NOT NULL, "
    + "NOT NULL FOREIGN KEY(dept_id) "
    + "REFERENCES Department(id))";

static String stmt_3 = "CREATE INDEX ON Employee(last_name, first_name)";

void createDb(Connection connection) throws ULjException {
    PreparedStatement ps;
    ps = connection.prepareStatement(stmt_1);
    ps.execute();
    ps.close();
    ps = connection.prepareStatement(stmt_2);
    ps.execute();
    ps.close();
    ps = connection.prepareStatement(stmt_3);
    ps.execute();
    ps.close();
}

在此示例中,createDB 方法使用 SQL 语句字符串准备和执行语句,从而在 last_name 和 first_name 上创建两个表和一个附加索引。

 另请参见