ALTER TABLE
Go Up to Statement and Function Reference (Language Reference Guide)
Changes a table by adding, dropping, or modifying columns or integrity constraints. Available in gpre
, DSQL, and isql
.
- Important: To alter a global temporary table see: "Altering a global temporary table" in the Data Definition Guide.
ALTER TABLE table operation [, operation …];
operation = ADD col_def | ADD tconstraint | ALTER [COLUMN] column_name alt_col_clause | DROP col | DROP CONSTRAINT constraint alt_col_clause = TO new_col_name | TYPE new_col_datatype | POSITION new_col_position col_def = col {datatype | COMPUTED [BY] (expr) | domain} [DEFAULT {literal | NULL | USER}] [NOT NULL] [col_constraint] [COLLATE collation] datatype = {SMALLINT | INTEGER | FLOAT | DOUBLE PRECISION}[array_dim] | (DATE | TIME | TIMESTAMP} [array_dim] | {DECIMAL | NUMERIC} [(precision [, scale])] [array_dim] | {CHAR | CHARACTER | CHARACTER VARYING | VARCHAR} [(int)] [array_dim] [CHARACTER SET charname] | {NCHAR | NATIONAL CHARACTER | NATIONAL CHAR} [VARYING] [(int)] [array_dim] | BLOB [SUB_TYPE {int | subtype_name}] [SEGMENT SIZE int] [CHARACTER SET charname] | BLOB [(seglen [, subtype])]array_dim = [[x:]y [, [x:]y …]] | BOOLEAN expr = A valid SQL expression that results in a single value. col_constraint = [CONSTRAINT constraint] { UNIQUE | PRIMARY KEY | REFERENCES other_table [(other_col [, other_col …])] [ON DELETE {RESTRICT|NO ACTION|CASCADE|SET DEFAULT|SET NULL}] [ON UPDATE {RESTRICT|NO ACTION|CASCADE|SET DEFAULT|SET NULL}] | CHECK (search_condition)} tconstraint = [CONSTRAINT constraint] {{PRIMARY KEY | UNIQUE} (col [, col …]) | FOREIGN KEY (col [, col …]) REFERENCES other_table [(other_col [, other_col …])] [ON DELETE {RESTRICT|NO ACTION|CASCADE|SET DEFAULT|SET NULL}] [ON UPDATE {RESTRICT|NO ACTION|CASCADE|SET DEFAULT|SET NULL}] | CHECK (search_condition)} search_condition = val operator {val | (select_one)} | val [NOT] BETWEEN val AND val | val [NOT] LIKE val [ESCAPE val] | val [NOT] IN (val [, val …] | select_list) | val IS [NOT] NULL | val {>= | <=} | val [NOT] {= | < | >} | {ALL | SOME | ANY} (select_list) | EXISTS (select_expr) | SINGULAR (select_expr) | val [NOT] CONTAINING val | val [NOT] STARTING [WITH] val | (search_condition) | NOT search_condition | search_condition OR search_condition | search_condition AND search_condition val = { col [array_dim] | :variable | constant | expr | function | udf ([val [, val …]]) | NULL | USER | RDB$DB_KEY | ? } [COLLATE collation] constant = num | 'string' | charsetname 'string' function = COUNT (* | [ALL] val | DISTINCT val) | SUM ([ALL] val | DISTINCT val) | AVG ([ALL] val | DISTINCT val) | MAX ([ALL] val | DISTINCT val) | MIN ([ALL] val | DISTINCT val) | CAST (val AS datatype) | UPPER (val) | GEN_ID (generator, val) operator = {= | < | > | <= | >= | !< | !> | <> | !=} select_one = SELECT on a single column; returns exactly one value. select_list = SELECT on a single column; returns zero or more values. select_expr = SELECT on a list of values; returns zero or more values.
- Important: In SQL statements passed to DSQL, omit the terminating semicolon. In embedded applications written in C and C++, and in
isql
, the semicolon is a terminating symbol for the statement, so it must be included.
Notes on ALTER TABLE
syntax:
- The column constraints for referential integrity were new in InterBase 5. See <constraint_def> in Table 1.5 and the Description for ALTER TABLE.
- You cannot specify a
COLLATE
clause for Blob columns. - When declaring arrays, you must include the outermost brackets, shown below in bold. For example, the following statement creates a 5 by 5 two-dimensional array of strings, each of which is 6 characters long:
my_array = varchar(6)[5,5]
- Use the colon (:) to specify an array with a starting point other than 1. The following example creates an array of integers that begins at 20 and ends at 30:
my_array = integer[20:30]
- For the full syntax of search_condition, see CREATE TABLE.
Argument | Description |
---|---|
<table> |
Name of an existing table to modify. |
<operation> |
Action to perform on the table. Valid options are:
|
<col_def> |
Description of a new column to add.
|
<col> |
Name of the column to add or drop; column name must be unique within the table. |
<datatype> |
Data type of the column; see Data Types (Language Reference Guide). |
|
Modifies column names, data types, and positions. Can also be used with ENCRYPT and DECRYPT options to encrypt and decrypt a column. For more information about encrypting databases and columns, see “Encrypting Your Data” in the Data Definition Guide. |
|
Specifies that the value of the column’s data is calculated from expr at runtime and is therefore not allocated storage space in the database.
|
<domain> |
Name of an existing domain. |
|
Specifies a default value for column data; this value is entered when no other entry is made; possible values are:
Defaults set at column level overrides defaults set at domain level. |
|
Name of a column or table constraint; the constraint name must be unique within the table. |
<constraint_def> |
Specifies the kind of column constraint; valid options are |
|
An attempt to enter a new value in the column fails if the value does not meet the <search_condition>. |
|
Specifies that the column values are derived from column values in another table; if you do not specify column names, InterBase looks for a column with the same name as the referencing column in the referenced table. |
|
Used with
|
|
Specifies that a column cannot contain a
|
|
Drops the specified table constraint. |
<table_constraint> |
Description of the new table constraint; constraints can be |
|
Establishes a default sorting behavior for the column; see Character Sets and Collation Orders for more information. |
Description: ALTER TABLE
modifies the structure of an existing table. A single ALTER TABLE
statement can perform multiple adds and drops.
- A table can be altered by its creator, the
SYSDBA
user, and any users with operating system superuser privileges. ALTER TABLE
fails if the new data in a table violates aPRIMARY KEY
orUNIQUE
constraint definition added to the table. Dropping or altering a column fails if any of the following are true:
- The column is part of a
UNIQUE
,PRIMARY
, orFOREIGN KEY
constraint. - The column is used in a
CHECK
constraint. - The column is used in the <value> expression of a computed column.
- The column is referenced by another database object such as a view.
- The column is part of a
- Important: When a column is dropped, all data stored in it is lost.
Constraints:
- Referential integrity constraints include optional
ON UPDATE
andON DELETE
clauses. They define the change to be made to the referencing column when the referenced column is updated or deleted. The values for these cascading referential integrity options are given in Table 1.5, “The ALTER TABLE statement,” on page 20. - To delete a column referenced by a computed column, you must drop the computed column before dropping the referenced column. To drop a column referenced in a
FOREIGN KEY
constraint, you must drop the constraint before dropping the referenced column. To drop aPRIMARY KEY
orUNIQUE
constraint on a column that is referenced byFOREIGN KEY
constraints, drop theFOREIGN KEY
constraint before dropping thePRIMARY KEY
orUNIQUE
key it references. - You can create a
FOREIGN KEY
reference to a table that is owned by someone else only if that owner has explicitly granted you theREFERENCES
privilege on that table usingGRANT
. Any user who updates your foreign key table must haveREFERENCES
orSELECT
privileges on the referenced primary key table. - You can add a check constraint to a column that is based on a domain, but be aware that changes to tables that contain
CHECK
constraints with subqueries may cause constraint violations. - Naming column constraints is optional. If you do not specify a name, InterBase assigns a system-generated name. Assigning a descriptive name can make a constraint easier to find for changing or dropping, and more descriptive when its name appears in a constraint violation error message.
- When creating new columns in tables with data, do not use the UNIQUE constraint. If you use the NOT NULL constraint on a table with data, you should also specify a default value.
Example: The following isql
statement adds a column to a table and drops a column:
ALTER TABLE COUNTRY ADD CAPITAL VARCHAR(25), DROP CURRENCY;
This statement results in the loss of all data in the dropped CURRENCY
column.
The next isql
statement changes the name of the LARGEST_CITY
column to BIGGEST_CITY
:
ALTER TABLE COUNTRY ALTER LARGEST_CITY TO BIGGEST_CITY;