Creating and updating a database schema

SQL statements are used to create and update a database schema. You can use these statements to create or update tables, indexes, foreign keys, and publications for your database. Use the prepareStatement and execute methods to apply the schema updates to your database.

The following example demonstrates the use of SQL statements for table creation:



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();
}

In this example, the createDB method uses SQL statement strings to prepare and execute statements, which create two tables and an additional index on last_name and last_name.

 See also