ALTER TABLE statement

Description

Modifies a table definition.

Syntax

ALTER TABLE [ owner.]table-name
{ add-clause | modify-clause | drop-clause | rename-clause }

Parameters

add-clause:

 ADD column-definition [ column-constraint ]... | ADD table-constraint

modify-clause:

 MODIFY column-definition | MODIFY column-name [ IDENTITY | DEFAULT default-value ]  [ NOT ] NULL | MODIFY column-name [ CONSTRAINT constraint-name ] CHECK NULL | MODIFY column-name CHECK ( new-condition )  ALTER column-name column-modification | ALTER CONSTRAINT constraint-name CHECK (new-condition )

drop-clause:

  { DELETE | DROP } column-name | { DELETE | DROP } CHECK | { DELETE | DROP } CONSTRAINT constraint-name | { DELETE | DROP } UNIQUE ( column-name [, ...] ) | { DELETE | DROP } PRIMARY KEY | { DELETE | DROP } FOREIGN KEY role-name

rename-clause:

 RENAME new-table-name | RENAME column-name TO new-column-name | RENAME constraint-name TO new-constraint-name

column-definition:

column-name data-type [ NOT NULL ] [ DEFAULT default-value | IDENTITY ]

column-constraint:

[ CONSTRAINT constraint-name ] { UNIQUE | PRIMARY KEY | REFERENCES table-name [ ( column-name ) ] [ actions ] | CHECK ( condition ) | IQ UNIQUE ( integer ) }

default-value:

special-value | string | global variable | [ - ] number | ( constant-expression ) | built-in-function ( constant-expression ) | AUTOINCREMENT | NULL | TIMESTAMP | LAST USER | USER

special-value:

CURRENT { DATABASE | DATE | REMOTE USER | TIME | TIMESTAMP | USER | PUBLISHER }

table-constraint:

{ UNIQUE ( column-name [, ...] ) | PRIMARY KEY ( column-name [, ...] ) | foreign-key-constraint | CHECK ( condition )}

foreign-key-constraint:

FOREIGN KEY [ role-name ] [ (column-name [, ...] ) ] ... REFERENCES table-name [ (column-name [, ...] ) ] ... [ actions ] [

actions:

[ ON {UPDATE | DELETE} action ]

action:

{ RESTRICT }

Examples

Example 1

ALTER TABLE employee
ADD office CHAR(20)
ALTER TABLE employee
DELETE office
ALTER TABLE customer
ADD sales_contact INTEGER
REFERENCES employee (emp_id)
ALTER TABLE customer
ADD cust_num INTEGER DEFAULT 88

Usage

The ALTER TABLE statement changes table attributes (column definitions and constraints) in a table that was previously created. The syntax allows a list of alter clauses; however, only one table constraint or column constraint can be added, modified, or deleted in each ALTER TABLE statement.

NoteYou cannot alter local temporary tables, but you can alter global temporary tables when they are in use by only one connection.

Sybase IQ enforces REFERENCES and CHECK constraints. Table and/or column check constraints added in an ALTER TABLE statement are not evaluated as part of that alter table operation. For details about CHECK constraints, see CREATE TABLE statement.

If SELECT * is used in a view definition and you alter a table referenced by the SELECT *, then you must run ALTER VIEW <viewname> RECOMPILE to ensure that the view definition is correct and to prevent unexpected results when querying the view.

ADD column-definition [ column-constraint ] Add a new column to the table. The table must be empty to specify NOT NULL. The table might contain data when you add an IDENTITY or DEFAULT AUTOINCREMENT column. If the column has a default IDENTITY value, all rows of the new column are populated with sequential values. You can also add a foreign key constraint as a column constraint for a single column key. The value of the IDENTITY/DEFAULT AUTOINCREMENT column uniquely identifies every row in a table. The IDENTITY/DEFAULT AUTOINCREMENT column stores sequential numbers that are automatically generated during inserts and updates. DEFAULT AUTOINCREMENT columns are also known as IDENTITY columns. When using IDENTITY/DEFAULT AUTOINCREMENT, the column must be one of the integer data types, or an exact numeric type, with scale 0. See CREATE TABLE statement for more about column constraints and IDENTITY/DEFAULT AUTOINCREMENT columns.

NoteYou cannot add foreign key constraints to an unenforced primary key created with Sybase IQ version 12.4.3 or earlier.

ADD table-constraint Add a constraint to the table. You can also add a foreign key constraint as a table constraint for a single-column or multicolumn key. See CREATE TABLE statement for a full explanation of table constraints.

If PRIMARY KEY is specified, the table must not already have a primary key created by the CREATE TABLE statement or another ALTER TABLE statement.

NoteYou cannot MODIFY a table or column constraint. To change a constraint, DELETE the old constraint and ADD the new constraint.

MODIFY column-name [ NOT ] NULL Change the NOT NULL constraint on the column to allow or disallow NULL values in the column.

MODIFY column-name [ DEFAULT default-value | IDENTITY ] The value of the IDENTITY or DEFAULT AUTOINCREMENT column uniquely identifies every row in a table. The IDENTITY/DEFAULT AUTOINCREMENT column stores sequential numbers that are automatically generated during inserts and updates. DEFAULT AUTOINCREMENT columns are also known as IDENTITY columns. When you use IDENTITY/DEFAULT AUTOINCREMENT, the column must be one of the integer data types, or an exact numeric type, with scale 0. See CREATE TABLE statement for a full explanation of column constraints and IDENTITY/DEFAULT AUTOINCREMENT columns.

ALTER TABLE also supports the modification of column default values other than IDENTITY/DEFAULT AUTOINCREMENT. When modifying a column of a table, you can specify a default value for the column using the DEFAULT keyword. If a DEFAULT value is specified for a column, this DEFAULT value is used as the value of the column in any INSERT (or LOAD) statement that does not specify a value for the column.

For detailed information on the use of column DEFAULT values, see “Using column defaults” in Chapter 9, “Ensuring Data Integrity” in the Sybase IQ System Administration Guide.

MODIFY column-name CHECK NULL Delete the check constraint for the column.

MODIFY column-name CHECK (new-condition) Replace the existing CHECK condition for the column with the one specified.

ALTER column-name column-modification Change the definition of a column. The permitted modifications are as follows:

DELETE column-name Delete the column from the table. If the column is contained in any multicolumn index, uniqueness constraint, foreign key, or primary key, then the index, constraint, or key must be deleted before the column can be deleted. This does not delete CHECK constraints that refer to the column. An IDENTITY/DEFAULT AUTOINCREMENT column can only be deleted if IDENTITY_INSERT is turned off and the table is not a local temporary table.

DELETE CHECK Delete all check constraints for the table. This includes both table check constraints and column check constraints.

DELETE UNIQUE (column-name,...) Delete a uniqueness constraint for this table. Any foreign keys referencing this uniqueness constraint (rather than the primary key) are also deleted. Reports an error if there are associated foreign-key constraints. Use ALTER TABLE to delete all foreign keys that reference the primary key before you delete the primary key constraint.

DELETE PRIMARY KEY Delete the primary key constraint for this table. All foreign keys referencing the primary key for this table are also deleted. Reports an error if there are associated foreign key constraints. If the primary key is unenforced, DELETE returns an error if associated unenforced foreign key constraints exist.

DELETE FOREIGN KEY role-name Delete the foreign key constraint for this table with the given role name. Retains the implicitly created nonunique HG index for the foreign key constraint. Users can explicitly remove the HG index with the DROP INDEX statement.

RENAME new-table-name Change the name of the table to the new-table-name. Any applications using the old table name must be modified. Also, any foreign keys that were automatically assigned the same name as the old table name do not change names.

RENAME column-name TO new-column-name Change the name of the column to the new-column-name. Any applications using the old column name must be modified.

RENAME constraint-name TO new-constraint-name Change the name of the constraint to the new-constraint-name. Any applications using the old constraint name must be modified.

ALTER TABLE is prevented whenever the statement affects a table that is currently being used by another connection. ALTER TABLE can be time consuming, and the server does not process requests referencing the same table while the statement is being processed.


Side effects

Standards

Permissions

Must be the owner of the table or have DBA authority or ALTER permission on the table. Requires exclusive access to the table.

See also

CREATE TABLE statement

DROP statement

“IDENTITY_INSERT option”

Chapter 4, “SQL Data Types”