A CREATE TABLE statement ( create_table_statement) defines a base table (see Table).
<create table statement> ::=
CREATE TABLE <table name> (<column
definition>[,<table description element>,...])
[IGNORE ROLLBACK] [<sample definition>]
| CREATE TABLE <table name> [(<table description
element>,...)]
[IGNORE ROLLBACK] [<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
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.
The 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:
·
The table owner must be
specified in front of the table name: temporary tables are a special type of table. They
only exist during a user database session and are deleted with their entire
contents afterwards. Temporary tables are identified by the owner TEMP in
front of the table 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 DBA or RESOURCE.
· The owner of the table is not specified: the result is the same as if the current user were the owner.
· If a QUERY expression is not specified, the CREATE TABLE statement must contain at least one 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.
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 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.