ASA SQL Reference
SQL Statements
Use this statement to create a new table in the database and, optionally, to create a table on a remote server.
CREATE [ GLOBAL TEMPORARY ] TABLE [ owner.]table-name
( { column-definition | table-constraint | pctfree }, ... )
[ { IN | ON } dbspace-name ]
[ ON COMMIT { DELETE | PRESERVE } ROWS
| NOT TRANSACTIONAL]
[ AT location-string ]
column-definition :
column-name data-type [ NOT NULL ]
[ DEFAULT default-value ] [ column-constraint ... ]
default-value :
special-value
| string
| global variable
| [ - ] number
| ( constant-expression )
| built-in-function( constant-expression )
| AUTOINCREMENT
| CURRENT DATABASE
| CURRENT REMOTE USER
| CURRENT UTC TIMESTAMP
| GLOBAL AUTOINCREMENT [ ( partition-size ) ]
| NULL
| TIMESTAMP
| UTC TIMESTAMP
| LAST USER
special-value:
CURRENT { DATE | TIME | TIMESTAMP
| UTC TIMESTAMP | USER | PUBLISHER }
| USER
column-constraint :
[ CONSTRAINT constraint-name ] {
UNIQUE
| PRIMARY KEY [ CLUSTERED ]
| REFERENCES table-name
[ ( column-name ) ] [ actions ] [ CLUSTERED ]
}
| [ CONSTRAINT constraint-name ] CHECK ( condition )
| COMPUTE ( expression )
table-constraint :
[ CONSTRAINT constraint-name ] {
UNIQUE ( column-name, ... )
| PRIMARY KEY [ CLUSTERED ] ( column-name, ... )
| CHECK ( condition )
| foreign-key-constraint
}
foreign-key-constraint :
[ NOT NULL ] FOREIGN KEY [ role-name ] [ (column-name, ... ) ]
REFERENCES table-name [ (column-name, ... ) ] [ CLUSTERED ]
[ actions ] [ CHECK ON COMMIT ]
action :
ON { UPDATE | DELETE }
...{ CASCADE | SET NULL | SET DEFAULT | RESTRICT }
location-string :
remote-server-name.[db-name].[owner].object-name
| remote-server-name;[db-name];[owner];object-name
pctfree : PCTFREE percent-free-space
percent-free-space : integer
PCTFREE Specifies the percentage of free space you want to reserve for each table page. The free space is used if rows increase in size when the data is updated. If there is no free space in a table page, every increase in the size of a row on that page requires the row to be split across multiple table pages, causing row fragmentation and possible performance degradation.
The value percent-free-space is an integer between 0 and 100. The former specifies that no free space is to be left on each page—each page is to be fully packed. A high value causes each row to be inserted into a page by itself. If PCTFREE is not set, 200 bytes are reserved in each page.
The value for PCTFREE is stored in the SYSATTRIBUTE system table.
For more information, see SYSATTRIBUTE system table.
IN clause The IN clause specifies the dbspace in which the table is to be created. If the table is a GLOBAL TEMPORARY table, the IN clause is ignored.
For more information about dbspaces, see CREATE DBSPACE statement.
ON COMMIT clause The ON COMMIT clause is allowed only 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.
AT clause Create a remote table on a different server specified by location-string and also a proxy table on the current database that maps to the remote table. The AT clause supports the semicolon (;) as a field delimiter in location-string. 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.
For example, the following statement maps the table a1 to the MS Access file mydbfile.mdb:
CREATE TABLE a1 AT 'access;d:\mydbfile.mdb;;a1'
For information on remote servers, see CREATE SERVER statement. For information on proxy tables, see CREATE EXISTING TABLE statement and Specifying proxy table locations.
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.
The COMPUTE clause is ignored for remote tables.
column-definition Define a column in the table. The following are part of column definitions.
column-name The column name is an identifier. Two columns in the same table cannot have the same name. For more information, see Identifiers.
data-type For information on data types, see SQL Data Types.
NOT NULL If NOT NULL is specified, or if the column is in a UNIQUE or PRIMARY KEY constraint, the column cannot contain NULL in any row.
DEFAULT For more information on the special-value, see Special values.
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.
Some of the defaults require more description:
AUTOINCREMENT When using AUTOINCREMENT, the column must be one of the integer data types, or an exact numeric type.
On inserts into the table, if a value is not specified for the AUTOINCREMENT column, a unique value larger than any other value in the column is generated. If an INSERT specifies a value for the column, it is used; if the specified value is larger than the current maximum value for the column, that value will be used as a starting point for subsequent inserts.
Deleting rows does not decrement the AUTOINCREMENT counter. Gaps created by deleting rows can only be filled by explicit assignment when using an insert. After an explicit insert of a row number less then the maximum, subsequent rows without explicit assignment are still automatically incremented with a value of one greater than the previous maximum.
The next value to be used for each column is stored as an integer. Using values greater than (231 - 1) may cause wraparound to incorrect values, and AUTOINCREMENT should not be used in such cases.
You can find the most recently inserted value of the column by inspecting the @@identity global variable.
The identity column is a Transact-SQL-compatible alternative to using the AUTOINCREMENT default. In Adaptive Server Anywhere, the identity column is implemented as AUTOINCREMENT default. For information, see The special IDENTITY column.
GLOBAL AUTOINCREMENT This default is intended for use when multiple databases will be used in a SQL Remote replication or MobiLink synchronization environment.
This default is similar to AUTOINCREMENT, except that the domain is partitioned. Each partition contains the same number of values. You assign each copy of the database a unique global database identification number. Adaptive Server Anywhere supplies default values in a database only from the partition uniquely identified by that database's number.
The partition size can be specified in parentheses immediately following the AUTOINCREMENT keyword. The partition size may be any positive integer, although the partition size is generally chosen so that the supply of numbers within any one partition will rarely, if ever, be exhausted.
If the column is of type BIGINT or UNSIGNED BIGINT, the default partition size is 232 = 4294967296; for columns of all other types the default partition size is 216 = 65536. Since these defaults may be inappropriate, especially if our column is not of type INT or BIGINT, it is best to specify the partition size explicitly.
When using this default, the value of the public option Global_database_id in each database must be set to a unique, non-negative integer. This value uniquely identifies the database and indicates from which partition default values are to be assigned. The range of allowed values is n p + 1 to (n + 1) p, where n is the value of the public option Global_database_id and p is the partition size. For example, if you define the partition size to be 1000 and set Global_database_id to 3, then the range is from 3001 to 4000.
If the previous value is less than (n + 1) p, the next default value will be one greater than the previous largest value in column. If the column contains no values, the first default value is n p + 1. Default column values are not affected by values in the column outside of the current partition; that is, by numbers less than pn + 1 or greater than p(n + 1). Such values may be present if they have been replicated from another database via MobiLink synchronization.
Because the public option Global_database_id cannot be set to negative values, the values chosen are always positive. The maximum identification number is restricted only by the column data type and the partition size.
If the public option Global_database_id is set to the default value of 2147483647, a null value is inserted into the column. Should null values not be permitted, attempting to insert the row causes an error. This situation arises, for example, if the column is contained in the table's primary key.
Null default values are also generated when the supply of values within the partition has been exhausted. In this case, a new value of Global_database_id should be assigned to the database to allow default values to be chosen from another partition. Attempting to insert the null value causes an error if the column does not permit nulls. To detect that the supply of unused values is low and handle this condition, create an event of type GlobalAutoincrement.
You cannot use DEFAULT GLOBAL AUTOINCREMENT in databases created with version 6 or earlier software, even if they have been upgraded.
Constant expressions Constant expressions that do not reference database objects are allowed in a DEFAULT clause, so functions such as GETDATE or DATEADD can be used. If the expression is not a function or simple value, it must be enclosed in parentheses.
TIMESTAMP Provides a way of indicating when each row in the table was last modified. When a column is declared with DEFAULT TIMESTAMP, a default value is provided for inserts, and the value is updated with the current date and time whenever the row is updated.
To provide a default value on insert, but not update the column whenever the row is updated, use DEFAULT CURRENT TIMESTAMP instead of DEFAULT TIMESTAMP.
For more information on timestamp columns, see The special Transact-SQL timestamp column and data type.
Columns declared with DEFAULT TIMESTAMP contain unique values, so that applications can detect near-simultaneous updates to the same row. If the current timestamp value is the same as the last value, it is incremented by the value of the DEFAULT_TIMESTAMP_INCREMENT option.
For more information, see DEFAULT_TIMESTAMP_INCREMENT option [database].
You can automatically truncate timestamp values in Adaptive Server Anywhere based on the DEFAULT_TIMESTAMP_INCREMENT option. This is useful for maintaining compatibility with other database software that records less precise timestamp values.
For more information, see TRUNCATE_TIMESTAMP_VALUES option [database].
The global variable @@dbts returns a TIMESTAMP value representing the last value generated for a column using DEFAULT TIMESTAMP. For more information, see Global variables.
string For more information, see Strings.
global-variable For more information, see Global variables.
column-constraint A column constraint restricts the values the column can hold.
table-constraint A table constraint restricts the values that one or more columns in the table can hold.
Constraints 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 statements 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.
Constraints include the following:
CHECK This allows arbitrary conditions to be verified. For example, a check constraint could be used to ensure that a column called Sex only contains the values M or F.
No row in a table is allowed to violate a constraint. If an INSERT or UPDATE statement would cause a row to violate a constraint, the operation is not permitted and the effects of the statement are undone.
The change is rejected only if a constraint condition evaluates to FALSE, the change is allowed if a constraint condition evaluates to TRUE or UNKNOWN.
For more information about TRUE, FALSE, and UNKNOWN conditions, see NULL value and Search conditions.
COMPUTE The COMPUTE constraint is a column constraint only. When a column is created using a COMPUTE constraint, its value in any row is the value of the supplied expression. Columns created with this constraint are read-only columns for applications: the value is changed by the database server when the expression is evaluated.
Any UPDATE statement that attempts to change the value of a computed column does fire any triggers associated with the column.
UNIQUE Identifies one or more columns that uniquely identify each row in the table. 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.
For information about unique indexes, see CREATE INDEX statement.
PRIMARY KEY This is the same as a unique constraint, except that a table can have only one primary key constraint. The primary key usually identifies the best identifier for a row. For example, the customer number might be the primary key for the customer table.
Columns included in primary keys cannot allow NULL. Each row in the table has a unique primary key value. A table can have only one PRIMARY KEY.
The order of the columns in a primary key is the order in which the columns were created in the table, not the order in which they are listed when the primary key is created.
For more information about the CLUSTERED option and clustered indexes, see Using clustered indexes.
Foreign key A foreign key constraint can be implemented using a REFERENCES column constraint (single column only) or a FOREIGN KEY table constraint. It restricts the values for a set of columns to match the values in a primary key or, less commonly, a unique constraint of another table (the primary table). For example, a foreign key constraint could be used to ensure that a customer number in an invoice table corresponds to a customer number in the customer table.
If you specify column name in a REFERENCES column constraint, it must be a column in the primary table, must be subject to a unique constraint or primary key constraint, and that constraint must consist of only that one column. If you do not specify column-name, the foreign key references the primary key of the primary table.
For more information about the CLUSTERED option and clustered indexes, see Using clustered indexes.
If you do not explicitly define a foreign key column, it is 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 of the same table must be explicitly created.
If foreign key column names are specified, then primary key column names must also be specified, and the column names are paired according to position in the lists. If the primary table column names are not specified in a FOREIGN KEY table constraint, then the primary key columns are used. If foreign key column names are not specified then the foreign key columns are give the same names as the columns in the primary table.
If at least one value in a multi-column foreign key is NULL, there is no restriction on the values that can be held in other columns of the key.
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.
NOT NULL Disallow NULL in the foreign key columns. A NULL in a foreign key means that no row in the primary table corresponds to this row in the foreign table.
role-name 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:
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.
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.
action 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 either an ON UPDATE clause, an ON DELETE clause, or both, followed by one of the following actions:
CASCADE When used with ON UPDATE, updates the corresponding foreign keys to match the new primary key value. When used with ON DELETE, deletes the rows from the foreign table that match the deleted primary key.
SET NULL Sets to NULL all the foreign key values that correspond to the updated or deleted primary key.
SET DEFAULT Sets foreign key values that match the updated or deleted primary key value to values specified on the DEFAULT clause of each foreign key column.
RESTRICT Generates an error if an attempt is made to update or delete a primary key value while there are corresponding foreign keys elsewhere in the database. RESTRICT is the default action.
CHECK ON COMMIT The CHECK ON COMMIT clause overrides the WAIT_FOR_COMMIT database option, and causes the database server to wait for a COMMIT before checking RESTRICT actions on a foreign key. The CHECK ON COMMIT clause does not delay CASCADE, SET NULL, or SET DEFAULT actions.
If you use CHECK ON COMMIT with out specifying any actions, then RESTRICT is implied as an action for UPDATE and DELETE.
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 specified, the table is a temporary table. Otherwise, the table is a base table.
The definition of a temporary table exists in the database, like that of 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 visible only 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 see only its own rows. The rows of a temporary table for a connection are deleted when the connection ends.
Must have RESOURCE authority.
Must have DBA authority to create a table for another user.
The AT clause to create proxy tables is not supported on Windows CE.
Automatic commit.
CREATE EXISTING TABLE statement
DECLARE LOCAL TEMPORARY TABLE statement
SQL/92 Entry-level feature.
SQL/99 Core feature.
The following are vendor extensions:
The { IN | ON } dbspace-name clause.
The ON COMMIT clause
Some of the default values.
Sybase Supported by Adaptive Server Enterprise, with some differences.
Temporary tables You can create a temporary table by preceding the table name in a CREATE TABLE statement with a pound sign (#). In Adaptive Server Anywhere, these are declared temporary tables, which are available only in the current connection. For information, see DECLARE LOCAL TEMPORARY TABLE statement.
Physical placement Physical placement of a table is carried out differently in Adaptive Server Anywhere and in Adaptive Server Enterprise. The ON segment-name clause supported by Adaptive Server Enterprise is supported in Adaptive Server Anywhere, but segment-name refers to a dbspace name.
Constraints Adaptive Server Anywhere does not support named constraints or named defaults, but does support domains, which allow constraint and default definitions to be encapsulated in the data type definition. It also supports explicit defaults and CHECK conditions in the CREATE TABLE statement.
NULL default By default, columns in Adaptive Server Enterprise default to NOT NULL, whereas in Adaptive Server Anywhere the default setting is NULL. This setting can be controlled using the ALLOW_NULLS_BY_DEFAULT database option. You should explicitly specify NULL or NOT NULL to make your data definition statements transferable between Adaptive Server Anywhere and Adaptive Server Enterprise.
For more information, see ALLOW_NULLS_BY_DEFAULT option [compatibility].
The following example creates a table for a library database to hold book information.
CREATE TABLE library_books ( -- NOT NULL is assumed for primary key columns isbn CHAR(20) PRIMARY KEY, copyright_date DATE, title CHAR(100), author CHAR(50), -- column(s) corresponding to primary key of room -- are created automatically 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 ( date_borrowed DATE NOT NULL DEFAULT CURRENT DATE, date_returned DATE, book CHAR(20) REFERENCES library_books (isbn), -- The check condition is UNKNOWN until -- the book is returned, which is allowed CHECK( date_returned >= date_borrowed ) )
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), -- When an order is deleted, delete all of its -- items. FOREIGN KEY (order_num) REFERENCES Orders (order_num) ON DELETE CASCADE )
The following example creates a table named t1 at the remote server SERVER_A and creates a proxy table named t1 that is mapped to the remote table.
CREATE TABLE t1 ( a INT, b CHAR(10)) AT 'SERVER_A.db1.joe.t1'