New parameters were added to the ALTER TABLE statement to let you change attributes and drop columns.
For more information about the full ALTER TABLE statement, see the SQL Reference Guide.
The following diagram shows the enhanced ALTER TABLE syntax:
►►─ ALTER TABLE ─┬─────────────────┬─ table-identifier ───────────────►
└─ schema-name ─.─┘
►────┬─ . . . ──────────────────────────────────────────────┬───────►◄
├─ ADD ───┬──────────┬─ column-definition ─────────────┤
│ └─ COLUMN ─┘ │
│ ┌──────── , ──────────┐ │
├─ ADD ───┬──────────┬─ (─▼─ column-definition ┴─)─────┤
│ └─ COLUMN ─┘ │
├─ ALTER ─┬──────────┬─ column-alteration ─────────────┤
│ └─ COLUMN ─┘ │
├─ DROP ──┬──────────┬─ column-name ─┬───────────┬─────┤
│ └─ COLUMN ─┘ └─ CASCADE ─┘ │
└─ RENAME ┬──────────┬ column-name TO new-column-name ─┘
└─ COLUMN ─┘
Expansion of column-alteration
►►────column-name ───┬─ SET ─┬─ DATA TYPE ─ data-type ────┬───┬──►◄
│ ├─┬─ ALLOW ─┬── NULL ────────┤ │
│ │ └─ NOT ───┘ │ │
│ └─ WITH DEFAULT ─────────────┘ │
└─ DROP DEFAULT ─────────────────────────┘
This section describes the parameters that support the ALTER TABLE statement:
Specifies the changes to be made to the attributes of a column.
Expanded syntax for column-alteration is shown after the ALTER TABLE syntax. Descriptions of column-alteration parameters follow the description of ALTER TABLE parameters.
Identifies the column to be removed from the table. Column-name must be the name of a column in the table.
Note: You cannot drop columns that are part of a CALC key of a populated table or that are named in a check constraint.
Drops the following entities:
If CASCADE is not specified, the column must not participate in a referential constraint or index, or be named in a view.
Identifies the column name to be changed. Column-name must be the name of a column in the table.
Note: You cannot rename a column if the column is named in a check constraint or in a view.
Specifies the new name for the identified column.
Limits: new-column-name must be a 1-32 character value that follows the conventions for SQL identifiers.
Note: It must be distinct from the name of any existing column in the table.
Identifies the column whose attributes are to be changed. Column-name must be the name of a column in the table.
Defines the new data type for the named column. The specified data type must be compatible for assignment with the column’s existing data type.
Note: For expanded data-type syntax, see the section “Expansion of Data-type” in the SQL Reference Guide.
You cannot change the data type of a column that is part of a CALC key of a populated table or that is a referenced or foreign key column in a constraint.
Indicates that the column can contain null values.
You cannot change the null attribute of a column that is part of a CALC key of a populated table or a referenced key.
Indicates that the column cannot contain null values.
You cannot change the null attribute of a column that is part of a CALC key of a populated table or a referenced key.
Sets the column’s value to a default if no value for the column is specified when a row is inserted.
Does not set the column’s value to a default when a row is inserted.
The following parameter describes the Default Index enhancement to the ALTER TABLE statement
Creates a default index for the named table.
The table must not already have a default index associated with it.
The following considerations apply when using the Alter and Drop Column parameters to the ALTER TABLE statement:
Allowing a column to have a default value affects only the table’s definition; existing table rows are not affected.
If the table is populated and the column does not allow null values, every existing row must contain a value in the changed column. To ensure this, each row is accessed and updated if it does not contain a value for the column.
A column that is named in a check constraint or a view cannot be renamed.
The definition of all referential constraints, sort keys, CALC keys and indexes in which the column participates are updated to show the new column name.
Every row in the table is updated to remove the column value.
If a column is named in a check constraint or is part of the CALC key of a populated table, you cannot drop the column.
If you do not specify CASCADE, the column must not be one of the following types of columns:
If you specify CASCADE, how the column is used determines what other items are dropped:
The following situations apply when you change a column's null attribute:
The following situations apply when you change a column's data type:
The following examples illustrate the Alter and Drop Column Support enhancement to the ALTER TABLE statement parameters:
alter table demo.empl
add column phone character(10);
alter table demo.empl
alter column city
set data type varchar(20);
alter table demo.empl
drop column status cascade;
alter table demo.empl
rename column proj_id to project_id;
|
Copyright © 2010 CA.
All rights reserved.
|
|