Entering content frame

CREATE TABLE Statement (create_table_statement)  

A CREATE TABLE statement (create_table_statement) defines a base table.

Syntax

<create_table_statement> ::=
  CREATE TABLE <table_name> (<column_definition>[,<table_description_element>,...])
  [IGNORE ROLLBACK] [NO FIXED LENGTH COLUMN] [<sample_definition>]
| CREATE TABLE <table_name> [(<table_description_element>,...)]
  [IGNORE ROLLBACK] [NO FIXED LENGTH COLUMN] [<sample_definition>] AS <query_expression> [<duplicates_clause> ]
| CREATE TABLE <table_name> LIKE <table_name> [IGNORE ROLLBACK]

<table_description_element> ::= <column_definition> | <constraint_definition> | <referential_constraint_definition> | <key_definition> | <unique_definition>

table_name, sample_definition, query_expression, duplicates_clause, column_definition, constraint_definition, referential_constraint_definition, key_definition, unique_definition

SQL statement for creating a table called person:

CREATE TABLE person (cno FIXED(4), firstname CHAR(7), name CHAR(7), account FIXED(7,2))

This CREATE TABLE statement comprises the keywords CREATE TABLE followed by the table name and (in parentheses) a list of column names, separated by commas. You can also define other criteria, such as a primary key, or referential integrity conditions.

Other examples: customer, hotel, reservation, room

Explanation

Executing a CREATE TABLE statement causes data that describes the table (or base table) to be stored in the database catalog. This data is called metadata.

A CREATE TABLE statement cannot contain more than one key definition (key_definition).

The table name (table_name) must not be identical with the name of an existing table of the current user.

The current user becomes the owner of the new table. In other words, he or she obtains the INSERT, UPDATE, DELETE, and SELECT privileges for this table. If the table is not a temporary table, the owner is also granted the INDEX, REFERENCES, and ALTER privileges.

See also:

Technical Specifications

Owner of a table

·        The owner of a table is specified before the table name: Temporary tables are identified by the owner TEMP before their name.
If a table name has an owner other than TEMP, the owner must be identical to the name of the current user and the user must have the status database administrator (DBA user) or RESOURCE user.

·        The owner of the table is not specified: the result is the same as if the current user were the owner.

CREATE TABLE … AS <query_expression> …

·        If no QUERY expression (query_expression) is specified, the CREATE TABLE statement must contain at least one column definition (column_definition).

·        If a query expression is specified, a base table is defined with the same structure as the result table defined by the QUERY expression.
If column definitions are specified, the column definition may only consist of a column name and the number of column definitions must be equal to the number of columns in the result table generated by the QUERY expression.
The data_type of the ith column in the base table is identical to that of the ith column in the result table generated by the QUERY expression.
The result table may also contain LONG columns.
If no column definitions are specified, the column names of the result table are used.
The rows of the result table are implicitly inserted in the generated base table. The DUPLICATES clause can be used to determine how key collisions are handled.
The QUERY expression is subject to certain restrictions that also apply to the INSERT statement.

LIKE <table_name>

If LIKE <table_name>is specified, an empty base table is created which, from the point of view of the current user, has the same structure as the source table, that is, it has all the columns with the same column names and definitions as the source table. This view does not necessarily have to be identical to the actual structure of the source table, since the user may not know all the columns because of privilege limitations.

The specified table must be either a base table, a view table, or a synonym. The user must have at least one privilege for this table.

The current user is the owner of the base table.

If all the key columns of the table specified after LIKE are contained in the base table, they form the key columns in this table. Otherwise, the database system implicitly inserts a key column SYSKEY CHAR(8) BYTE which then represents the key for the base table.

DEFAULT specifications or CONSTRAINT definitions for columns that are copied to the base table also apply to the new base table.

IGNORE ROLLBACK

IGNORE ROLLBACK is optional and can only be specified for temporary tables. Temporary tables with this characteristic are not affected by the transaction mechanism; i.e., changes affecting these tables are not reversed by rolling back a transaction.

NO FIXED LENGTH COLUMN

NO FIXED LENGTH COLUMN is optional. If you specify NO FIXED LENGTH COLUMN, all character columns and numeric columns are stored with variable lengths. The only columns excluded from this rule are the first n-1 primary key columns and any columns whose length does not exceed 2.

For character columns, this is the same as specifying VARCHAR.

SQL Statements for Changing Table Properties

Adding and Deleting Columns; Changing Data Types; Changing the CONSTRAINT Definition

ALTER TABLE statement

Renaming Columns

RENAME COLUMN statement

Renaming Tables

RENAME TABLE statement

 

Leaving content frame