Note: Documentation for Pivotal SQLFire 1.1.x is now available at http://docs.gopivotal.com/sqlfire/index.html. Please reference the docs.gopivotal.com site for the latest and most up-to-date documentation on SQLFire. The vFabric SQLFire documentation site will no longer be updated.

ALTER TABLE

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

Note: You can perform table modifications only on non-primary key columns that are not used for table partitioning. See also ALTER TABLE Limitations.

Syntax

ALTER TABLE table-name
{
  ADD COLUMN column-definition |
  ADD table-constraint |
  ALTER [ COLUMN ] column-name SET GENERATED ALWAYS AS IDENTITY |
  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] * ) 
}

Description

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 that 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. Also, 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 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 constraint, unique constraint, or a foreign key constraint drops the physical index that enforces the constraint.

You can 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 the 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.

Example

–- 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 column is not used for table partitioning, and the column has no dependents
ALTER TABLE trade.customers drop column addr;

–- add the column back 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
ALTER TABLE maps ALTER COLUMN map_id SET GENERATED ALWAYS AS IDENTITY;

-- add a gateway sender configuration to a table that was created without any senders
ALTER TABLE maps SET GATEWAYSENDER (uksender);

-- 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:
ALTER TABLE maps SET GATEWAYSENDER (apacsender);

-- remove all gateway sender configurations from "maps:"
ALTER TABLE maps SET GATEWAYSENDER ();

See also ALTER TABLE Limitations.