ALTER TABLE
Description
Alters the schema of an existing table by adding new columns or modifying existing columns of the specified table.
Syntax
ALTER TABLE [ owner_name. ] table_name
{ ADD new_column_definition
| ADD (new_column_definition [, new_column_definition] ...)
| MODIFY (column_name [ new_column_type ]
[ default_value ] [ NULL | NOT NULL ]
| DROP [ COLUMN ] { column_name | ( column_name , ... ) }
| ADD [ CONSTRAINT new_constraint_name ] table_constraint
| RENAME CONSTRAINT constraint_name TO new_constraint_name
| RENAME COLUMN column_name TO new_column_name
| RENAME TO new_table_name
};
new_column_definition ::
new_column_name column_type
[ [ NULL | NOT NULL ]
[ default_value
| IDENTITY [(seed, increment)] ] ]
default_value::
DEFAULT { literal | USER | NULL | UID | SYSDATE | SYSTIME | SYSTIMESTAMP }
table_constraint ::
PRIMARY KEY ( column [, ... ] )
| UNIQUE ( column [, ... ] )
| FOREIGN KEY ( column [, ... ] )
REFERENCES [ owner_name. ] table_name [ ( column [, ... ] ) ]
| CHECK ( search_condition )
[ REORG ]
Notes
However, columns referenced in check constraints, views, procedures, triggers, and UDFs cannot be modified from an ALTER TABLE event and need to be simultaneously addressed by a database administrator.
Examples
To add columns to the tmp table:
ISQL> ALTER TABLE tmp
ADD mname CHAR(8) ;
ISQL> ALTER TABLE tmp
ADD (mname CHAR(8), nname CHAR(8)) ;
To add table-level constraints, consider the table tmp consisting of integer fields, fld, fld1, fld2, and fld3.
ISQL> ALTER TABLE tmp ADD UNIQUE(fld) ;
ISQL> ALTER TABLE tmp ADD primary key(fld1) ;
ISQL> ALTER TABLE tmp ADD constraint fri_cns foreign key(fld2) ;
ISQL> ALTER TABLE tmp ADD constraint chk_cns (fld2 > 10) ;
Authorization
The user executing this statement must have any of the following privileges:
SQL Compliance |
SQL-92, ODBC Core SQL grammar |
Environment |
Embedded SQL, interactive SQL, ODBC applications |
Related Statements |
CREATE TABLE, DROP TABLE |
REORG STORAGE_ATTRIBUTES
In V11.5 and later, the REORG option forces table record and index reorganization. It can change the definitions by applying the storage attributes specified.
ALTER TABLE [tbl] REORG STORAGE_ATTRIBUTES '[attribs]'
The specified storage attributes affect the organization of the table and the use of Hot Alter Table (see below). The following storage attributes are valid (anything else is ignored):
The REORG option is helpful in the rare cases when the index becomes out-of-sync. It is recommended when the Hot Alter Table feature (the default for SQL tables) has been used on a table many times, perform record reorganization to bring all records to the current status.
Hot Alter Table
FairCom's Hot Alter Table feature can dramatically increase efficiency and minimize downtime when rolling out changes to your application data schema. Hot Alter Table allows you to provide "rolling upgrades" to your product deployed in the field. Schema changes no longer cause your customer to go through a lengthy file migration process, which previously could require many hours of downtime. Instead, Hot Alter Table upgrades records upon read or write.
The SQL API supports the Hot Alter Table feature:
create table.... storage_attributes 'nohotalter' to not allow hot alter
create table.... storage_attributes 'hotalter' to allow hot alter
SQL_OPTION HOT_ALTER_TABLE
Calls to Alter Table will automatically always attempt to use Hot Alter Table for tables created with the support turned on.
For example, adding a field to a table does use Hot Alter Table:
ALTER TABLE mytable ADD (newfield VARCHAR(100));
ALTER TABLE [tbl] REORG - Forces table record and index reorganization with no definition changes.