Alters a table definition by: adding a column or table constraint; dropping a table constraint or a column; changing the data-type or the default value for a column.
Adding a Column
When a column is added, the existing table is extended with the addition of a new column, which is placed at the end of the table definition.
For each existing row in the table, the column will be assigned the default value (which will be the column default value if one is defined, the domain default if the column belongs to a domain or otherwise the NULL value).
Note: If the column-definition of the column being added includes the NOT NULL column constraint, then the column must either have a non-NULL default value defined or belong to a domain with a non-NULL default value. Otherwise an attempt would be made to insert the NULL value into a column which cannot accept it.
For information on column-definition, please see CREATE TABLE.
Altering a Column
When a column is altered, it is possible to change the data type of the data in it and to set or drop the column default value.
If a new data type is set for the column, it must be assignment-compatible with the values that already exist in the column.
If a column default value is set for the column, it must be assignment-compatible with the values that already exist in the column.
When the column default value is dropped, the column takes its default value from the domain to which the column belongs (if it uses a domain), otherwise the column default becomes the NULL value.
Dropping a Column
When a column is dropped, it is removed from the table. The keywords CASCADE and RESTRICT specify the action to be taken if other objects (such as views, table constraints, indexes, routines and triggers) exist which reference the column being dropped.
If CASCADE is specified, referencing objects will be dropped as well.
If RESTRICT is specified, an error will be raised if referencing objects exist and neither the column nor the referencing objects will be dropped. If neither keyword is specified, RESTRICT behavior is the default.
Adding a Table Constraint
It is possible to add a new table constraint to the table, which is specified in the same way it would be when a new table is created. If the table constraint is explicitly named, it cannot have the same name as a constraint that already exists on the table, see CREATE TABLE for more details.
If the existing data in a table violates the table constraint being added, the ALTER TABLE statement will fail and the new constraint will not be added to the table.
Dropping Table Constraints
It is also possible to drop an existing table constraint in order to remove the constraint from the table. The keywords CASCADE and RESTRICT specify the action to be taken in the case of a referential constraint being dropped.
If CASCADE is specified when a referential constraint is dropped, any other referential constraints which are referencing the unique key being dropped will also be dropped.
If RESTRICT is specified an error will be raised, and nothing will be dropped, if there are other referential constraints referencing the one to be dropped. If neither keyword is specified, RESTRICT behavior is the default.
column-definition, see CREATE TABLE.
table-constraint-definition, see CREATE TABLE.
default-value, see Default Values.
A table can only be altered by the creator of the schema to which the table belongs.
You must have exclusive access to a table to alter it.
A column cannot be dropped if it is the only column in a table (i.e. a drop column operation may not result in a table with no columns).
The ident performing an ALTER TABLE operation must have USAGE privilege on any domain or sequence involved, EXECUTE privilege on any function involved and REFERENCES privilege on all columns specified in references of a referential constraint.
Change of data type for a column is not allowed if the column participates in any type of table constraint (PRIMARY KEY, UNIQUE, REFERENTIAL) or index (INDEX, UNIQUE INDEX).
Change of data type for a column is not allowed if the column is used by a view, procedure, function or trigger.
If a UNIQUE constraint is added to the table, it must be stored in a databank with the TRANS or LOG option.
If a REFERENTIAL constraint is added to the table, both the referencing table and the referenced table must be stored in a databank with the TRANS or LOG option.
If any record exists for a table it is not allowed to add a column with PRIMARY KEY or UNIQUE constraint.
A column of LARGE OBJECT (i.e. LOB, CLOB, or NCLOB) data type is not allowed in any type of table constraint.
If a trigger exists for a table it is not allowed to add a column of LARGE OBJECT data type.
ExamplesALTER TABLE staff ADD city VARCHAR(50); ALTER TABLE staff ALTER city NCHAR VARYING(50) COLLATE english_1;
See Limits for information on the maximum length of a row in a table.
SQL-99 Core Fully compliant. SQL-99 Features outside core Feature F033, "ALTER TABLE statement: DROP COLUMN clause"Feature F381, "Extended schema manipulation" support for set column default and drop column default and support for add and drop table constraints. Mimer SQL extension Support for changing the data type for a column is a Mimer SQL extension.
Mimer Information Technology AB
Voice: +46 18 780 92 00
Fax: +46 18 780 92 40