ALTER TABLE

The ALTER TABLE statement adds, drops, or alters a column from an existing table; adds unique, referential, or check constraints; and adds or drops a primary, unique, or foreign key.

Invocation

This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared.

Authorization

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 ALTER privilege on the table when one of the following is true:

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:

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:

Syntax



>>-ALTER TABLE--table-name-------------------------------------->
 
      .-------------------------------------------------------------------------------------------.
      V           .-COLUMN-.                                                                      |
>----------+-ADD--+--------+----column-definition--------------------------------------------+----+>
           |                                       .-------------------------------------.   |
           |       .-COLUMN-.                      V                               (1)   |   |
           +-ALTER-+--------+--column-name--+-SET------+-DATA TYPE--data-type--+---------+-+-+
           |                                |          +-default-clause--------+           | |
           |                                |          '-NOT NULL--------------'           | |
           |                                |       .------------------------.             | |
           |                                |       V                  (2)   |             | |
           |                                '-DROP------+-DEFAULT--+---------+-------------' |
           |                                            '-NOT NULL-'                         |
           |         .-COLUMN-.               .-CASCADE--.                                   |
           +-DROP----+--------+--column-name--+----------+-----------------------------------+
           |                                  '-RESTRICT-'                                   |
           |     (3)                                                                         |
           +-ADD-------+-unique-constraint------+--------------------------------------------+
           |           +-referential-constraint-+                                            |
           |           '-check-constraint-------'                                            |
           |                                                 .-CASCADE--.                    |
           '-DROP--+-PRIMARY KEY-------------------------+---+----------+--------------------'
                   '--+-FOREIGN KEY-+---constraint-name--'   '-RESTRICT-'
                      +-UNIQUE------+
                      +-CHECK-------+
                      '-CONSTRAINT--'
 
>--------------------------------------------------------------><
 
column-definition
 
|---column-name----+-------------------------------------+------>
                   |     .-COLUMN-.                      |
                   '-FOR-+--------+--system-column-name--'
 
>----data-type-------------------------------------------------->
 
      .----------------------------------------------------------------------------.
      V                                                                            |
>--------+----------------------------------------------------------------------+--+>
         +-NOT NULL-------------------------------------------------------------+
         +-default-clause-------------------------------------------------------+
         |                  (4)                                                 |
         +-datalink-options-----------------------------------------------------+
         '-+------------------------------+---+-+-UNIQUE------+---------------+-'
           '-CONSTRAINT--constraint-name--'   | '-PRIMARY KEY-'               |
                                              +-references-clause-------------+
                                              '-CHECK--(--check-condition--)--'
 
>---------------------------------------------------------------|
 


Notes:


  1. Each clause may be specified only once. If DATA TYPE is specified,
    it must be specified first.

  2. Each clause may be specified only once.

  3. If this is the first clause of the ALTER TABLE statement, the ADD keyword
    is optional, but strongly recommended. Otherwise, it is
    required.

  4. The datalink-options can only be specified for DATALINKs and
    distinct-types sourced on DATALINKs.




 
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---------------------------------+
                      '-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    (1)                            |
|-----+------------------------------+--+-----------------------|
      +-INTEGRITY ALL----------------+
      +-+-READ PERMISSION FS-+-------+
      | '-READ PERMISSION DB-'       |
      +-+-WRITE PERMISSION FS------+-+
      | '-WRITE PERMISSION BLOCKED-' |
      +-RECOVERY NO------------------+
      '-+-ON UNLINK RESTORE-+--------'
        '-ON UNLINK DELETE--'
 


Notes:


  1. The file-link-options can be specified in any order.




 
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--)-------------------------------|
 


Notes:


  1. For compatibility with other products, constraint-name (without the
    CONSTRAINT keyword) may be specified following FOREIGN KEY.

  2. The ON DELETE and ON UPDATE clauses may be specified in either
    order.


Description

table-name
Identifies the table you want to be altered. The table-name must identify a table that exists at the current server. It must not be a view or a catalog table.

ADD COLUMN

column-definition
Adds a column to the table. If the table has rows, every value of the column is set to its default value. If the table previously had n columns, the ordinality of the new column is n+1. The value of n must not exceed 8000.

Adding a new column must not make the total byte count of all columns exceed 32766 or, if a VARCHAR or VARGRAPHIC column is specified, 32740.

column-name
Names the column you want to add to the table. Do not use the same name for more than one column of the table or for a system-column-name of the table. Do not qualify column-name.

FOR COLUMN system-column-name

Provides an OS/400 name for the column. Do not use the same name for more than one column-name or system-column-name of the table.

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".

data-type
Specifies the data type of the column. See CREATE TABLE for a description of data-type.

A DataLink column with FILE LINK CONTROL cannot be added to a table that is a dependent in a referential constraint with a delete rule of CASCADE.

NOT NULL
Prevents the column from containing null values. Omission of NOT NULL implies that the column can contain null values. If NOT NULL is specified, DEFAULT must also be specified.

DEFAULT
Specifies a default value for the column. This clause cannot be specified more than once in the column-definition. If a value is not specified and the column is nullable, the default value is the null value. If a value is not specified and the column is not nullable, the default depends on the data type of the column:

Data type
Default value

Numeric
0

Fixed-length string
Blanks

Varying-length string
A string length of 0

Date
For existing rows, a date corresponding to 1 January 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 1 January 0001 and a time corresponding to 0 hours, 0 minutes, 0 seconds, and 0 microseconds. For added rows, the current timestamp.

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.

constant
Specifies the constant as the default for the column. The specified constant must represent a value that could be assigned to the column in accordance with the rules of assignment as described in "Assignments and Comparisons". A floating-point constant must not be used for a SMALLINT, INTEGER, BIGINT, DECIMAL, or NUMERIC column. A decimal constant must not contain more digits to the right of the decimal point than the specified scale of the column.

NULL
Specifies null as the default for the column. If NOT NULL is specified, DEFAULT NULL must not be specified within the same column-definition.

USER
Specifies the value of the USER special register at the time of INSERT or UPDATE as the default value for the column. The data type of the column must be CHAR or VARCHAR with a length attribute greater than or equal to 18.

CURRENT_DATE
Specifies the current date as the default for the column. If CURRENT_DATE is specified, the data type of the column must be DATE.

CURRENT_TIME
Specifies the current time as the default for the column. If CURRENT_TIME is specified, the data type of the column must be TIME.

CURRENT_TIMESTAMP
Specifies the current timestamp as the default for the column. If CURRENT_TIMESTAMP is specified, the data type of the column must be TIMESTAMP.

cast-function-name
This form of a default value can only be used with columns defined as a distinct type, BLOB, CLOB, DBCLOB, DATE, TIME, or TIMESTAMP data types. The following table describes the allowed uses of these cast-functions.
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 *
Notes:

* The name of the function must match the name of the data type (or the source type of the distinct type) with an implicit or explicit schema name of QSYS2

** The name of the function must match the name of the distinct type for the column. If qualified with a collection name, it must be the same as the collection name for the distinct type. If not qualified, the collection name from function resolution must be the same as the collection name for the distinct type.

constant
Specifies a constant as the argument. The constant must conform to the rules of a constant for the source type of the distinct type or for the data type if not a distinct type. For BLOB, CLOB, DBCLOB, DATE, TIME, and TIMESTAMP functions, the constant must be a string constant.

USER
Specifies the value of the USER special register at the time of INSERT or UPDATE as the default value for the column. The data type of the source type of the distinct type of the column must be CHAR or VARCHAR with a length attribute greater than or equal to 18.

CURRENT_DATE
Specifies the current date as the default for the column. If CURRENT_DATE is specified, the data type of the source type of the distinct type of the column must be DATE.

CURRENT_TIME
Specifies the current time as the default for the column. If CURRENT_TIME is specified, the data type of the source type of the distinct type of the column must be TIME.

CURRENT_TIMESTAMP
Specifies the current timestamp as the default for the column. If CURRENT_TIMESTAMP is specified, the data type of the source type of the distinct type of the column must be TIMESTAMP.

datalink-options
Specifies the options associated with a DATALINK column. See CREATE TABLE for a description of datalink-options.

CONSTRAINT constraint-name
Names the constraint. A constraint-name must not identify a constraint that already exists at the current server.

If the clause is not specified, a unique constraint name is generated by the database manager.

PRIMARY KEY
Provides a shorthand method of defining a primary key composed of a single column. Thus, if PRIMARY KEY is specified in the definition of column C, the effect is the same as if the PRIMARY KEY(C) clause is specified as a separate clause.

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.

UNIQUE
Provides a shorthand method of defining a unique key composed of a single column. Thus, if UNIQUE is specified in the definition of column C, the effect is the same as if the UNIQUE(C) clause is specified as a separate clause.

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.

references-clause
The references-clause of a column-definition provides a shorthand method of defining a foreign key composed of a single column. Thus, if a references-clause is specified in the definition of column C, the effect is the same as if that references-clause were specified as part of a FOREIGN KEY clause in which C is the only identified column.

CHECK(check-condition)
Provides a shorthand method of defining a check constraint whose check-condition only references a single column. Thus, if CHECK is specified in the column definition of column C, no columns other than C can be referenced in the check-condition of the check constraint. The effect is the same as if the check constraint were specified as a separate clause.

ALTER COLUMN

Alters the definition of an existing column. Only the attributes specified will be altered. Others will remain unchanged.

column-name
Identifies the column to be altered. The column name must not be qualified. The name must identify a column of the specified table. The name must not identify a column that was already added or dropped in this ALTER TABLE statement.

SET DATA TYPE data-type

Specifies the new data type of the column to be altered. The new data type must be compatible with the existing data type of the column. For more information on the compatibility of data types see "Assignments and Comparisons". However, there are two exceptions to the general rules:

The specified length, precision, and scale may be larger, smaller, or the same as the existing length, precision, and scale. However, if the new length, precision, or scale is smaller, truncation or numeric conversion errors may occur.

If the specified column has a default value and a new default value is not specified, the existing default value must represent a value that could be assigned to the column in accordance with the rules for assignment as described in "Assignments and Comparisons".

If the column is specified in a unique, primary, or foreign key, the new sum of the lengths of the columns of the keys must not exceed 2000-n, where n is the number of columns specified that allow nulls.

Changing the attributes will cause any existing values in the column to be converted to the new column attributes according to the rules for assignment to a column, except that string values will be truncated.

SET default-clause
Specifies the new default value of the column to be altered. The specified default value must represent a value that could be assigned to the column in accordance with the rules for assignment as described in "Assignments and Comparisons".

SET NOT NULL
Specifies that the column cannot contain null values. All values for this column in existing rows of the table must be not null. If the specified column has a default value and a new default value is not specified, the existing default value must not be NULL. SET NOT NULL is not allowed if the column is identified in the foreign key of a referential constraint with a DELETE rule of SET NULL and no other nullable columns exist in the foreign key.

DROP DEFAULT
Drops the current default for the column. The specified column must have a default value and must not have NOT NULL as the null attribute. The new default value is the null value.

DROP NOT NULL
Drops the NOT NULL attribute of the column, allowing the column to have the null value. If a default value is not specified or does not already exist, the new default value is the null value. DROP NOT NULL is not allowed if the column is specified in the primary key of the table.

DROP COLUMN

Drops the identified column from the table.

column-name
Identifies the column to be dropped. The column name must not be qualified. The name must identify a column of the specified table. The name must not identify a column that was already added or altered in this ALTER TABLE statement. The name must not identify the only column of a table.

CASCADE
Specifies that any views, indexes, or constraints that are dependent on the column being dropped are also dropped.

RESTRICT
Specifies that the column cannot be dropped if any views, indexes, or constraints are dependent on the column.

If all the columns referenced in a constraint are dropped in the same ALTER TABLE statement, RESTRICT does not prevent the drop.

ADD unique-constraint

CONSTRAINT constraint-name
Names the constraint. A constraint-name must not identify a constraint that already exists at the current server. The constraint-name must be unique within a collection.

If not specified, a unique constraint name is generated by the database manager.

UNIQUE(column-name,...)
Defines a unique key composed of the identified columns. 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 identified columns cannot be the same as the columns specified in another UNIQUE constraint or PRIMARY KEY on the table. For example, UNIQUE(A,B) is not allowed if UNIQUE(B,A) or PRIMARY KEY(A,B) already exists on the table. Any existing nonnull values in the set of columns must be unique. Multiple null values are allowed.

If a unique index already exists on the identified columns, that index is designated as a unique index. Otherwise, a unique index is created to support the uniqueness of the unique key. The unique index is created as part of the AS/400 physical file, not as a separate AS/400 logical file.

PRIMARY KEY(column-name,...)
Defines a primary key composed of the identified columns. 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. The table must not already have a primary key. The identified columns cannot be the same as the columns specified in another UNIQUE constraint on the table. For example, PRIMARY KEY(A,B) is not allowed if UNIQUE(B,A) already exists on the table. Any existing values in the set of columns must be unique. 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.

If a unique index already exists on the identified columns, that index is designated as a primary index. Otherwise, a primary index is created to support the uniqueness of the primary key. The unique index is created as part of the AS/400 physical file, not a separate AS/400 logical file.

ADD referential-constraint

CONSTRAINT constraint-name

Names the constraint. A constraint-name must not identify a constraint that already exists at the current server.

If not specified, a unique constraint name is generated by the database manager.

FOREIGN KEY
Defines a referential constraint.

Let T1 denote the table being altered.

(column-name,...)
The foreign key of the referential constraint is composed of the identified columns. Each column-name must be an unqualified name that identifies a column of T1. The same column must not be identified more than once. The column must not be a LOB or DATALINK column. The number of the 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.

REFERENCES table-name
The table-name specified in a REFERENCES clause must identify a base table that exists at the current server, but it must not identify a catalog table. This table is referred to as the parent table in the constraint relationship.

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 an existing referential constraint. Duplicate referential constraints are allowed, but not recommended.

Let T2 denote the identified parent table.

(column-name,...)
The parent key of the referential constraint is composed of the identified columns. Each column-name must be an unqualified name that identifies a column of T2. 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 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 may be specified in any order. For example, if (A,B) is specified, a unique constraint defined as UNIQUE(B,A) would satisfy the requirement. 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 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 nth column of the parent key must have identical data types and lengths.

Unless the table is empty, the values of the foreign key must be validated before the table can be used. Values of the foreign key are validated during the execution of the ALTER TABLE statement. Therefore, every nonnull value of the foreign key must match some value of the parent key of T2.

The referential constraint specified by the FOREIGN KEY clause defines a relationship in which T2 is the parent and T1 is the dependent.

ON DELETE

Specifies what action is to take place on the dependent tables when a row of the parent table is deleted.

SET NULL must not be specified unless some column of the foreign key allows null values.

CASCADE must not be specified if T1 has a delete trigger. SET NULL and SET DEFAULT must not be specified if T1 has an update trigger.

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.

  • If RESTRICT or NO ACTION is specified, an error occurs and no rows are deleted.
  • If CASCADE is specified, the delete operation is propagated to the dependents of p in T1.
  • If SET NULL is specified, each nullable column of the foreign key of each dependent of p in T1 is set to null.
  • If SET DEFAULT is specified, each column of the foreign key of each dependent of p in T1 is set to its default value.

ON UPDATE

Specifies what action is to take place on the dependent tables when a row of the parent table is updated.

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 RESTRICT or NO ACTION is specified, an error occurs and no rows are updated.

ADD check-constraint

CONSTRAINT constraint-name
Names the constraint. A constraint-name must not identify a constraint that already exists at the current server. The constraint-name must be unique within a collection.

If not specified, a unique constraint name is generated by the database manager.

CHECK(check-condition)

Defines a check constraint. At any time, the check-condition must be true or unknown for every row of the table.

The check-condition is a search-condition, except:

For more information on search-condition, see Search Conditions.

DROP

PRIMARY KEY
Drops the definition of the primary key and all referential constraints in which the primary key is a parent key. The table must have a primary key.

FOREIGN KEY constraint-name

Drops the referential constraint constraint-name. The constraint-name must identify a referential constraint in which the table is a dependent.

UNIQUE constraint-name

Drops the unique constraint constraint-name and all referential constraints in which the unique key is a parent key. The constraint-name must identify a unique constraint on the table. DROP UNIQUE will not drop a PRIMARY KEY unique constraint.

CHECK constraint-name
Drops the check constraint constraint-name. The constraint-name must identify a check constraint on the table.

CONSTRAINT constraint-name

Drops the constraint constraint-name. If the constraint is a PRIMARY KEY or UNIQUE constraint, all referential constraints in which the primary key or unique key is a parent are also dropped. The constraint-name must identify a check, unique, or referential constraint on the table.

CASCADE
Specifies for unique constraints that any referential constraints that are dependent on the constraint being dropped are also dropped.

RESTRICT
Specifies for unique constraints that the constraint cannot be dropped if any referential constraints are dependent on the constraint.

Notes

A column can only be referenced once in an ADD, ALTER, or DROP COLUMN clause in a single ALTER TABLE statement. However, that same column can be referenced multiple times for adding or dropping constraints in the same ALTER TABLE statement.

The order of operations within an ALTER TABLE statement is:

Within each of these stages, the order in which the user specifies the clauses is the order in which they are performed, with one exception. If any columns are being dropped, that operation is logically done before any column definitions are added or altered, in case record length is increased as a result of the ALTER TABLE statement.

Any views or logical files in another job's QTEMP that are dependent on the table being altered will be dropped as a result of an ALTER TABLE statement.

Authority checking is performed only on the table being altered. Other objects may be accessed by the ALTER TABLE statement, but no authority to those objects is required. For example, no authority is required on views that exist on the table being altered, nor on dependent tables that reference the table being altered through a referential constraint.

It is strongly recommended that a current backup of the table and dependent views and logical files exist prior to altering a table.

The following performance considerations apply to an ALTER TABLE statement when adding, altering, or dropping columns from a table:

Cascaded Effects

Adding a column has no cascaded effects to SQL views or most logical files.31 For example, adding a column to a table does not cause the column to be added to any dependent views, even if those views were created with a SELECT * clause.

Dropping or altering a column may cause several cascaded effects. Table 21 lists the cascaded effects of dropping a column.

Table 21. Cascaded effects of dropping a column
Operation RESTRICT Effect CASCADE Effect
Drop of a column referenced by a view The drop of the column is not allowed. The view and all views dependent on that view are dropped.
Drop of a column referenced by a non-view logical file The drop is allowed, and the column is dropped from the logical file if:
  • The logical file shares a format with the file being altered, and
  • The dropped column is not used as a key field or in select/omit specifications, and
  • That format is not used again in the logical file with another based-on file.
Otherwise, the drop of the column is not allowed.
The drop is allowed, and the column is dropped from the logical file if:
  • The logical file shares a format with the file being altered, and
  • The dropped column is not used as a key field or in select or omit specifications, and
  • That format is not used again in the logical file with another based-on file.
Otherwise, the logical file is dropped.
Drop of a column referenced in the key of an index The drop of the index is not allowed. The index is dropped.
Drop of a column referenced in a unique constraint If all the columns referenced in the unique constraint are dropped in the same ALTER COLUMN statement and the unique constraint is not referenced by a referential constraint, the columns and the constraint are dropped. (Hence, the index used to satisfy the constraint is also dropped.) For example, if column A is dropped, and a unique constraint of UNIQUE(A) or PRIMARY KEY(A) exists and no referential constraints reference the unique constraint, the operation is allowed.

Otherwise, the drop of the column is not allowed.

The unique constraint is dropped as are any referential constraints that refer to that unique constraint. (Hence, any indexes used by those constraints are also dropped).
Drop of a column referenced in a referential constraint If all the columns referenced in the referential constraint are dropped at the same time, the columns and the constraint are dropped. (Hence, the index used by the foreign key is also dropped). For example, if column B is dropped and a referential constraint of FOREIGN KEY (A) exists, the operation is allowed.

Otherwise, the drop of the column is not allowed.

The referential constraint is dropped. (Hence, the index used by the foreign key is also dropped).

Table 22 lists the cascaded effects of altering a column. (Alter of a column in the following chart means altering a data type, precision, scale, length, or nullability characteristic.)

Table 22. Cascaded effects of altering a column
Operation Effect
Alter of a column referenced by a view The alter is allowed.

The views that are dependent on the table will be recreated. The new column attributes will be used when recreating the views.

Alter of a column referenced by a non-view logical file The alter is allowed.

The non-view logical files that are dependent on the table will be recreated. If the logical file shares a format with the file being altered, and that format is not used again in the logical file with another based-on file, the new column attributes will be used when recreating the logical file.

Otherwise, the new column attributes will not be used when recreating the logical file. Instead, the current logical file attributes are used.

Alter of a column referenced in the key of an index. The alter is allowed. (Hence, the index will usually be rebuilt.)
Alter of a column referenced in a unique constraint The alter is allowed. (Hence, the index will usually be rebuilt.)

If the unique constraint is referenced by a referential constraint, the attributes of the foreign keys no longer match the attributes of the unique constraint. The constraint will be placed in a defined and check-pending state.

Alter of a column referenced in a referential constraint The alter is allowed.
  • If the referential constraint is in the defined but check-pending state, the alter is allowed and an attempt is made to put the constraint in the enabled state. (Hence, the index used to satisfy the unique constraint will usually to be rebuilt.)
  • If the referential constraint is in the enabled state, the constraint is placed in the defined and check-pending state.

Examples

Example 1

Assume a new table EQUIPMENT has been created with the following columns:
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. 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. Assume the DEPARTMENT table has a primary key defined as (DEPTNO).

   ALTER TABLE EQUIPMENT
     ADD CONSTRAINT DEPTQUIP
      FOREIGN KEY (EQUIP_OWNER)
      REFERENCES DEPARTMENT
      ON DELETE SET NULL

Example 2

Assume the same table EQUIPMENT exists as in the first example.


Footnotes:

29
In cases where enough storage does not exist to make a complete copy, a special copy that only requires approximately 16-32 megabytes of free storage is performed.

30
Any indexes that need to be rebuilt are rebuilt asynchronously by database server jobs.

31
A column will also be added to a logical file that shares its physical file's format when a column is added to that physical file (unless that format is used again in the logical file with another based-on file).


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]