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

SQL Anywhere 10.0.1 » UltraLite - Database Management and Reference » UltraLite SQL Statement Reference

UltraLite CREATE PUBLICATION statement Next Page

UltraLite CREATE TABLE statement


Create a new table in the database.

Syntax

CREATE TABLE table-name
( { column-definition | table-constraint }, ... )

column-definition :
column-name data-type [ [ NOT ]
NULL ] [ DEFAULT column-default ] [ column-constraint ... ]

column-constraint :
[ UNIQUE | PRIMARY KEY ]

table-constraint :
[ CONSTRAINT constraint-name ] spec

column-default :
{GLOBAL AUTOINCREMENT [ ( number ) ] |
AUTOINCREMENT | CURRENT DATE |
CURRENT TIME | CURRENT TIMESTAMP |
NULL |
NEWID( ) |
constant-value}

spec :
{ PRIMARY KEY ( ordered-column-list |
[ NOT NULL ] FOREIGN KEY [ role-name ]( ordered-column-list )
REFERENCES table-name ( column-name, . . . )
[ CHECK ON COMMIT ] |
UNIQUE ( ordered-column-list ) } [ WITH MAX HASH SIZE number ]


ordered-column-list :
[ ( column-name [ ASC | DESC ] ...,] ) ]

Parameters

column-definition    Define a column in the table. Available parameters for this clause include:

column-constraint    A short form for the equivalent table constraint, with only the current row. A column constraint can be one of:

table-constraint    A table constraint restricts the values that one or more columns in the table can hold in order to 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 rolled back, and an error is reported.

column-default    If a DEFAULT value is specified, it is used as the value for the column in any INSERT statement that does not specify a value for the column. If no DEFAULT is specified, it is equivalent to DEFAULT NULL. Default options include:

spec    Describes additional specifications. spec can be one of:

Remarks

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. 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.

Each row in the table has a unique primary key value.

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, 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.

Schema changes    Statements are not released if schema changes are initiated at the same time. See Schema changes with DDL statements.

UltraLite does not process requests or queries referencing the table while the CREATE TABLE statement is being processed. Furthermore, you cannot execute CREATE TABLE when the database includes active queries or uncommitted transactions.

For UltraLite.NET users: You cannot execute this statement, unless you also call the Dispose method for all data objects (for example, ULDataReader). See Dispose method.

Side effects

Automatic commit.

See also
Example

The following example creates a table for a library database to hold book information.

CREATE TABLE library_books (
   isbn CHAR(20)      PRIMARY KEY,
   copyright_date     DATE,
   title              CHAR(100),
   author             CHAR(50),
   location           CHAR(50),
   FOREIGN KEY location REFERENCES room
)

The following example creates a table for a library database to hold information on borrowed books. The default value for date_borrowed indicates that the book is borrowed on the day the entry is made. The date_returned column is NULL until the book is returned.

CREATE TABLE borrowed_book (
   loaner_name CHAR(100)      PRIMARY KEY,
   date_borrowed DATE NOT NULL DEFAULT CURRENT DATE,
   date_returned       DATE,
   book                CHAR(20)
   FOREIGN KEY book REFERENCES library_books (isbn),
)

The following example creates tables for a sales database to hold order and order item information.

CREATE TABLE Orders (
   order_num INTEGER NOT NULL PRIMARY KEY,
   date_ordered DATE,
   name CHAR(80)
);
CREATE TABLE Order_item (
   order_num        INTEGER NOT NULL,
   item_num         SMALLINT NOT NULL,
   PRIMARY KEY (order_num, item_num),
   FOREIGN KEY (order_num)
   REFERENCES Orders (order_num)
)