altering the structure of a table.

    Available in

    DSQL, ESQL

    Syntax

    The ALTER TABLE statement changes the structure of an existing table. With one ALTER TABLE statement it is possible to perform multiple operations, adding/dropping columns and constraints and also altering column specifications.

    Multiple operations in an ALTER TABLE statement are separated with commas.

    Some changes in the structure of a table increment the metadata change counter (“version count”) assigned to every table. The number of metadata changes is limited to 255 for each table. Once the counter reaches the 255 limit, you will not be able to make any further changes to the structure of the table without resetting the counter.

    To reset the metadata change counter

    You should back up and restore the database using the gbak utility.

    The ADD Clause

    With the ADD clause you can add a new column or a new table constraint. The syntax for defining the column and the syntax of defining the table constraint correspond with those described for CREATE TABLE statement.

    Effect on Version Count

    • Each time a new column is added, the metadata change counter is increased by one

    • Adding a new table constraint does not increase the metadata change counter

    Points to Be Aware of
    1. Be careful about adding a new column with the NOT NULL constraint set. It may lead to breaking the logical integrity of data, since you will have existing records containing NULL in a non-nullable column. When adding a non-nullable column, it is recommended either to set a default value for it or to update the column in existing rows with a non-null value.

    The DROP Clause

    The DROP *<column name>* clause deletes the specified column from the table. An attempt to drop a column will fail if anything references it. Consider the following items as sources of potential dependencies:

    • column or table constraints

    • indexes

    • stored procedures and triggers

    Effect on Version Count

    • Each time a column is dropped, the table’s metadata change counter is increased by one.

    The DROP CONSTRAINT Clause

    The DROP CONSTRAINT clause deletes the specified column-level or table-level constraint.

    A PRIMARY KEY or UNIQUE key constraint cannot be deleted if it is referenced by a FOREIGN KEY constraint in another table. It will be necessary to drop that FOREIGN KEY constraint before attempting to drop the PRIMARY KEY or UNIQUE key constraint it references.

    Effect on Version Count

    • Deleting a column constraint or a table constraint does not increase the metadata change counter.

    With the ALTER [COLUMN] clause, attributes of existing columns can be modified without the need to drop and re-add the column. Permitted modifications are:

    • change the name (does not affect the metadata change counter)

    • change the data type (increases the metadata change counter by one)

    • change the column position in the column list of the table (does not affect the metadata change counter)

    • delete the default column value (does not affect the metadata change counter)

    • set a default column value or change the existing default (does not affect the metadata change counter)

    • change the type and expression for a computed column (does not affect the metadata change counter)

    Renaming a Column: the TO Keyword

    The TO keyword with a new identifier renames an existing column. The table must not have an existing column that has the same identifier.

    It will not be possible to change the name of a column that is included in any constraint: PRIMARY KEY, UNIQUE key, FOREIGN KEY, column constraint or the CHECK constraint of the table.

    Renaming a column will also be disallowed if the column is used in any trigger, stored procedure or view.

    Changing the Data Type of a Column: the TYPE Keyword

    The keyword TYPE changes the data type of an existing column to another, allowable type. A type change that might result in data loss will be disallowed. As an example, the number of characters in the new type for a CHAR or VARCHAR column cannot be smaller than the existing specification for it.

    If the column was declared as an array, no change to its type or its number of dimensions is permitted.

    The data type of a column that is involved in a foreign key, primary key or unique constraint cannot be changed at all.

    Changing the Position of a Column: the POSITION Keyword

    The POSITION keyword changes the position of an existing column in the notional “left-to-right” layout of the record.

    Numbering of column positions starts at 1.

    • If a position less than 1 is specified, an error message will be returned

    • If a position number is greater than the number of columns in the table, its new position will be adjusted silently to match the number of columns.

    The optional DROP DEFAULT clause deletes the default value for the column if it was put there previously by a CREATE TABLE or ALTER TABLE statement.

    • If the column is based on a domain with a default value, the default value will revert to the domain default

    The optional SET DEFAULT clause sets a default value for the column. If the column already has a default value, it will be replaced with the new one. The default value applied to a column always overrides one inherited from a domain.

    The COMPUTED [BY] or Clauses

    The data type and expression underlying a computed column can be modified using a COMPUTED [BY] or GENERATED ALWAYS AS clause in the ALTER TABLE ALTER [COLUMN] statement. Converting a regular column to a computed one and vice versa are not permitted.

    Attributes that Cannot Be Altered

    The following alterations are not supported:

    • Enabling or disabling the NOT NULL constraint on a column

    • Changing the default collation for a character type column

    Only the table owner and have the authority to use ALTER TABLE.

    Examples Using ALTER TABLE

    1. Adding the CAPITAL column to the COUNTRY table.

      1. ALTER TABLE COUNTRY
      2. ADD CAPITAL VARCHAR(25);
    2. Adding the CAPITAL column with the UNIQUE constraint and deleting the CURRENCY column.

    3. Adding the CHK_SALARY check constraint and a foreign key to the JOB table.

      1. ALTER TABLE JOB
      2. ADD CONSTRAINT CHK_SALARY CHECK (MIN_SALARY < MAX_SALARY),
      3. ADD FOREIGN KEY (JOB_COUNTRY) REFERENCES COUNTRY (COUNTRY);
    4. Setting default value for the MODEL field, changing the type of the ITEMID column and renaming the MODELNAME column.

    5. Changing the computed columns NEW_SALARY and SALARY_CHANGE.

      1. ALTER TABLE SALARY_HISTORY
      2. ALTER NEW_SALARY GENERATED ALWAYS AS
      3. (OLD_SALARY + OLD_SALARY * PERCENT_CHANGE / 100),
      4. (OLD_SALARY * PERCENT_CHANGE / 100);

    See also

    , DROP TABLE,