Use the ALTER TABLE statement to add columns and constraints to a table, remove them from a table, and modify other table features such as AsyncEventListener implementations and gateway sender configurations.

You perform table modifications only on non-primary key columns that are not used for table partitioning.

Many table modifications can be performed only before you add data to the table. After you add rows to the table, you cannot use ALTER TABLE clauses even if you first delete the data. The following clauses are exceptions to this rule:
  • You can use the ALTER COLUMN column-name SET GENERATED ALWAYS AS IDENTITY clause after loading data into a table to resume generating GENERATED ALWAYS identity column values.
  • You can ADD or DROP constraints using the CONSTRAINT, UNIQUE, CHECK, or FOREIGN KEY clauses after you have added data to a table.


ALTER TABLE table-name
  ADD COLUMN column-definition |
  ADD table-constraint |
  DROP [ COLUMN ] column-name [ RESTRICT ] |
  DROP { PRIMARY KEY | FOREIGN KEY constraint-name | UNIQUE 
    constraint-name | CHECK constraint-name | CONSTRAINT constraint-name } |
  SET EVICTION MAXSIZE integer-constant |
  SET GATEWAYSENDER ( [sender-name] [, sender-name] * ) |
  SET ASYNCEVENTLISTENER ( [listener-name] [, listener-name] * ) 


See also ALTER TABLE Limitations.

See also Identity Columns.

Adding columns and table-level constraints follows the same syntax as the CREATE TABLE statement.

When you add columns with the ADD COLUMN clause, you can also place a column constraint on the new column, as shown above. However, you can add a column with a NOT NULL constraint to an existing table only if a default value is provided; otherwise an exception is thrown.

When you add constraints or a primary key, the existing table data is verified to satisfy the constraint. If the existing data violates the constraint, the alteration fails and a constraint violation exception is thrown.

You cannot drop a primary key column, or any column that is used for table partitioning.

Dropping a column from the table can throw a constraint violation if related object data has become invalid. This behavior applies to the RESTRICT clause, which is used by default. CASCADE deletes are not supported. The schema objects which can cause a DROP COLUMN RESTRICT to be rejected include views, triggers, primary key constraints, foreign key constraints, unique key constraints, check constraints, and column privileges.

You cannot drop the last remaining column in a table. Currently DROP COLUMN is not allowed if sqlfire.sql-authorization is true. When a column is dropped, it is removed from any indexes that contain it, and the indexes are rebuilt if required. If that column was the only column in the index, then the entire index is dropped.

The DROP CONSTRAINT clause above drops a constraint on an existing table. To drop an unnamed constraint, specify the generated constraint name stored in SYS.SYSCONSTRAINTS as a delimited identifier. Dropping a primary key, unique, or foreign key constraint drops the physical index that enforces the constraint.

The ALTER COLUMN clause is a special form of the ALTER TABLE command that is executed on a table that already contains data. Use the ALTER COLUMN clause after importing existing identity column values (in a non-identity column) to change the column to a GENERATED ALWAYS AS IDENTITY column. (GENERATED ALWAYS identity columns do not allow manual insertion of identity values.) After executing this ALTER TABLE statement, SQLFire automatically generates identity values for new rows, and ensures that new identity values are greater than the last imported value at the time you executed the ALTER TABLE command. See sqlf write-schema-to-db and sqlf write-data-to-db for more information. See also Auto-Generated Columns.

The SET EVICTION MAXSIZE clause enables you to change the eviction LRUMEMSIZE setting for the table. See EVICTION BY Clause.

The SET GATEWAYSENDER and SET ASYNCEVENTLISTNER clauses enable you to change or remove the gateway senders and asynceventlisteners associated with a table. The new list of senders or listeners that you specify in each clause replaces the current configuration. Do not specify a name to remove all gateway sender or AsyncEventListener configurations from a table, as shown in the examples below.


–- create a table with no constraints
CREATE TABLE trade.customers (
    cid int not null,
    cust_name varchar(100),
    addr varchar(100),
    tid int);
-- add a primary key constraint with no data in the table
ALTER TABLE trade.customers add constraint cust_pk primary key (cid);

–- add a new unique key constraint
ALTER TABLE trade.customers add constraint cust_uk unique (tid);

-- add a new foreign key constraint to a child table;
–- each row is checked to make sure it satisfies the new constraint
CREATE TABLE trade.portfolio (
    cid int not null,
    sid int not null,
    qty int not null,
    availQty int not null,
    tid int,
    constraint portf_pk primary key (cid, sid));
ALTER TABLE trade.portfolio add constraint
    cust_fk foreign key (cid) references trade.customers (cid)
      on delete restrict;

–- drop the unique and foreign key constraints added above
ALTER TABLE trade. customers drop constraint cust_uk;
ALTER TABLE trade.portfolio drop constraint cust_fk;

-- drop a non-primary key column if the table has no data and the column has no dependents
ALTER TABLE trade.customers drop column addr;

–- add the column back when table has no data with a default value
ALTER TABLE trade.customers add column addr varchar(100);

-- change a non-identity column with existing identity values to GENERATED ALWAYS AS IDENTITY

-- add a gateway sender configuration to a table that was created without any senders

-- add a second gateway sender configuration to the "maps" table:
ALTER TABLE maps SET GATEWAYSENDER (uksender, apacsender);

-- remove one gateway sender configuration from the "maps" table:

-- remove all gateway sender configurations from "maps:"

See also ALTER TABLE Limitations.