ALTER TABLE Statement

Modifies a table definition.

Syntax

Syntax 1

ALTER TABLEowner.]table-name
|{ ENABLE | DISABLE } RLV STOREalter-clause, ... }

Syntax 2

ALTER TABLE table_name ALTER OWNER TO new_owner 
   [ { PRESERVE | DROP } PERMISSIONS ] 
   [ { PRESERVE | DROP } FOREIGN KEYS ]

Parameters

Examples

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. 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.

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

SAP Sybase IQ enforces REFERENCES and CHECK constraints. Table and/or column check constraints added in an ALTER TABLE statement are evaluated, only if they are defined on one of the new columns added, 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 FOREIGN 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.

  • IQ UNIQUE constraint — Defines the expected cardinality of a column and determines whether the column loads as Flat FP or NBit FP. An IQ UNIQUE(n) value explicitly set to 0 loads the column as Flat FP. Columns without an IQ UNIQUE constraint implicitly load as NBit up to the limits defined by the FP_NBIT_AUTOSIZE_LIMIT, FP_NBIT_LOOKUP_MB, and FP_NBIT_ROLLOVER_MAX_MB options:
    • FP_NBIT_AUTOSIZE_LIMIT limits the number of distinct values that load as NBit

    • FP_NBIT_LOOKUP_MB sets a threshold for the total NBit dictionary size

    • FP_NBIT_ROLLOVER_MAX_MB sets the dictionary size for implicit NBit rollovers from NBit to Flat FP

    • FP_NBIT_ENFORCE_LIMITS enforces NBit dictionary sizing limits. This option is OFF by default

    Using IQ UNIQUE with an n value less than the FP_NBIT_AUTOSIZE_LIMIT is not necessary. Auto-size functionality automatically sizes all low or medium cardinality columns as NBit. Use IQ UNIQUE in cases where you want to load the column as Flat FP or when you want to load a column as NBit when the number of distinct values exceeds the FP_NBIT_AUTOSIZE_LIMIT.
Note:
  • Consider memory usage when specifying high IQ UNIQUE values. If machine resources are limited, avoid loads with FP_NBIT_ENFORCE_LIMITS='OFF' (default).

    Prior to SAP Sybase IQ 16.0, an IQ UNIQUE n value > 16777216 would rollover to Flat FP. In 16.0, larger IQ UNIQUE values are supported for tokenization, but may require significant memory resource requirements depending on cardinality and column width.

  • BIT, BLOB,and CLOB data types do not support NBit dictionary compression. If FP_NBIT_IQ15_COMPATIBILITY=’OFF’, a non-zero IQ UNIQUE column specification in a CREATE TABLE or ALTER TABLE statement that includes these data types returns an error.

{ ENABLE | DISABLE } RLV STORE — Registers this table with the RLV store for real-time in-memory updates. This value overrides the value of the database option BASE_TABLES_IN_RLV. Requires the CREATE TABLE system privilege and CREATE permissions on the RLV store dbspace to set this value to ENABLE.
Note: The { ENABLE | DISABLE } RLV STORE clause is not supported for IQ temporary tables.
ALTER column-name column-alteration — Change the column definition:
  • SET DEFAULT default-value — Change the default value of an existing column in a table. You can also use the MODIFY clause for this task, but ALTER is ISO/ANSI SQL compliant, and MODIFY is not. Modifying a default value does not change any existing values in the table.

  • DROP DEFAULT — Remove the default value of an existing column in a table. You can also use the MODIFY clause for this task, but ALTER is ISO/ANSI SQL compliant, and MODIFY is not. Dropping a default does not change any existing values in the table.

  • ADD — Add a named constraint or a CHECK condition to the column. The new constraint or condition applies only to operations on the table after its definition. The existing values in the table are not validated to confirm that they satisfy the new constraint or condition.

  • CONSTRAINT column-constraint-name — The optional column constraint name lets you modify or drop individual constraints at a later time, rather than having to modify the entire column constraint.

  • [ CONSTRAINT constraint-name ] CHECK ( condition ) — Use this clause to add a CHECK constraint on the column.

  • SET COMPUTE (expression) — Change the expression associated with a computed column. The values in the column are recalculated when the statement is executed, and the statement fails if the new expression is invalid.

  • DROP COMPUTE — Change a column from being a computed column to being a non-computed column. This statement does not change any existing values in the table.

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. 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. See CREATE TABLE Statement for a full explanation of table constraints.

Note: You cannot MODIFY a table or column constraint. To change a constraint, DELETE the old constraint and ADD the new constraint.
DROP drop-object — Drops a table object.
  • DROP column-name — Drop 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.

  • DROP CHECK — Drop all check constraints for the table. This includes both table check constraints and column check constraints.

  • DROP CONSTRAINT constraint-name — Drop the named constraint for the table or specified column.

  • DROP UNIQUE ( column-name, ... ) — Drop the unique constraints on the specified column(s). Any foreign keys referencing the unique 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.

  • DROP PRIMARY KEY — Drop the primary key. 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.

  • DROP FOREIGN KEY role-name — Drop the foreign key constraint for this table with the given role name. Retains the implicitly created non-unique HG index for the foreign key constraint. Users can explicitly remove the HG index with the DROP INDEX statement.

  • DROP [ PARTITION | SUBPARTITION ] — Drop the specified partition. The rows in partition P1 are deleted and the partition definition is dropped. You cannot drop the last partition because dropping the last partition would transform a partitioned table to a non-partitioned table. (To merge a partitioned table, use an UNPARTITION clause instead.) For example:
    	CREATE TABLE foo (c1 INT, c2 INT)
    		PARTITION BY RANGE (c1)
    		(P1 VALUES <= (100) IN dbsp1,
    		 P2 VALUES <= (200) IN dbsp2,
    		 P3 VALUES <= (MAX) IN dbsp3
    		) IN dbsp4);
    	LOAD TABLE ….
    	ALTER TABLE DROP PARTITION P1;
    Use DROP SUBPARTITIONfor tables partitioned by a composite-partitioning-scheme.
RENAME rename-object
  • 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 new-column-name. Any applications using the old column name must be modified.

  • RENAME [ PARTITION | SUBPARTITION ] — Rename an existing partition or sub-partition.

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

MOVE clause — Moves a table object.

A table object can only reside in one dbspace. Any type of ALTER MOVE blocks any modification to the table for the entire duration of the move.
  • MOVE TO — Move all table objects including columns, indexes, unique constraints, primary key, foreign keys, and metadata resided in the same dbspace as the table is mapped to the new dbspace. The ALTER Column MOVE TO clause cannot be requested on a partitioned table.

  • MOVE TABLE METADATA — Move the metadata of the table to a new dbspace. For a partitioned table, MOVE TABLE METADATA also moves metadata that is shared among partitions.

  • MOVE PARTITION — Move the specified partition to the new dbspace.

  • MOVE SUBPARTITION — Move the specified range subpartition of an existing hash-range partitioned table to the new dbspace.

PARTITION BY — Partitions a non-partitioned table. A non-partitioned table can be partitioned, if all existing rows belong to the first partition. You can specify a different dbspace for the first partition than the dbspace of the column or table. But existing rows are not moved. Instead, the proper dbspace for the column/partition is kept in SYS.ISYSIQPARTITIONCOLUMN for existing columns. Only the default or max identity column(s) that are added later for the first partition are stored in the specified dbspace for the first partition.

SAP Sybase IQ supports range, hash, and composite partitioning schemes:
  • PARTITION BY RANGE — Maps data to partitions based on a range of partition keys established for each partition.

  • PARTITION BY HASH — Maps data to partitions based on partition key values processed by a system-defined function. An existing table with rows can only be made hash-range partitioned if all the hash partition keys hash to a single subpartition; in practice, this means only empty tables can always be altered.

  • PARTITION BY composite-partitioning-scheme — Subpartition rows after partitioning by RANGE or HASH. This method provides the benefits of combined partitioning methods.

Note: Range-partitions and composite partitioning schemes, like hash-range partitions, require the separately licensed VLDB Management option.
SUBPARTITION — Subpartition range or hash partitioned tables by range or hash partitioning strategy.  In a create-clause, use SUBPARTITION to subpartition tables that are partitioned by a composite-partitioning-scheme.
  • SUBPARTITION BY RANGE — Adds a new range subpartition to an existing hash-range partitioned table. The new range subpartition will be logically partitioned by hash with the same hash partitioned keys as the existing hash-range partitioned table.

  • DROP SUBPARTITION — Delete rows and drops the sub-partition definition from an existing hash-range partitioned table. The specified range subpartition is dropped

  • MOVE PARTITION — Moves the specified range subpartition of an existing hash-range partitioned table to the new dbspace.

  • MOVE SUBPARTITION — Moves the column of the specified hash-range partition to the specified dbspace.

  • SPLIT PARTITION — Splits the specified range subpartition of a hash-range partitioned table into two partitions. Split the specified partition into two partitions. A partition can be split only if no data must be moved. All existing rows of the partition to be split must remain in a single partition after the split. merges range-subpartition-name-1 into range-subpartition-name-2 of a hash-range partitioned table.

    The boundary value for partition-decl-1 must be less than the boundary value of partition-name and the boundary value for partition-decl-2 must be equal to the boundary value of partition-name. You can specify different names for the two new partitions. The old partition-name can only be used for the second partition, if a new name is not specified.

  • SPLIT SUBPARTITION — Splits the specified range subpartition of a hash-range partitioned table into two partitions.

MERGE PARTITION — Merge partition-name-1 into partition-name-2. Two partitions can be merged if they are adjacent partitions and the data resides on the same dbspace. You can only merge a partition with a lower partition value into the adjacent partition with a higher partition value. Note that the server does not check CREATE permission on the dbspace into which the partition is merged. For an example of how to create adjacent partitions, see CREATE TABLE Statement examples.

RENAME [ PARTITION | SUBPARTITION ] — Rename an existing PARTITION or SUBPARTITION.

UNPARTITION — Remove partitions from a partitioned table. Each column is placed in a single dbspace. Note that the server does not check CREATE permission on the dbspace to which data of all partitions is moved. ALTER TABLE UNPARTITION blocks all database activities.

ALTER OWNER – Change the owner of a table. The ALTER OWNER clause may not be used in conjunction with any other [alter-clause] clauses of the ALTER TABLE statement.

Side effects:
  • Automatic commit. The ALTER and DROP options close all cursors for the current connection. The Interactive SQL data window is also cleared.

  • A checkpoint is carried out at the beginning of the ALTER TABLE operation.

  • Once you alter a column or table, any stored procedures, views or other items that refer to the altered column no longer work.

Standards

  • SQL—Vendor extension to ISO/ANSI SQL grammar.

  • Sybase—Some clauses are supported by Adaptive Server Enterprise.

Permissions

Syntax 1

The system privileges required for syntax 1 varies depending upon the clause used.

Clause Privilege Required
Add Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER privilege on the underlying table
  • You own the underlying table

UNIQUE, PRIMARY KEY, FOREIGN KEY, or IQ UNIQUE column constraint – Requires above along with REFERENCE privilege on the underlying table.

FOREIGN KEY table constraint requires above along with one of:
  • CREATE ANY INDEX system privilege
  • CREATE ANY OBJECT system privilege
  • REFERENCE privilege on the base table
Hash partition, range partition, or hash-range partition requires above along with one of:
  • CREATE ANY OBJECT system privilege
  • CREATE permission on the dbspaces where the partitions are being created
Alter Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER permission on the table
  • You own the table.

To alter a primary key or unique constraint, also requires REFERENCE permission on the table.

Drop Drop a column with no constraints – Requires one of:
  • ALTER ANY OBJECT system privilege
  • ALTER ANY TABLE system privilege
  • ALTER permission on the underlying table
  • You own the underlying table

Drop a column or table with a constraint requires above along with REFERENCE permission if using ALTER permission.

Drop a partition on table owned by self – None required.

Drop a partition on table owned by other users – Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER permission on the table
RENAME Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER permission on the table
  • You own the table
Move Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • MANAGE ANY DBSPACE system privilege
  • ALTER privilege on the underlying table
  • You own the underlying table
Also requires one of the following:
  • CREATE ANY OBJECT system privilege
  • CREATE privilege on the dbspace to which the partition is being moved
Split Partition or Subpartition Partition on table owned by self – None required.
Partition on table owned by other users – Requires one of:
  • SELECT ANY TABLE system privilege
  • SELECT privilege on table
Also requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER privilege on the table
Merge Partition or Subpartition

Unpartition

Table owned by self – None required.
Table owned by other users – Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER privilege on the table
Partition By Hash partition, range partition, or hash-range partition – Requires one of:
  • CREATE ANY OBJECT system privilege
  • CREATE permission on the dbspaces where the partitions are being created
Also requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER permission on the table
  • You own the table
Enable or disable RLV store Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege

Syntax 2

Requires one of:
  • ALTER ANY TABLE system privilege
  • ALTER ANY OBJECT system privilege
  • ALTER privilege on the table
  • You own the table
Related reference
CREATE TABLE Statement