The ALTER TABLE statement modifies existing tables by:
This statement can be used in an application using DB2® CLI functions or issued through the DB2eCLP application.
>>-ALTER TABLE--table-name--------------------------------------> .----------------------------------------. V | >--+---ADD--+--------+--| column-definition |-+---+------------>< | '-COLUMN-' | | .------------------------------------------. | | V | | '---ALTER--+--------+--| column-alteration |-+-' '-COLUMN-' column-definition: |--column-name--| data-type |--| column-options |---------------| column-alteration: |--column-name--SET DATA TYPE VARCHAR--(--integer--)------------| column-options: .---------------------------------------------. V | |----+-----------------------------------------+-+--------------| +-NOT NULL--------------------------------+ +-+-REFERENCES--table-name-------+--------+ | '-CHECK--(--check-condition--)-' | '-DEFAULT--+-constant-------------------+-' +-datetime-special-register--+ '-cast-function-(-constant-)-'
You must use delimited identifiers (with double quotation marks) 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.
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 use delimited identifiers when a column name contains blanks or special characters.
The column name can include DBCS characters.
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.
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.
Example: 1.234 cannot be the default for a DECIMAL(5,2) column.
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 32767 bytes for this combined check constraint.
| 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 |
The following example shows some of the ways that ALTER TABLE can be used.
CREATE TABLE t1 (c1 INT PRIMARY KEY NOT NULL, c2 VARCHAR(10)); CREATE TABLE t2 (c1 DATE); CREATE TABLE t3 (c1 TIME, c2 INT PRIMARY KEY NOT NULL); ALTER TABLE t2 ADD COLUMN c2 INT REFERENCES t1; ALTER TABLE t2 ADD c3 INT CHECK (c3 > 1) DEFAULT 10 ADD c4 DECIMAL(5,2) NOT NULL; ALTER TABLE t2 ADD c5 TIMESTAMP DEFAULT CURRENT TIMESTAMP ADD COLUMN c6 CHAR(20) DEFAULT 'xyz' ADD c7 INT REFERENCES t3; CREATE TABLE t4 (c1 INT, c2 VARCHAR(2), c3 VARCHAR(10)); ALTER TABLE t1 ALTER c2 SET DATA TYPE VARCHAR(20) ALTER c3 SET DATA TYPE VARCHAR(100);