ALTER TABLE Statement

Modifies a table definition.

Syntax

Syntax 1

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

Syntax 2

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

alter-clause: 
   ADD create-clause
      | ALTER column-name column-alteration
      | ALTER [ CONSTRAINT constraint-name ] CHECK ( condition ) 
      | DROP drop-object 
      | RENAME rename-object
      | move-clause
      | SPLIT PARTITION range-partition-name 
           INTOrange-partition-decl-1range-partition-decl-2 
      | MERGE PARTITION partition-name-1 INTO partition-name-2 
      | UNPARTITION
      | PARTITION BY 
         range-partitioning-scheme 

create-clause:
   column-name column-definition [ column-constraint ]
   | table-constraint
   | [ PARTITION BY ] range-partitioning-scheme

column definition:
    column-name data-typeNOT NULL | NULL  ] 
    [ IN dbspace-name ] 
    [ DEFAULT default-value | IDENTITY ]

column-constraint:
   [ CONSTRAINT constraint-name ] 
   { UNIQUE
     | PRIMARY KEY
     | REFERENCES table-name [ (column-name ) ] [ actions ] 
     | CHECKcondition ) 
     | IQ UNIQUEinteger ) 
   }

table-constraint:
   [ CONSTRAINT constraint-name ] 
    { UNIQUEcolumn-name [ , … ] )
      | PRIMARY KEYcolumn-name [ , … ] ) 
      | foreign-key-constraint 
      | CHECKcondition )
   }

foreign-key-constraint:
    FOREIGN KEYrole-name ] [ ( column-name [ , … ] ) ]
   ... REFERENCES table-name [ ( column-name [ , … ] ) ] 
   ... [ actions ] 

actions:
   [ ON  { UPDATE | DELETE }   { RESTRICT } ]

column-alteration:
   { column-data-type | alterable-column-attribute } [ alterable-column-attribute … ]  
    | ADD [ constraint-name ] CHECK ( condition )  
    | DROP { DEFAULTCHECK | CONSTRAINT constraint-name }

alterable-column-attribute:
   [ NOT ] NULL 
   | DEFAULT default-value  
   | [ CONSTRAINT constraint-name ] CHECK { NULL |( condition ) 
     }

default-value:
   CURRENT { DATABASE |DATE |REMOTE USER |TIME |TIMESTAMP | USER |PUBLISHER )
   | string 
   | global variable 
   | [ - ] number 
   | ( constant-expression ) 
   | built-in-function constant-expression ) 
   | AUTOINCREMENT 
   | NULL 
   | TIMESTAMP 
   | LAST USER 
   | USER

drop-object:
   { column-nameCHECK constraint-nameCONSTRAINT  
   | UNIQUE ( index-columns-list )  
   | PRIMARY KEYFOREIGN KEY fkey-name
   | [  PARTITION  ] range-partition-name 
  }

rename-object:
   new-table-name  
    | column-name TO new-column-name   
    | CONSTRAINT constraint-name TO new-constraint-name  
    | [ PARTITION ] range-partition-name TO new-range-partition-name 

move-clause:
   { ALTER column-name
        MOVE 
        { PARTITION ( range-partition-name TO new-dbspace-name)
           |  TO new-dbspace-name }  
         }  
        | MOVE PARTITION range-partition-name TO new-dbspace-name
        | MOVE TO new-dbspace-name  
        | MOVE METADATA TO new-dbspace-name  
   }

range-partitioning-scheme:
   RANGEpartition-key ) 
    ( range-partition-decl [,range-partition-decl ...] )

partition-key:
   column-name

range-partition-decl:
   range-partition-name VALUES <= ( {constant |  MAX } ) [ IN dbspace-name ]                

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.

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

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

Syntax 2

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
PARTITION BY RANGE 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 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, 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 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
Related concepts
Restrictions
Range Partitions
Hash Partitions
Hash-Range Partitions