CREATE TABLE statement

Description

Creates a new table in the database or on a remote server.

Syntax

CREATE [ GLOBAL TEMPORARY ] TABLE [ owner.]table-name
... ( column-definition [ column-constraint ]... 
[, column-definition [ column-constraint ]... ]
[, table-constraint ]... )
... [ { IN | ON } dbspace-name ]
... [ ON COMMIT { DELETE | PRESERVE } ROWS 
| NOT TRANSACTIONAL]
[ AT location-string ]

Parameters

column-definition:

column-name data-type [ [ NOT ] NULL ] [ default-value ]

default-value:

[ DEFAULT AUTOINCREMENT | IDENTITY ]

column-constraint:

{ UNIQUE | PRIMARY KEY | REFERENCES table-name [( column-name )] [ actions ] | CHECK ( condition ) | IQ UNIQUE ( integer ) }

table-constraint:

{ UNIQUE ( column-name [, column-name ]... ) | PRIMARY KEY ( column-name [, column-name ]... ) | CHECK ( condition ) | foreign-key-constraint }

foreign-key-constraint:

FOREIGN KEY [ role-name ] [ ( column-name [, column-name ]... ) ] ... REFERENCES table-name [(column-name [, column-name ]... ) ] ... [ action ] [

action:

ON { UPDATE | DELETE { RESTRICT }

location-string:

{ remote-server-name.[db-name].[owner].object-name | remote-server-name;[db-name];[owner];object-name }

Examples

Example 1

Usage

The CREATE TABLE statement creates a new table. A table can be created for another user by specifying an owner name. If GLOBAL TEMPORARY is not specified, the table is referred to as a base table. Otherwise, the table is a temporary table.

A created global temporary table is a table that exists in the database like a base table and remains in the database until it is explicitly removed by a DROP TABLE statement. The rows in a temporary table are only visible to the connection that inserted the rows. Multiple connections from the same or different applications can use the same temporary table at the same time and each connection will only see its own rows. A given connection inherits the schema of a global temporary table as it exists when the connection first refers to the table. The rows of a temporary table are deleted when the connection ends.

When you create a local temporary table, omit the owner specification. If you specify an owner when creating a temporary table, for example, CREATE TABLE dbo.#temp(col1 int), a base table is incorrectly created.

You cannot use a temporary table to create a join index.

IN clause The IN clause specifies in which database file (dbspace) the table is to be created. You can specify SYSTEM with this clause to put either a permanent or temporary table in the Catalog Store. All other use of the IN clause is ignored. You cannot use this clause to place an IQ table in a particular dbspace. By default all permanent tables are placed in the main IQ Store, and all temporary tables are placed in the Temporary IQ Store. Global temporary tables can never be in the IQ Store.

For more information about dbspaces, see CREATE DBSPACE statement.

ON COMMIT clause The ON COMMIT clause is only allowed for temporary tables. By default, the rows of a temporary table are deleted on COMMIT.

NOT TRANSACTIONAL The NOT TRANSACTIONAL clause is allowed only for temporary tables. A table created using NOT TRANSACTIONAL is not affected by either COMMIT or ROLLBACK.

The NOT TRANSACTIONAL clause provides performance improvements in some circumstances because operations on non-transactional temporary tables do not cause entries to be made in the rollback log. For example, NOT TRANSACTIONAL may be useful if procedures that use the temporary table are called repeatedly with no intervening COMMITs or ROLLBACKs.

The parenthesized list following the CREATE TABLE statement can contain the following clauses in any order:

AT clause The AT clause is used to create a table at the remote location specified by location-string. The local table that is created is a proxy table that maps to the remote location. Tables used as proxy tables must have names of 30 characters or less. The AT clause supports the semicolon (;) as a delimiter. If a semicolon is present anywhere in the location-string, the semicolon is the field delimiter. If no semicolon is present, a period is the field delimiter. This allows filenames and extensions to be used in the database and owner fields.

Semicolon field delimiters are used primarily with server classes not currently supported; however, you can also use them in situations where a period would also work as a field delimiter. For example, the following statement maps the table proxy_a to the Adaptive Server Anywhere database mydb on the remote server myasa:

CREATE TABLE proxy_a1
AT 'myasa;mydb;;a1'

Foreign key definitions are ignored on remote tables. Foreign key definitions on local tables that refer to remote tables are also ignored. Primary key definitions are sent to the remote server if the server supports primary keys.

column-definition Define a column in the table. Allowable data types are described in Chapter 4, “SQL Data Types”. Two columns in the same table cannot have the same name. If NOT NULL is specified, or if the column is in a UNIQUE or PRIMARY KEY constraint, the column cannot contain any NULL values. You can create up to 45,000 columns; however, there may be performance penalties with more than 10,000 columns in a table.

table-constraint Table constraints help ensure the integrity of data in the database. There are four types of integrity constraints:

If a statement would cause changes to the database that would violate an integrity constraint, the statement is effectively not executed and an error is reported. (Effectively means that any changes made by the statement before the error was detected are undone.)

Sybase IQ enforces single-column UNIQUE constraints by creating an HG index for that column.

NoteYou cannot define a column with a BIT data type as a UNIQUE or PRIMARY KEY constraint. Also, the default for columns of BIT data type is to not allow NULL values; you can change this by explicitly defining the column as allowing NULL values.

column-constraint A column constraint restricts the values the column can hold. Column and table constraints help ensure the integrity of data in the database. If a statement would cause a violation of a constraint, execution of the statement does not complete, any changes made by the statement before error detection are undone, and an error is reported. Column constraints are abbreviations for the corresponding table constraints. For example, the following are equivalent:

CREATE TABLE Product (
	product_num integer UNIQUE
)
CREATE TABLE Product (
	product_num integer,
	UNIQUE ( product_num )
)

Column constraints are normally used unless the constraint references more than one column in the table. In these cases, a table constraint must be used.

IQ UNIQUE constraint This constraint can be specified for columns only. IQ UNIQUE defines the cardinality of the column, and it is used to optimize the indexes internally. The default value is 0, which gives IQ no information for optimizing the default index. The IQ UNIQUE constraint should be applied if the expected distinct count (the number of unique values) for the column is less than or equal to 65536. This allows Sybase IQ to optimize storage of this column's data.

When the MINIMIZE_STORAGE option is ON (the default for new databases is OFF), it is equivalent to specifying IQ UNIQUE 255 for every newly created column, and there is no need to specify IQ UNIQUE except for columns with more than 65536 unique values.


Integrity constraints

UNIQUE or UNIQUE ( column-name, ... ) No two rows in the table can have the same values in all the named column(s). A table may have more than one unique constraint.

There is a difference between a unique constraint and a unique index. Columns of a unique index are allowed to be NULL, while columns in a unique constraint are not. A foreign key can reference either a primary key or a column with a unique constraint, but not a unique index, because it can include multiple instances of NULL.

PRIMARY KEY or PRIMARY KEY ( column-name, ... ) The primary key for the table will consist of the listed column(s), and none of the named column(s) can contain any NULL values. Sybase IQ ensures that each row in the table will have a unique primary key value. A table can have only one PRIMARY KEY.

When the second form is used (PRIMARY KEY followed by a list of columns), the primary key is created including the columns in the order in which they are defined, not the order in which they are listed.

When a column is designated as PRIMARY KEY, FOREIGN KEY, or UNIQUE, Sybase IQ creates a High_Group index for it automatically. For multicolumn primary keys, this index is on the primary key, not the individual columns. For best performance, you should also index each column with a HG or LF index separately.

REFERENCES primary-table-name [(primary-column-name)] This clause defines the column as a foreign key for a primary key or a unique constraint of a primary table. Normally, a foreign key would be for a primary key rather than an unique constraint. If a primary column name is specified, it must match a column in the primary table which is subject to a unique constraint or primary key constraint, and that constraint must consist of only that one column. Otherwise the foreign key references the primary key of the second table. Primary key and foreign key must have the same data type and the same precision, scale and sign. Only a non-unique single-column HG index is created for a single-column foreign key. For a multicolumn foreign key, Sybase IQ creates a non-unique composite HG index. The maximum width of a multicolumn composite key for a unique or non-unique HG index is 1KB.

A temporary table cannot have a foreign key that references a base table and a base table cannot have a foreign key that references a temporary table. Local temporary tables cannot have or be referenced by a foreign key.

FOREIGN KEY [role-name] [(...)] REFERENCES primary-table-name [(...)] This clause defines foreign key references to a primary key or a unique constraint in another table. Normally, a foreign key would be for a primary key rather than an unique constraint. (In this description, this other table will be called the primary table.)

If the primary table column names are not specified, then the primary table columns will be the columns in the table's primary key. If foreign key column names are not specified then the foreign key columns will have the same names as the columns in the primary table. If foreign key column names are specified, then the primary key column names must be specified, and the column names are paired according to position in the lists.

If the primary table is not the same as the foreign key table, either the unique or primary key constraint must have been defined on the referenced key. Both referenced key and foreign key must have the same number of columns, of identical data type with the same sign, precision, and scale.

The value of the row’s foreign key must appear as a candidate key value in one of the primary table’s rows unless one or more of the columns in the foreign key contains null(s) in a null allows foreign key column.

Any foreign key column not explicitly defined will automatically be created with the same data type as the corresponding column in the primary table. These automatically created columns cannot be part of the primary key of the foreign table. Thus, a column used in both a primary key and foreign key must be explicitly created.

The role-name is the name of the foreign key. The main function of the role-name is to distinguish two foreign keys to the same table. If no role-name is specified, the role-name is assigned as follows:

  1. If there is no foreign key with a role-name the same as the table name, then the table name is assigned as the role-name.

  2. If the table name is already taken, the role-name is the table name concatenated with a zero-padded three-digit number unique to the table.

The referential integrity action defines the action to be taken to maintain foreign key relationships in the database. Whenever a primary key value is changed or deleted from a database table, there may be corresponding foreign key values in other tables that should be modified in some way. You can specify an ON DELETE clause, followed by the RESTRICT clause:

RESTRICT Generates an error if you try to update or delete a primary key value while there are corresponding foreign keys elsewhere in the database. Generates an error if you try to update a foreign key so that you create new values unmatched by a candidate key. This is the default action, unless you specify that LOAD optionally reject rows that violate referential integrity.This enforces referential integrity at the statement level.

If you use CHECK ON COMMIT without specifying any actions, then RESTRICT is implied as an action for DELETE. Sybase IQ does not support CHECK ON COMMIT.

A global temporary table cannot have a foreign key that references a base table and a base table cannot have a foreign key that references a global temporary table. Local temporary tables cannot have or be referenced by a foreign key.

CHECK ( condition ) No row is allowed to fail the condition. If an INSERT statement would cause a row to fail the condition, the operation is not permitted and the effects of the statement are undone.

The change is rejected only if the condition is FALSE; in particular, the change is allowed if the condition is UNKNOWN. (See “NULL value” and “Search conditions” in Chapter 3, “SQL Language Elements” for more information about TRUE, FALSE, and UNKNOWN conditions.) CHECK condition is not enforced by Sybase IQ.

NoteSybase recommends that you do not define referential integrity (i.e., foreign key-primary key relationships) in IQ unless you are certain there are no orphan foreign keys.


Remote tables

Foreign key definitions are ignored on remote tables. Foreign key definitions on local tables that refer to remote tables are also ignored. Primary key definitions will be sent to the remote server if the server supports it.


Side effects

Automatic commit.

Standards

Permissions

Must have RESOURCE authority. To create a table for another user, you must have DBA authority.

See also