The CREATE TABLE statement defines a table. The definition must include its name and the names and attributes of its columns. The definition can also include other attributes of the table, such as its primary key.
Invocation
This statement can be used in an application program using the DB2 CLI functions or issued through the CLP.
Syntax
>>-CREATE TABLE--table-name--| element-list |--+-----------------+->< '-WITH ENCRYPTION-' element-list: .-,--------------------------------------------------. V | |--(----+-column-name--| data-type |--| column-options |-+-+--)--| | .-,-----------. | | V | | +-PRIMARY KEY--(----column-name-+--)-------------+ +-| referential-constraint |---------------------+ '-CHECK--(--check-condition--)-------------------' data-type: |--+-+-INTEGER-+------------------------+-----------------------| | '-INT-----' | +-SMALLINT---------------------------+ +-DECIMAL--(--integer--,--integer--)-+ +-CHAR--(--integer--)----------------+ +-CHARACTER--(--integer--)-----------+ +-VARCHAR--(--integer--)-------------+ +-BLOB--(--integer--)----------------+ +-DATE-------------------------------+ +-TIME-------------------------------+ '-TIMESTAMP--------------------------' column-options: .--------------------------------------------. V | |----+----------------------------------------+-+---------------| +-NOT NULL-------------------------------+ +-+-PRIMARY KEY------------------+-------+ | +-REFERENCES--table-name-------+ | | '-CHECK--(--check-condition--)-' | +-DEFAULT--+-constant------------------+-+ | '-datetime-special-register-' | '-GENERATED ALWAYS AS IDENTITY-----------' referential-constraint: .-,-----------. V | |--FOREIGN KEY--(----column-name-+--)--REFERENCES--table-name---|
Description
Table names are converted to uppercase before being stored in the catalog. You can use delimited identifiers (with double quotation marks) to prevent such conversion. You must use delimited identifiers when a table name contains blanks or special characters.
The table name can include Double Byte Character Set characters.
Restriction: The system-created data files that correspond to tables created and named by user names do not distinguish between upper and lowercase characters. For example, the data file for a table named TB is named DSY_TB. The data file for a table named "tb" is also DSY_TB. Therefore, to ensure data integrity, it is strongly recommended that you do not name a table using a series of characters identical, except for character case, to an existing table name.
A user table can be encrypted only at the time it is created. After a table is created, encryption cannot be added or removed except by deleting the table.
Column names are converted to uppercase before being stored in the catalog. You can use delimited identifiers (with double quotation marks) to prevent such conversion. You must also use delimited identifiers when a column name contains blanks or special characters.
The length might be in the range of 1 byte to 32767 bytes.
integer is the maximum length.
The special register CURRENT DATE also produces the current date in ISO format.
The special register CURRENT TIME also produces the current time in ISO format.
The special register CURRENT TIMESTAMP also produces the current timestamp.
If NOT NULL is not specified, the column can contain null values, and its default value is either the null value or the value provided by the DEFAULT clause.
See the description of PRIMARY KEY on page ***.
Omission of DEFAULT from a column-definition results in the use of the null value as the default for the column. If such a column is defined NOT NULL, then the column does not have a valid default.
The generated value for IDENTITY column starts from 1, and increases by 1 each time a row is inserted into the table. Thus, uniqueness is guaranteed, although DB2 Everyplace does not automatically create an index on an IDENTITY column. If you want to have an index on an IDENTITY column, you must either create an index explicitly, or specify the column as PRIMARY KEY. When the range of the values of an IDENTITY column is exhausted (the maximum value is reached), further INSERT statements will cause an error (SQLSTATE 23522). The maximum value of an IDENTITY column of INT and SMALLINT types are the maximum values allowed by those 2 types. The maximum value of an IDENTITY column of a DECIMAL type is determined by (1) definition of the data type (precision, scale) and (2) maximum value allowed for IDENTITY column: 2.15* (10^18) (19 decimal digits). The smaller of the (1) and (2) is the range limit. For an IDENTITY column of a DECIMAL type, the value's fractional part is always 0, and the integral part is increased by 1 each time.
The IDENTITY specification can only be defined on columns whose data type is one of the 3 numeric types: INT, SMAIINT, DECIMAL. Otherwise, an error is raised (SQLSTATE 42815). There can be at most one IDENTITY column per table (otherwise error SQLSTATE 428C1). The user can not provide a value for an IDENTITY column in an INSERT statement (must default to DB2 Everyplace system generated value), nor can the user UPDATE an IDENTITY column.
The number of identified columns must not exceed 8.
A unique index will be automatically created on the specified columns.
Only one primary key can be defined on a table.
The length attribute of each specified column must not be greater than 1024 bytes.
Let T1 denote the object table of the statement. The foreign key of the referential constraint is composed of the identified columns. Each name in the list of column names must identify a column of T1, and the same column must not be identified more than once. The number of identified columns must not exceed 8. Foreign keys are not enforced by DB2 Everyplace.
A referential constraint is a duplicate if its foreign key is the same as the foreign key table of a previously specified referential constraint.
In the following discussion, let T2 denote the identified parent table, and let T1 denote the table being created.
The specified foreign key must have the same number of columns as the parent key of T2 and the description of the nth column of the foreign key must be comparable to the description of the nth column of that parent key. Datetime columns are not considered to be comparable to string columns for the purposes of this rule. Foreign keys are not enforced by DB2 Everyplace.
If a check constraint is specified as part of a column-definition then a column reference can be made only to the same column. Check constraints specified as part of a table definition can have column references identifying columns previously defined in the CREATE TABLE statement. Check constraints are not checked for inconsistencies, duplicate conditions, or equivalent conditions. Therefore, contradictory or redundant check constraints can be defined.
The check-condition "IS NOT NULL" can be specified, however it is recommended that nullability be enforced directly using the NOT NULL attribute of a column. For example, CHECK (salary + bonus > 30000) is accepted if salary is set to NULL, because CHECK constraints must be either satisfied or unknown and in this case salary is unknown. However, CHECK (salary IS NOT NULL) would be considered false and a violation of the constraint if salary is set to NULL.
Check constraints are enforced when rows in the table are inserted or updated.
All check constraints defined in a CREATE TABLE statement are combined and stored in the system catalog. DB2 Everyplace has a limit of 512 bytes for this combined check constraint.
Rules
Notes
Data type | Column byte count |
---|---|
INTEGER | 4 |
SMALLINT | 4 |
DECIMAL(n, m) | 4 - 20 |
CHAR(n) | n+1 |
VARCHAR(n) | i+5 where i is the actual length |
BLOB | i+4 where i is the actual length |
DATE | 4 |
TIME | 4 |
TIMESTAMP | 12 |
Example
Create table EMPLOYEE with column names EMPNO, FIRSTNAME, LASTNAME, DEPT, PHONENO, SALARY, and HIREDATE. CHAR means that the column will contain character data. NOT NULL means that the column cannot contain a null value. VARCHAR means that the column will contain varying-length character data. The primary key consists of the column EMPNO.
CREATE TABLE EMPLOYEE (EMPNO CHAR(3) PRIMARY KEY, FIRSTNAME VARCHAR(12) NOT NULL, LASTNAME VARCHAR(15) NOT NULL, DEPT CHAR(3), PHONENO CHAR(4), SALARY INT, HIREDATE DATE)
Related reference