The ALTER TABLE statement alters the definition of a table.
This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared only if DYNAMICRULES run behavior is in effect for the package (SQLSTATE 42509).
To add a column of type DB2SECURITYLABEL to a table, the privileges held by the authorization ID of the statement must include at least a security label from the security policy associated with the table.
To remove the security policy from a table, the privileges held by the authorization ID of the statement must include SECADM authority.
>>-ALTER TABLE--table-name--------------------------------------> .-------------------------------------------------------------------------. V .-COLUMN-. | >--+-------+-ADD--+-+--------+--| column-definition |-+--------------------------+-+-----+->< | | +-| unique-constraint |-------------+ | | | | +-| referential-constraint |--------+ | | | | +-| check-constraint |--------------+ | | | | +-| distribution-clause |-----------+ | | | | '-RESTRICT ON DROP------------------' | | | | .-MATERIALIZED-. | | | | .-+--------------+--QUERY-. | | | +-ADD--+-------------------------+--| materialized-query-definition |-+ | | +-ALTER--+-FOREIGN KEY-+--constraint-name--| constraint-alteration |--+ | | | '-CHECK-------' | | | | .-COLUMN-. | | | +-ALTER--+--------+--| column-alteration |----------------------------+ | | | .-COLUMN-. | | | +-RENAME--+--------+--source-column-name--TO--target-column-name------+ | | +-DROP--+-PRIMARY KEY---------------------------+---------------------+ | | | +-+-FOREIGN KEY-+--constraint-name------+ | | | | | +-UNIQUE------+ | | | | | | +-CHECK-------+ | | | | | | '-CONSTRAINT--' | | | | | | .-COLUMN-. .-CASCADE--. | | | | | +-+--------+--column-name--+----------+-+ | | | | | '-RESTRICT-' | | | | | '-RESTRICT ON DROP----------------------' | | | +-DROP DISTRIBUTION---------------------------------------------------+ | | | .-MATERIALIZED-. | | | +-DROP--+--------------+--QUERY---------------------------------------+ | | +-DATA CAPTURE--+-NONE---------------------------------+--------------+ | | | '-CHANGES--+-------------------------+-' | | | | '-INCLUDE LONGVAR COLUMNS-' | | | +-ACTIVATE NOT LOGGED INITIALLY--+------------------+-----------------+ | | | '-WITH EMPTY TABLE-' | | | +-PCTFREE--integer----------------------------------------------------+ | | +-LOCKSIZE--+-ROW---------+-------------------------------------------+ | | | +-BLOCKINSERT-+ | | | | '-TABLE-------' | | | +-APPEND--+-ON--+-----------------------------------------------------+ | | | '-OFF-' | | | | .-CARDINALITY-. | | | +-+-VOLATILE-----+--+-------------+-----------------------------------+ | | | '-NOT VOLATILE-' | | | +-COMPRESS--+-YES-+---------------------------------------------------+ | | | '-NO--' | | | +-+-ACTIVATE---+--VALUE COMPRESSION-----------------------------------+ | | | '-DEACTIVATE-' | | | '-LOG INDEX BUILD--+-NULL-+-------------------------------------------' | | +-OFF--+ | | '-ON---' | +-ADD PARTITION--| add-partition |----------------------------------------------------+ +-ATTACH PARTITION--| attach-partition |----------------------------------------------+ +-DETACH PARTITION--partition-name--INTO--table-name1---------------------------------+ +-ADD SECURITY POLICY--policy-name----------------------------------------------------+ '-DROP SECURITY POLICY----------------------------------------------------------------' add-partition |--+----------------+--| boundary-spec |--+---------------------+--> '-partition-name-' '-IN--tablespace-name-' >--+---------------------------------------------------------+--| '-INDEX IN--tablespace-name--+--------------------------+-' '-LONG IN--tablespace-name-' boundary-spec |--+-| starting-clause |--| ending-clause |-+-------------------| '-| ending-clause |----------------------' starting-clause .-,------------. .-FROM-. V | |--STARTING--+------+--+-(----+-constant-+-+--)-+---------------> | +-MINVALUE-+ | | '-MAXVALUE-' | '-+-constant-+-----------' +-MINVALUE-+ '-MAXVALUE-' .-INCLUSIVE-. >--+-----------+------------------------------------------------| '-EXCLUSIVE-' ending-clause .-,------------. .-AT-. V | .-INCLUSIVE-. |--ENDING--+----+--+-(----+-constant-+-+--)-+--+-----------+----| | +-MINVALUE-+ | '-EXCLUSIVE-' | '-MAXVALUE-' | '-+-constant-+-----------' +-MINVALUE-+ '-MAXVALUE-' attach-partition |--+----------------+--| boundary-spec |--FROM--table-name------> '-partition-name-' .-BUILD MISSING INDEXES----. >--+--------------------------+---------------------------------| '-REQUIRE MATCHING INDEXES-' column-definition |--column-name--+-------------------+--+--------------------+---| | (1) | '-| column-options |-' '-| data-type |-----' column-options .--------------------------------------------------------------------------------------------------. V | |----+----------------------------------------------------------------------------------------------+-+--| +-NOT NULL-------------------------------------------------------------------------------------+ | (2) | +-| lob-options |------------------------------------------------------------------------------+ | (3) | +-SCOPE--+-typed-table-name2-+-----------------------------------------------------------------+ | '-typed-view-name2--' | +-+-----------------------------+--+-+-PRIMARY KEY-+-----------------------------------------+-+ | '-CONSTRAINT--constraint-name-' | '-UNIQUE------' | | | +-| references-clause |-----------------------------------+ | | '-CHECK--(--check-condition--)--| constraint-attributes |-' | +-| generated-column-definition |--------------------------------------------------------------+ +-COMPRESS SYSTEM DEFAULT----------------------------------------------------------------------+ | .-COLUMN-. | +-+--------+--SECURED WITH--security-label-name------------------------------------------------+ | .-NOT HIDDEN------------. | | | (4) | | '-+-IMPLICITLY HIDDEN-----+--------------------------------------------------------------------' lob-options .-LOGGED-----. .-NOT COMPACT-. |--●--+------------+--●--+-------------+--●---------------------| '-NOT LOGGED-' '-COMPACT-----' references-clause |--REFERENCES--+-table-name-+--+-----------------------+--------> '-nickname---' | .-,-----------. | | V | | '-(----column-name-+--)-' >--| rule-clause |--| constraint-attributes |-------------------| rule-clause .-ON DELETE NO ACTION-----. .-ON UPDATE NO ACTION-. |--●--+-------------------------+--●--+---------------------+--●--| '-ON DELETE--+-RESTRICT-+-' '-ON UPDATE RESTRICT--' +-CASCADE--+ '-SET NULL-' constraint-attributes .-ENFORCED-----. .-ENABLE QUERY OPTIMIZATION--. |--●--+--------------+--●--+----------------------------+--●----| '-NOT ENFORCED-' '-DISABLE QUERY OPTIMIZATION-' generated-column-definition |--+-| default-clause |--------------------------------------------+--| | .-ALWAYS-----. | +-GENERATED--+------------+--| as-row-change-timestamp-clause |-+ | '-BY DEFAULT-' | | .-ALWAYS-. | '-GENERATED--+--------+--AS--(--generation-expression--)--------' default-clause .-WITH-. |--+------+--DEFAULT--+----------------------------------------------------+--| +-constant-------------------------------------------+ +-datetime-special-register--------------------------+ +-user-special-register------------------------------+ +-CURRENT SCHEMA-------------------------------------+ +-NULL-----------------------------------------------+ +-cast-function--(--+-constant------------------+--)-+ | +-datetime-special-register-+ | | +-user-special-register-----+ | | '-CURRENT SCHEMA------------' | +-EMPTY_CLOB()---------------------------------------+ +-EMPTY_DBCLOB()-------------------------------------+ '-EMPTY_BLOB()---------------------------------------' unique-constraint |--+-----------------------------+--+-UNIQUE------+-------------> '-CONSTRAINT--constraint-name-' '-PRIMARY KEY-' .-,-----------. V | >--(----column-name-+--)----------------------------------------| referential-constraint |--+-----------------------------+------------------------------> '-CONSTRAINT--constraint-name-' .-,-----------. V | >--FOREIGN KEY--(----column-name-+--)--| references-clause |----| check-constraint |--+-----------------------------+------------------------------> '-CONSTRAINT--constraint-name-' >--CHECK--(--| check-condition |--)-----------------------------> >--| constraint-attributes |------------------------------------| check-condition |--+-search-condition----------+--------------------------------| '-| functional-dependency |-' functional-dependency |--+-column-name-----------+--DETERMINED BY--+-column-name-----------+--| | .-,-----------. | | .-,-----------. | | V | | | V | | '-(----column-name-+--)-' '-(----column-name-+--)-' distribution-clause .-,-----------. .-HASH-. V | |--DISTRIBUTE BY--+------+--(----column-name-+--)---------------| materialized-query-definition |--(--fullselect--)--| refreshable-table-options |--------------| refreshable-table-options |--●--DATA INITIALLY DEFERRED--●--REFRESH--+-DEFERRED--+--●-----> '-IMMEDIATE-' .-ENABLE QUERY OPTIMIZATION--. >--+----------------------------+--●----------------------------> '-DISABLE QUERY OPTIMIZATION-' .-MAINTAINED BY SYSTEM--------------. >--+-----------------------------------+--●---------------------| '-MAINTAINED BY--+-USER-----------+-' '-FEDERATED_TOOL-' constraint-alteration .-----------------------------------------. V (5) | |--------+-+-ENABLE--+--QUERY OPTIMIZATION-+-+------------------| | '-DISABLE-' | '-+-----+--ENFORCED---------------' '-NOT-' column-alteration |--column-name--------------------------------------------------> >--+-SET--+-DATA TYPE--| altered-data-type |-----------+--------+--| | +-| generated-column-alteration |------------+ | | +-EXPRESSION AS--(--generation-expression--)-+ | | +-INLINE LENGTH--integer---------------------+ | | '-NOT NULL-----------------------------------' | +-+-| generation-alteration |--+-------------------------+-+-+ | | '-| identity-alteration |-' | | | '-| identity-alteration |--------------------------------' | +-DROP--+-IDENTITY---+---------------------------------------+ | +-EXPRESSION-+ | | +-DEFAULT----+ | | '-NOT NULL---' | +-ADD SCOPE--+-typed-table-name-+----------------------------+ | '-typed-view-name--' | +-COMPRESS--+-SYSTEM DEFAULT-+-------------------------------+ | '-OFF------------' | +-SECURED WITH--security-label-name--------------------------+ '-DROP COLUMN SECURITY---------------------------------------' altered-data-type >>-| built-in-type |------------------------------------------->< built-in-type |--+-+-+-INTEGER-+-+-------------------------------------------------+--| | | '-INT-----' | | | '-BIGINT------' | | .-(5,0)-------------------. | +-+-+-DECIMAL-+-+--+-------------------------+--------------------+ | | '-DEC-----' | | .-,0-------. | | | '-+-NUMERIC-+-' '-(integer-+----------+-)-' | | '-NUM-----' '-,integer-' | | .-(53)------. | +-+-FLOAT--+-----------+--+---------------------------------------+ | | '-(integer)-' | | | +-REAL------------------+ | | | .-PRECISION-. | | | '-DOUBLE--+-----------+-' | | .-(34)-. | +-DECFLOAT--+------+----------------------------------------------+ | '-(16)-' | | .-(1)-------. | +-+-+-+-CHARACTER-+--+-----------+----------+--+--------------+-+-+ | | | '-CHAR------' '-(integer)-' | '-FOR BIT DATA-' | | | | '-+-VARCHAR----------------+--(integer)-' | | | | '-+-CHARACTER-+--VARYING-' | | | | '-CHAR------' | | | | .-(1M)-------------. | | | '-+-CLOB------------------------+--+------------------+-------' | | '-+-CHARACTER-+--LARGE OBJECT-' '-(integer-+---+-)-' | | '-CHAR------' +-K-+ | | +-M-+ | | '-G-' | | .-(1)-------. | +-+-GRAPHIC--+-----------+-------+--------------------------------+ | | '-(integer)-' | | | +-VARGRAPHIC--(integer)--------+ | | | .-(1M)-------------. | | | '-DBCLOB--+------------------+-' | | '-(integer-+---+-)-' | | +-K-+ | | +-M-+ | | '-G-' | | .-(1M)-------------. | '-+-BLOB----------------+--+------------------+-------------------' '-BINARY LARGE OBJECT-' '-(integer-+---+-)-' +-K-+ +-M-+ '-G-' generated-column-alteration |--+-| default-clause |-------------------------------------+---| | .-ALWAYS-----. | +-GENERATED--+------------+--| identity-options |--------+ | '-BY DEFAULT-' | | .-ALWAYS-. | '-GENERATED--+--------+--AS--(--generation-expression--)-' default-clause .-WITH-. |--+------+--DEFAULT--+----------------------------------------------------+--| +-constant-------------------------------------------+ +-datetime-special-register--------------------------+ +-user-special-register------------------------------+ +-CURRENT SCHEMA-------------------------------------+ +-NULL-----------------------------------------------+ +-cast-function--(--+-constant------------------+--)-+ | +-datetime-special-register-+ | | +-user-special-register-----+ | | '-CURRENT SCHEMA------------' | +-EMPTY_CLOB()---------------------------------------+ +-EMPTY_DBCLOB()-------------------------------------+ '-EMPTY_BLOB()---------------------------------------' identity-options |--AS IDENTITY--------------------------------------------------> >--+------------------------------------------------------+-----| | .--------------------------------------------. | | V (5) .-1----------------. | | '-(--------+-START WITH--+-numeric-constant-+---+-+--)-' | .-1----------------. | +-INCREMENT BY--+-numeric-constant-+-+ | .-NO MINVALUE----------------. | +-+-MINVALUE--numeric-constant-+-----+ | .-NO MAXVALUE----------------. | +-+-MAXVALUE--numeric-constant-+-----+ | .-NO CYCLE-. | +-+-CYCLE----+-----------------------+ | .-CACHE 20----------------. | '-+-NO CACHE----------------+--------' '-CACHE--integer-constant-' as-row-change-timestamp-clause (6) |--------FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP---------| generation-alteration |--SET GENERATED--+-ALWAYS-----+--------------------------------| '-BY DEFAULT-' identity-alteration .---------------------------------------------. V (5) | |--------+-SET INCREMENT BY--numeric-constant--+-+--------------| +-SET--+-NO MINVALUE----------------+-+ | '-MINVALUE--numeric-constant-' | +-SET--+-NO MAXVALUE----------------+-+ | '-MAXVALUE--numeric-constant-' | +-SET--+-NO CYCLE-+-------------------+ | '-CYCLE----' | +-SET--+-NO CACHE----------------+----+ | '-CACHE--integer-constant-' | +-SET--+-NO ORDER-+-------------------+ | '-ORDER----' | '-RESTART--+------------------------+-' '-WITH--numeric-constant-'
If table-name identifies a materialized query table, alterations are limited to adding or dropping the materialized query, activating not logged initially, adding or dropping RESTRICT ON DROP, and modifying data capture, pctfree, locksize, append, volatile, data row compression, or value compression
If table-name identifies a range-clustered table, alterations are limited to adding, changing, or dropping constraints, activating not logged initially, adding or dropping RESTRICT ON DROP, changing locksize, data capture, or volatile, and setting column default values.
Adding the new column must not make the total byte count of all columns exceed the maximum record size.
IMPLICITLY HIDDEN must only be specified for a ROW CHANGE TIMESTAMP column (SQLSTATE 42867). The ROW CHANGE TIMESTAMP FOR table-designator expression will resolve to an IMPLICITLY HIDDEN ROW CHANGE TIMESTAMP column. Therefore, a ROW CHANGE TIMESTAMP column can be added to a table as IMPLICITLY HIDDEN, and existing applications that do a SELECT * from this table will not need to be modified to handle the column. Using the expression, new applications can always access the column without knowing the column name.
If the constraint name is not specified by the user, an 18 byte long identifier unique within the identifiers of the existing constraints defined on the table is generated by the system. (The identifier consists of "SQL" followed by a sequence of 15 numeric characters that are generated by a timestamp-based function.)
When used with a PRIMARY KEY or UNIQUE constraint, the constraint-name may be used as the name of an index that is created to support the constraint. See Notes for details on index names associated with unique constraints.
See PRIMARY KEY within the description of the unique-constraint below.
See UNIQUE within the description of the unique-constraint below.
See references-clause in "CREATE TABLE".
If a column is defined using a distinct type, then the default value of the column is the default value of the source data type cast to the distinct type.
Data Type | Default Value |
---|---|
Numeric | 0 |
Fixed-length character string | Blanks |
Varying-length character string | A string of length 0 |
Fixed-length graphic string | Double-byte blanks |
Varying-length graphic string | A string of length 0 |
Date | For existing rows, a date corresponding to January 1, 0001. For added rows, the current date. |
Time | For existing rows, a time corresponding to 0 hours, 0 minutes, and 0 seconds. For added rows, the current time. |
Timestamp | For existing rows, a date corresponding to January 1, 0001, and a time corresponding to 0 hours, 0 minutes, 0 seconds and 0 microseconds. For added rows, the current timestamp. |
Binary string (blob) | A string of length 0 |
Omission of DEFAULT from a column-definition results in the use of the null value as the default for the column.
Specific types of values that can be specified with the DEFAULT keyword are as follows.
If the value specified is not valid, an error (SQLSTATE 42894) is returned.
Allowing system default values to be stored in this manner causes a slight performance penalty during insert and update operations on the column because of extra checking that is done.
The base data type must not be a DATE, TIME, TIMESTAMP, XML, or structured data type (SQLSTATE 42842). If the base data type is a varying-length string, this clause is ignored. String values of length 0 are automatically compressed if a table has been set with VALUE COMPRESSION.
A check is performed to determine whether an existing index matches the unique key definition (ignoring any INCLUDE columns in the index). An index definition matches if it identifies the same set of columns without regard to the order of the columns or the direction (ASC/DESC) specifications. However, for partitioned tables, non-unique partitioned indexes whose columns are not a superset of the table-partitioning key columns are not considered matching indexes.
A check is performed to determine if an existing index matches the primary key definition (ignoring any INCLUDE columns in the index). An index definition matches if it identifies the same set of columns without regard to the order of the columns or the direction (ASC/DESC) specifications. However, for partitioned tables, non-unique partitioned indexes whose columns are not a superset of the table-partitioning key columns are not considered matching indexes.
Only one primary key can be defined on a table.
as the result columns of fullselect (SQLSTATE 428EW). For details about specifying the fullselect for a materialized query table, see "CREATE TABLE". One additional restriction is that table-name cannot be directly or indirectly referenced in the fullselect.
Specifies the new data type of the column. The new data type must be castable from the existing data type of the column (SQLSTATE 42837). A LOB column cannot be altered to a different data type (SQLSTATE 42837). A non-LOB column cannot be altered to a LOB data type (SQLSTATE 42837).
Altering a string data type that results in the truncation of non-blank characters from existing data is not allowed (SQLSTATE 42837).
The administrative routine SYSPROC.ADMIN_REVALIDATE_DB_OBJECTS can be called to do table reorganization as required. A data type alteration that requires a table reorganization cannot be specified if the table is in SET INTEGRITY PENDING state (SQLSTATE 57007).
A string data type cannot be altered if the column is a column of a table-partitioning key.
The specified length cannot be less than the existing length if the data type is a LOB (SQLSTATE 42837).
The data type of an identity column cannot be altered (SQLSTATE 42997).
The table cannot have data capture enabled (SQLSTATE 42997).
Altering a column must not make the total byte count of all columns exceed the maximum record size (SQLSTATE 54010). If the column is used in a unique constraint or an index, the new length must not cause the sum of the stored lengths for the unique constraint or index to exceed the index key length limit for the page size (SQLSTATE 54008). For column stored lengths, see "Byte Counts" in "CREATE TABLE". For key length limits, see "SQL limits".
If auto_reval is set to DISABLED, the cascaded effects of altering a column is shown in Table 2.
Operation | Effect |
---|---|
Altering a column that is referenced by a view or check constraint | The object is regenerated during alter processing. In the case of a view, function or method resolution for the object might be different after the alter operation, changing the semantics of the object. In the case of a check constraint, if the semantics of the object will change as a result of the alter operation, the operation fails. |
Altering a column in a table that has a dependent package, trigger, or SQL routine | The object is marked invalid, and is revalidated on next use. |
Altering the type of a column in a table that is referenced by an XSROBJECT enabled for decomposition | The XSROBJECT is marked inoperative for decomposition. Re-enabling the XSROBJECT might require readjustment of its mappings; following this, issue an ALTER XSROBJECT ENABLE DECOMPOSITION statement against the XSROBJECT. |
Altering a column that is referenced in the default expression of a global variable | The default expression of the global variable is validated during alter processing. If a user-defined function used in the default expression cannot be resolved, the operation fails. |
The data type of column-name must be a structured type, XML, or LOB data type (SQLSTATE 42842).
The default inline length for a structured type column is the inline length of its data type (specified explicitly or by default in the CREATE TYPE statement). If the inline length of a structured type is less than 292, the value 292 is used for the inline length of the column.
The explicit inline length value can only be increased (SQLSTATE 429B2); it cannot exceed 32673 (SQLSTATE 54010). For a structured type or XML data type column, it must be at least 292. For a LOB data type column, the INLINE LENGTH must not be less than the maximum LOB descriptor size.
Altering the column must not make the total byte count of all columns exceed the row size limit (SQLSTATE 54010).
UPDATE table-name SET lob-column = lob-column
WHERE LENGTH(lob-column) <= chosen-inline-length - 4
where table-name is the table
that had the inline length of the LOB data type column altered, lob-column is
the LOB data type column that was altered, and chosen-inline-length is
the new value that was chosen for the INLINE LENGTH.A table can only have one ROW CHANGE TIMESTAMP column (SQLSTATE 428C1). If data-type is specified, it must be TIMESTAMP or TIMESTAMP(6) (SQLSTATE 42842). A ROW CHANGE TIMESTAMP column cannot have a DEFAULT clause (SQLSTATE 42623). NOT NULL must be specified for a ROW CHANGE TIMESTAMP column (SQLSTATE 42831).
This value can be any positive or negative value that could be assigned to this column (SQLSTATE 42815), and does not exceed the value of a large integer constant (SQLSTATE 42820), without nonzero digits existing to the right of the decimal point (SQLSTATE 428FA).
If this value is negative, this is a descending sequence after the ALTER statement. If this value is 0 or positive, this is an ascending sequence after the ALTER statement.
When CYCLE is in effect, duplicate values can be generated for an identity column. Although not required, if unique values are desired, a single-column unique index defined using the identity column will ensure uniqueness. If a unique index exists on such an identity column and a non-unique value is generated, an error occurs (SQLSTATE 23505).
When this option is specified, the values of the identity column are not stored in the cache. In this case, every request for a new identity value results in synchronous I/O to the log.
If a new value is needed for the identity column and there are no unused values available in the cache, the allocation of the value requires waiting for I/O to the log. However, when a new value is needed for the identity column and there is an unused value in the cache, the allocation of that identity value can happen more quickly by avoiding the I/O to the log.
In the event of a database deactivation, either normally or due to a system failure, all cached sequence values that have not been used in committed statements are lost (that is, they will never be used). The value specified for the CACHE option is the maximum number of values for the identity column that could be lost in case of system failure.
The minimum value is 2 (SQLSTATE 42815).
The column must exist in the specified table (SQLSTATE 42703), and must already be defined with the IDENTITY attribute (SQLSTATE 42837). RESTART does not change the original START WITH value.
The numeric-constant is an exact numeric constant that can be any positive or negative value that could be assigned to this column (SQLSTATE 42815), without nonzero digits existing to the right of the decimal point (SQLSTATE 428FA). The numeric-constant will be used as the next value for the column.
Allowing system default values to be stored in this manner causes a slight performance penalty during insert and update operations on the column because of the extra checking that is done.
Existing data in the column is not changed. Consider offline table reorganization to enable existing data to take advantage of storing system default values using minimal space.
The base data type must not be DATE, TIME or TIMESTAMP (SQLSTATE 42842). If the base data type is a varying-length string, this clause is ignored. String values of length 0 are automatically compressed if a table has been set with VALUE COMPRESSION.
If the table being altered is a typed table, the column must not be inherited from a supertable (SQLSTATE 428DJ).
Operation | RESTRICT Effect | CASCADE Effect |
---|---|---|
Dropping a column that is referenced by a view or a trigger | Dropping the column is not allowed. | The object and all objects that are dependent on that object are dropped. |
Dropping a column that is referenced in the key of an index | If all columns that are referenced in the index are dropped in the same ALTER TABLE statement, dropping the index is allowed. Otherwise, dropping the column is not allowed. | The index is dropped. |
Dropping a column that is referenced in a unique constraint | If all columns that are referenced in the unique constraint are dropped in the same ALTER TABLE statement, and the unique constraint is not referenced by a referential constraint, the columns and the constraint are dropped. (The index that is used to satisfy the constraint is also dropped.) Otherwise, dropping the column is not allowed. | The unique constraint and any referential constraints that reference that unique constraint are dropped. (Any indexes that are used by those constraints are also dropped). |
Dropping a column that is referenced in a referential constraint | If all columns that are referenced in the referential constraint are dropped in the same ALTER TABLE statement, the columns and the constraint are dropped. Otherwise, dropping the column is not allowed. | The referential constraint is dropped. |
Dropping a column that is referenced by a system-generated column that is not being dropped. | Dropping the column is not allowed. | Dropping the column is not allowed. |
Dropping a column that is referenced in a check constraint | Dropping the column is not allowed. | The check constraint is dropped. |
Dropping a column that is referenced in a decomposition-enabled XSROBJECT | Dropping the column is not allowed. | The XSROBJECT is marked inoperative for decomposition. Re-enabling the XSROBJECT might require readjustment of its mappings; following this, issue an ALTER XSROBJECT ENABLE DECOMPOSITION statement against the XSROBJECT. |
Dropping a column that is referenced in the default expression of a global variable | Dropping the column is not allowed. | The global variable is dropped, unless the dropping of the global variable is disallowed because there are other objects, which do not allow the cascade, that depend on the global variable. |
If the table is a typed table, then this option is not supported (SQLSTATE 428DH for root tables or 428DR for other subtables).
Any changes made to the table by an INSERT, DELETE, UPDATE, CREATE INDEX, DROP INDEX, or ALTER TABLE in the same unit of work after the table is altered by this statement are not logged. Any changes made to the system catalog by the ALTER statement in which the NOT LOGGED INITIALLY attribute is activated are logged. Any subsequent changes made in the same unit of work to the system catalog information are logged.
At the completion of the current unit of work, the NOT LOGGED INITIALLY attribute is deactivated and all operations that are done on the table in subsequent units of work are logged.
If using this feature to avoid locks on the catalog tables while inserting data, it is important that only this clause be specified on the ALTER TABLE statement. Use of any other clause in the ALTER TABLE statement will result in catalog locks. If no other clauses are specified for the ALTER TABLE statement, then only a SHARE lock will be acquired on the system catalog tables. This can greatly reduce the possibility of concurrency conflicts for the duration of time between when this statement is executed and when the unit of work in which it was executed is ended.
If the table is a typed table, this option is only supported on the root table of the typed table hierarchy (SQLSTATE 428DR).
For more information about the NOT LOGGED INITIALLY attribute, see the description of this attribute in "CREATE TABLE".
When this action is requested, no DELETE triggers defined on the affected table are fired. The index data is also deleted for all indexes that exist on the table.
A partitioned table with attached data partitions or logically detached partitions cannot be emptied (SQLSTATE 42928).
The table should be reorganized after setting APPEND OFF since the information about available free space is not accurate and may result in poor performance during insert.
If the starting-clause is omitted, the new data partition is assumed to be at the end of the table. If the ending-clause is omitted, the new data partition is assumed to be at the start of the table.
The table space used by the new index partition, whether default or specified by the INDEX IN clause, must match the type (SMS or DMS), page size, and extent size of the table spaces used by all other index partitions (SQLSTATE 42838).
For rules governing the use of the LONG IN clause with partitioned tables, see "Large object behavior in partitioned tables".
If the starting-clause is omitted, the new data partition is assumed to be at the end of the table. If the ending-clause is omitted, the new data partition is assumed to be at the start of the table.
If the change data table is not altered before the Capture program processes log records with the altered attributes, the Capture program will likely fail. If a copy containing the altered column is not altered before the subscription maintaining the copy runs, the subscription will likely fail.
Issuing the SET INTEGRITY statement on the target table is required to bring the attached partition online.
If the REQUIRE MATCHING INDEXES clause is specified, and the target table is a partitioned MDC table created in DB2 V9.7 Fix Pack 1 or later releases, the ALTER TABLE ... ATTACH PARTITION statement fails and returns SQL20307N (SQLSTATE 428GE). Removing the REQUIRE MATCHING INDEXES clause allows the attach process to proceed.
If the target partitioned MDC table is created prior to DB2 V9.7 Fix Pack 1, the block indexes are nonpartitioned. The block indexes on the source MDC table are dropped during the ATTACH operation. Issuing a SET INTEGRITY statement on the target table is required to bring the attached partition online. The block indexes are created as nonpartitioned block indexes.
When detaching a partition from a partitioned MDC table created using DB2 V9.7 Fix Pack 1 or later releases, the block indexes are partitioned, and the previous restrictions do not apply. Assuming that no other dependent objects such as dependent MQTs exist, access to the newly detached table is allowed in the same unit of work. All the partitioned indexes, including block indexes, become indexes on the target table without the need to be re-created.
ALTER TABLE DEPARTMENT
ADD RATING CHAR(1)
ALTER TABLE PROJECT
ADD SITE_NOTES VARCHAR(1000) FOR BIT DATA
Column Name Data Type
EQUIP_NO INT
EQUIP_DESC VARCHAR(50)
LOCATION VARCHAR(50)
EQUIP_OWNER CHAR(3)
Add a referential constraint
to the EQUIPMENT table so that the owner (EQUIP_OWNER) must be a department
number (DEPTNO) that is present in the DEPARTMENT table. DEPTNO is
the primary key of the DEPARTMENT table. If a department is removed
from the DEPARTMENT table, the owner (EQUIP_OWNER) values for all
equipment owned by that department should become unassigned (or set
to null). Give the constraint the name DEPTQUIP. ALTER TABLE EQUIPMENT
ADD CONSTRAINT DEPTQUIP
FOREIGN KEY (EQUIP_OWNER)
REFERENCES DEPARTMENT
ON DELETE SET NULL
ALTER TABLE EQUIPMENT
ADD COLUMN EQUIP_QTY
SMALLINT NOT NULL DEFAULT 1
ALTER TABLE EMPLOYEE
ADD CONSTRAINT REVENUE
CHECK (SALARY + COMM > 30000)
ALTER TABLE EMPLOYEE
DROP CONSTRAINT REVENUE
ALTER TABLE SALARY1
DATA CAPTURE NONE
ALTER TABLE SALARY2
DATA CAPTURE CHANGES
ALTER TABLE EMPLOYEE
ADD COLUMN HEIGHT MEASURE DEFAULT MEASURE(1)
ADD COLUMN BIRTHDAY BIRTHDATE DEFAULT DATE('01-01-1850')
ADD COLUMN FLAGS BLOB(1M) DEFAULT BLOB(X'01')
ADD COLUMN PHOTO PICTURE DEFAULT BLOB(X'00')
The
default values use various function names when specifying the default.
Since MEASURE is a distinct type based on INTEGER, the MEASURE function
is used. The HEIGHT column default could have been specified without
the function since the source type of MEASURE is not BLOB or a datetime
data type. Since BIRTHDATE is a distinct type based on DATE, the DATE
function is used (BIRTHDATE cannot be used here). For the FLAGS and
PHOTO columns the default is specified using the BLOB function even
though PHOTO is a distinct type. To specify a default for BIRTHDAY,
FLAGS and PHOTO columns, a function must be used because the type
is a BLOB or a distinct type sourced on a BLOB or datetime data type. Column Name Data Type
BRANCH_NO SMALLINT
CUSTOMER_NO DECIMAL(7)
CUSTOMER_NAME VARCHAR(50)
ALTER TABLE CUSTOMERS
ADD DISTRIBUTE BY HASH (BRANCH_NO)
ALTER TABLE EMPLOYEE
ADD COLUMN PHONE_NO CHAR(4) NOT NULL
ADD COLUMN WORK_DEPT CHAR(3)
ADD PRIMARY KEY (EMP_NO, WORK_DEPT)
ALTER TABLE DEPARTMENT
ADD CONSTRAINT FD1
CHECK ( DEPTNAME DETERMINED BY DEPTNO) NOT ENFORCED
ALTER TABLE DEPARTMENT
DROP CHECK FD1
ALTER TABLE EMPLOYEE
ALTER COLUMN WORKDEPT
SET DEFAULT '123'
ALTER TABLE EMPLOYEE
ADD SECURITY POLICY DATA_ACCESS
ALTER TABLE EMPLOYEE
ALTER COLUMN SALARY
SECURED WITH EMPLOYEESECLABEL
Column Name Data Type
----------- ---------
EMP_NAME VARCHAR(50) NOT NULL
EMP_ID SMALLINT NOT NULL
EMP_POSITION VARCHAR(100) NOT NULL
SALARY DECIMAL(5,2)
PROMOTION_DATE DATE NOT NULL
Change this table
to allow salaries to be stored in a DECIMAL(6,2) column, make PROMOTION_DATE
an optional field that can be set to the null value, and remove the
EMP_POSITION column. ALTER TABLE SALARY_DATA
ALTER COLUMN SALARY SET DATA TYPE DECIMAL(6,2)
ALTER COLUMN PROMOTION_DATE DROP NOT NULL
DROP COLUMN EMP_POSITION
ALTER TABLE BOOKS
ADD COLUMN DATE_ADDED TIMESTAMP
WITH DEFAULT CURRENT TIMESTAMP