The CREATE TABLE statement defines a table. The definition must include its name and the names and attributes of its columns. The definition may include other attributes of the table such as primary key.
This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared.
The privileges held by the authorization ID of the statement must include at least one of the following:
If SQL names are specified and a user profile exists that has the same name as the library into which the table is created, and that name is different from the authorization ID of the statement, then the privileges held by the authorization ID of the statement must include at least one of the following:
To define a foreign key, the privileges held by the authorization ID of the statement must include at least one of the following on the parent table:
The authorization ID of the statement has the REFERENCES privilege on a table when one of the following is true:
The authorization ID of the statement has the REFERENCES privilege on a column of the table when one of the following is true:
If a user-defined type is referenced, the privileges held by the authorization ID of the statement must include at least one of the following:
The authorization ID of the statement has the USAGE privilege on a user-defined type when one of the following is true:
If a user-defined cast function is referenced, the privileges held by the authorization ID of the statement must include at least one of the following:
The authorization ID of the statement has the EXECUTE privilege on a user-defined function when one of the following is true:
>>-CREATE TABLE--table-name--(---------------------------------->
.-,-----------------------------.
V |
>---------+-column-definition------+--+--)---------------------->
+-unique-constraint------+
+-referential-constraint-+
'-check-constraint-------'
>-----+-------------------+------------------------------------><
'-nodegroup-clause--'
column-definition
|---column-name----+-------------------------------------+------>
| .-COLUMN-. |
'-FOR-+--------+--system-column-name--'
>----data-type-------------------------------------------------->
.----------------------------------------------------------------------------.
V |
>--------+----------------------------------------------------------------------+--+>
+-NOT NULL-------------------------------------------------------------+
+-default-clause-------------------------------------------------------+
| (1) |
+-datalink-options-----------------------------------------------------+
'-+------------------------------+---+-+-UNIQUE------+---------------+-'
'-CONSTRAINT--constraint-name--' | '-PRIMARY KEY-' |
+-references-clause-------------+
'-CHECK--(--check-condition--)--'
>---------------------------------------------------------------|
Notes:
|
data-type
|--+---BIGINT-------------------------------------------------------------------------------------------------------+->
+-+-INTEGER-+----------------------------------------------------------------------------------------------------+
| '-INT-----' |
+---SMALLINT-----------------------------------------------------------------------------------------------------+
+--+-DECIMAL-+---+-------------------------------+---------------------------------------------------------------+
| +-DEC-----+ '-(--integer--+-----------+--)--' |
| '-NUMERIC-' '-, integer-' |
+-+-FLOAT--+----------------+-+----------------------------------------------------------------------------------+
| | '-(--integer--)--' | |
| +-REAL----------------------+ |
| | .-PRECISION-. | |
| '-DOUBLE-+-----------+------' |
+----+-BLOB----------------+-----+------------------------+---+------------------+-------------------------------+
| '-BINARY LARGE OBJECT-' '-(--integer--+---+---)--' '-allocate-clause--' |
| +-K-+ |
| '-M-' |
+--+--+-CHARACTER-+---+----------------+-------------------------------------------------+---+-----------------+-+
| | '-CHAR------' '-(--integer--)--' | +-FOR BIT DATA----+ |
| +--+--+-VARCHAR-------------------+---(--integer--)--+---+------------------+---------+ +-FOR SBCS DATA---+ |
| | | '--+-CHARACTER-+---VARYING--' | '-allocate-clause--' | +-FOR MIXED DATA--+ |
| | | '-CHAR------' | | '-CCSID--integer--' |
| | '-LONG VARCHAR------------------------------------' | |
| '----+-CLOB-------------------+-----+------------------------+---+------------------+-' |
| +-CHAR LARGE OBJECT------+ '-(--integer--+---+---)--' '-allocate-clause--' |
| '-CHARACTER LARGE OBJECT-' +-K-+ |
| '-M-' |
+--+----GRAPHIC-----+----------------+---------------------------------+---+-----------------+-------------------+
| | '-(--integer--)--' | '-CCSID--integer--' |
| +--+--+-VARGRAPHIC------+---(--integer--)--+---+------------------+-+ |
| | | '-GRAPHIC VARYING-' | '-allocate-clause--' | |
| | '-LONG VARGRAPHIC-----------------------' | |
| '----DBCLOB------+------------------------+---+------------------+--' |
| '-(--integer--+---+---)--' '-allocate-clause--' |
| +-K-+ |
| '-M-' |
+---DATE---------------------------------------------------------------------------------------------------------+
+---TIME---------------------------------------------------------------------------------------------------------+
+---TIMESTAMP----------------------------------------------------------------------------------------------------+
+---DATALINK--+----------------+---+------------------+---+-----------------+------------------------------------+
| '-(--integer--)--' '-allocate-clause--' '-CCSID--integer--' |
'---distinct-type------------------------------------------------------------------------------------------------'
>---------------------------------------------------------------|
allocate-clause
|---ALLOCATE--(integer)-----------------------------------------|
default-clause
.-WITH-.
|--+------+--DEFAULT--+--------------------------------------------------------+->
+-constant-----------------------------------------------+
+-NULL---------------------------------------------------+
+-USER---------------------------------------------------+
+-CURRENT_DATE-------------------------------------------+
+-CURRENT_TIME-------------------------------------------+
+-CURRENT_TIMESTAMP--------------------------------------+
| (1) |
'-cast-function-name--(--+-constant----------+---)-------'
+-USER--------------+
+-CURRENT_DATE------+
+-CURRENT_TIME------+
'-CURRENT_TIMESTAMP-'
>---------------------------------------------------------------|
datalink-options
.-NO LINK CONTROL--------------------------.
|---LINKTYPE URL--+------------------------------------------+--|
'-FILE LINK CONTROL--+-file-link-options-+-'
'-MODE DB2OPTIONS---'
file-link-options
.-----------------------------------.
V (2) |
|-----+------------------------------+--+-----------------------|
+-INTEGRITY ALL----------------+
+-+-READ PERMISSION FS-+-------+
| '-READ PERMISSION DB-' |
+-+-WRITE PERMISSION FS------+-+
| '-WRITE PERMISSION BLOCKED-' |
+-RECOVERY NO------------------+
'-+-ON UNLINK RESTORE-+--------'
'-ON UNLINK DELETE--'
Notes:
|
unique-constraint
|--+------------------------------+---+-UNIQUE------+--(-------->
'-CONSTRAINT--constraint-name--' '-PRIMARY KEY-'
.-,--------------.
V |
>--------column-name---+--)-------------------------------------|
referential-constraint
(1)
|---+------------------------------+--FOREIGN KEY--------------->
'-CONSTRAINT--constraint-name--'
.-,--------------.
V |
>----(-----column-name---+---)---references-clause--------------|
references-clause
|---REFERENCES--table-name----+----------------------------+---->
| .-,--------------. |
| V | |
'-(-----column-name---+---)--'
(2)
.-ON DELETE NO ACTION--------. .-ON UPDATE NO ACTION--.
>-----+----------------------------+---+----------------------+-|
'-ON DELETE--+-RESTRICT----+-' '-ON UPDATE RESTRICT---'
+-CASCADE-----+
+-SET NULL----+
'-SET DEFAULT-'
check-constraint
|---+------------------------------+---------------------------->
'-CONSTRAINT--constraint-name--'
>----CHECK--(--check-condition--)-------------------------------|
nodegroup-clause
|---IN----nodegroup-name-----+------------------------------------------------------------------+->
| .-,--------------. |
| V | |
| .-USING HASHING-. |
'-PARTITIONING KEY--(-----column-name---+---)---+---------------+--'
>---------------------------------------------------------------|
Notes:
|
If SQL names were specified, the table will be created in the collection or library specified by the implicit or explicit qualifier. The qualifier is the owner of the table if a user profile with that name exists. Otherwise, the owner of the table is the user profile or group user profile of the job executing the statement.
If system names were specified, the table will be created in the collection or library that is specified by the qualifier. If not qualified, the table will be created in the current library (*CURLIB). If there is no current library, the table will be created in QGPL. The owner of the table is the user profile or group user profile of the job executing the statement.
If the owner of the table is a member of a group profile (GRPPRF keyword) and group authority is specified (GRPAUT keyword), that group profile will also have authority to the table.
Defines the attributes of a column. There must be at least one column definition and no more than 8000 column definitions.
The sum of the record buffer byte counts of the columns must not be greater than 32766 or, if a VARCHAR or VARGRAPHIC column is specified, 32740. Additionally, if a LOB is specified, the sum record data byte counts of the columns must not be greater than 15 728 640. For information on the byte counts of columns according to data type, see Notes.
If the system-column-name is not specified, and the column-name is not a valid system-column-name, a system column name is generated. For more information on how system column names are generated, see "Rules for Column Name Generation".
You can use DECIMAL(p) for DECIMAL(p,0), and DECIMAL for DECIMAL(5,0).
You can use NUMERIC(p) for NUMERIC(p,0), and NUMERIC for NUMERIC(5,0).
A DATALINK value is an encapsulated value with a set of built-in scalar functions. The DLVALUE function creates a DATALINK value. The following functions can be used to extract attributes from a DATALINK value.
A DataLink cannot be part of any index. Therefore, it cannot be included as a column of a primary key, foreign key, or unique constraint.
| Data type | Default value |
|---|---|
| Numeric | 0 |
| Fixed-length string | Blanks |
| Varying-length string | A string length of 0 |
| Date | The current date at the time of INSERT |
| Time | The current time at the time of INSERT |
| Timestamp | The current timestamp at the time of INSERT |
| Datalink | A value corresponding to DLVALUE('','URL','') |
| distinct-type | The default value of the corresponding source type of the distinct type. |
Omission of NOT NULL and DEFAULT from a column-definition is an implicit specification of DEFAULT NULL.
| Data Type | Cast Function Name | ||
|---|---|---|---|
| Distinct type N based on a BLOB, CLOB, or DBCLOB | BLOB, CLOB, or DBCLOB * | ||
| Distinct type N based on a DATE, TIME, or TIMESTAMP | N (the user-defined cast function that was generated when N was created)
**
or DATE, TIME, or TIMESTAMP * | ||
| Distinct type N based on other data types | N (the user-defined cast function that was generated when N was created) ** | ||
| BLOB, CLOB, or DBCLOB | BLOB, CLOB, or DBCLOB * | ||
| DATE, TIME, or TIMESTAMP | DATE, TIME, or TIMESTAMP * | ||
| |||
If FILE LINK CONTROL is specified, each file can only be linked once. That is, its URL can only be specified in a single FILE LINK CONTROL column in a single table.
If the clause is not specified, a unique constraint name is generated by the database manager.
This clause must not be specified in more than one column definition and must not be specified at all if the UNIQUE clause is specified in the column definition. When a primary key is added, a CHECK constraint is implicitly added to enforce the rule that the NULL value is not allowed in the column that makes up the primary key.
This clause cannot be specified more than once in a column definition and must not be specified if PRIMARY KEY is specified in the column definition.
If the clause is not specified, a unique constraint name is generated by the database manager.
Each column-name must be an unqualified name that identifies a column of the table. The same column must not be identified more than once. The column must not be a LOB or DATALINK column. The number of identified columns must not exceed 120, and the sum of their lengths must not exceed 2000-n, where n is the number of columns specified that allow nulls. The unique index is created as part of the AS/400 physical file, not a separate AS/400 logical file. When a primary key is added, a CHECK constraint is implicitly added to enforce the rule that the NULL value is not allowed in any of the columns that make up the primary key.
Each column-name must be an unqualified name that identifies a column of the table. The same column must not be identified more than once. The column must not be a LOB or DATALINK column. The number of identified columns must not exceed 120, and the sum of their lengths must not exceed 2000-n, where n is the number of columns specified that allows nulls.
A unique index on the identified column is created during the execution of the CREATE TABLE statement. The unique index is created as part of the AS/400 physical file, not as a separate AS/400 logical file.
If the clause is not specified, a unique constraint name is generated by the database manager.
A referential constraint is a duplicate if its foreign key, parent key, and parent table are the same as the foreign key, parent key, and parent table of a previously specified referential constraint. Duplicate referential constraints are allowed, but not recommended.
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. The description of the nth column of the foreign key and the description of the nth column of that parent key must have identical data types and lengths.
The list of column names must be identical to the list of column names in the primary key of T2 or a UNIQUE constraint that exists on T2. The names need not be specified in the same order as in the primary key; however, they must be specified in corresponding order to the list of columns in the foreign key clause. If a column name list is not specified, then T2 must have a primary key. Omission of the column name list is an implicit specification of the columns of that primary key.
The referential constraint specified by a FOREIGN KEY clause defines a relationship in which T2 is the parent and T1 is the dependent.
SET NULL must not be specified unless some column of the foreign key allows null values.
CASCADE must not be specified if T1 contains a DataLink column with FILE LINK CONTROL.
The delete rule applies when a row of T2 is the object of a DELETE or propagated delete operation and that row has dependents in T1. Let p denote such a row of T2.
The update rule applies when a row of T2 is the object of an UPDATE or propagated update operation and that row has dependents in T1. Let p denote such a row of T2.
If the clause is not specified, a unique constraint name is generated by the database manager.
The check-condition is a search-condition except:
For more information on search-condition, see Search Conditions. For more information on check constraints involving LOB data types and expressions, see the Database Programming book.
A LOB or DATALINK column is not allowed in a distributed table.
The DB2 Multisystem product must be installed to create a distributed table. For more information on distributed tables, see the DB2 Multisystem book.
The columns that make up the partitioning key must be a subset of the columns that make up any unique constraints over the table. Floating point, date, time, and timestamp columns cannot be used in a partitioning key.
Tables are created as physical files. If SQL names are used, tables are created with the system authority of *EXCLUDE on *PUBLIC. If system names are used, tables are created with the authority to *PUBLIC as determined by the create authority (CRTAUT) parameter of the collection or library. When a table is created, journaling is automatically started on the journal named QSQJRN in the collection or library.
When a table is created, the file wait time and record wait time attributes are set to the default that is specified on the WAITFILE and WAITRCD keywords of the Create Physical File (CRTPF) command.
There are two maximum record size restrictions referred to in the description of column-definition.
To determine the length of a record buffer and/or record data add the corresponding length of each column of that record based on the byte counts of the data type.
The follow table gives the byte counts of columns by data type for columns
that do not allow null values. If any column allows null values, one
byte is required for every eight columns.
| Data Type | Record Buffer Byte Count | Record Data Byte Count | ||
|---|---|---|---|---|
| SMALLINT | 2 | 2 | ||
| INTEGER | 4 | 4 | ||
| BIGINT | 8 | 8 | ||
| DECIMAL( p, s) | The integral part of (p/2) + 1 | The integral part of (p/2) + 1 | ||
| NUMERIC( p, s) | p | p | ||
| FLOAT (single precision) | 4 | 4 | ||
| FLOAT (double precision) | 8 | 8 | ||
| BLOB( n) | 29+pad | n+29 | ||
| CHAR( n) | n | n | ||
| VARCHAR( n) | n+2 | n+2 | ||
| CLOB( n) | 29+pad | n+29 | ||
| GRAPHIC(n) | n*2 | n*2 | ||
| VARGRAPHIC (n) | n*2+2 | n*2+2 | ||
| DBCLOB( n) | 29+pad | n*2+29 | ||
| DATE | 10 | 4 | ||
| TIME | 8 | 3 | ||
| TIMESTAMP | 26 | 10 | ||
| DATALINK( n) | n+24 | n+24 | ||
| distinct-type | The byte count for the source type. | The byte count for the source type. | ||
| ||||
The length of a LONG column is determined as follows. Let:
The length of each LONG VARCHAR column is INTEGER((m-24-i-((k+7)/8))/j).
The length of each LONG VARGRAPHIC column is determined by taking the length calculated for a LONG VARCHAR column and dividing it by 2. The integer portion of the result is the length.
SQL tables are created so that space used by deleted rows will be reclaimed by future insert requests. This attribute can be changed via the AS/400 command CHGPF and specifying the REUSEDLT(*NO) parameter. For more information on the CHGPF command, see the CL Reference information in the Programming category of the AS/400 Information Center.
There are specific instances when the system generates a system table, view, index, or column name. These instances and the name generation rules are described in the following sections.
A system-column-name is generated if the system-column-name is not specified when a table or view is created and the column-name is not a valid system-column-name.
If the column-name does not contain special characters and is longer than 10 characters, a 10-character system-column-name will be generated as:
For example:
The system-column-name for LONGCOLUMNNAME would be LONGC00001
If the column name is delimited:
For example:
The system-column-name for "abc" would be ABC__00001 The system-column-name for "COL2.NAME" would be COL2_00001 The system-column-name for "C 3" would be C_3__00001 The system-column-name for "??" would be Q____00001 The system-column-name for "*column1" would be QCOLU00001
A system name will be generated if a table, view, alias, or index is created with either:
The SQL name or its corresponding system name may both be used in SQL statements to access the file once it is created. However, the SQL name is only recognized by DB2 UDB for AS/400 and the system name must be used in other environments.
If the name does not contain special characters and is longer than 10 characters, a 10-character system name will be generated as:
For example:
The system name for LONGTABLENAME would be LONGT00001
If the SQL name contains special characters, the system name is generated as:
In addition:
For example:
The system name for "??" would be "__0001" The system name for "longtablename" would be "long0001" The system name for "LONGTableName" would be LONG0001 The system name for "A b " would be "A_b0001"
SQL ensures the system name is unique by searching the cross reference file. If the name already exists in the cross reference file, the number is incremented until the name is no longer a duplicate.
Given that you have administrative authority, create a table named 'ROSSITER.INVENTORY' with the following columns:
The primary key is PARTNO.
CREATE TABLE ROSSITER.INVENTORY
(PARTNO SMALLINT NOT NULL,
DESCR VARCHAR(24 ),
QONHAND INT,
PRIMARY KEY(PARTNO))
Create a table named CORPDATA.DEPT with the following columns:
CREATE TABLE CORPDATA.DEPT
(DEPTNO CHAR(3) NOT NULL,
DEPTNAME VARCHAR(36) NOT NULL,
MGRNO CHAR(6),
ADMRDEPT CHAR(3) NOT NULL)