Oracle7 Server SQL Reference

Contents Index Home Previous Next

ALTER TABLE

Purpose

To alter the definition of a table in one of the following ways:

Prerequisites

The table must be in your own schema or you must have ALTER privilege on the table or you must have ALTER ANY TABLE system privilege.

If you are using Trusted Oracle7 in DBMS MAC mode, your DBMS label must match the table's creation label or you must satisfy one of the following criteria:

Syntax

Keywords and Parameters

schema

is the schema containing the table. If you omit schema, Oracle7 assumes the table is in your own schema.

table

is the name of the table to be altered.

ADD

adds a column or integrity constraint.

MODIFY

modifies the definition of an existing column. If you omit any of the optional parts of the column definition (datatype, default value, or column constraint), these parts remain unchanged.

column

is the name of the column to be added or modified.

datatype

specifies a datatype for a new column or a new datatype for an existing column.

You can only omit the datatype if the statement also designates the column as part of the foreign key of a referential integrity constraint. Oracle7 automatically assigns the column the same datatype as the corresponding column of the referenced key of the referential integrity constraint.

DEFAULT

specifies a default value for a new column or a new default for an existing column. Oracle7 assigns this value to the column if a subsequent INSERT statement omits a value for the column. The datatype of the default value must match the datatype specified for the column. The column must also be long enough to hold the default value. A DEFAULT expression cannot contain references to other columns, the pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM, or date constants that are not fully specified.

column_constraint

adds or removes a NOT NULL constraint to or from and existing column. See the syntax of column_constraint [*].

table_constraint

adds an integrity constraint to the table. See the syntax of table_constraint [*].

PCTFREE PCTUSED INITRANS MAXTRANS

changes the value of specified parameters for the table. See the PCTFREE, PCTUSED, INITRANS, and MAXTRANS parameters of the CREATE TABLE command [*].

STORAGE

changes the storage characteristics of the table. See the STORAGE clause beginning [*].

DROP

drops an integrity constraint. See the DROP clause [*].

ALLOCATE EXTENT

explicitly allocates a new extent for the table.

SIZE specifies the size of the extent in bytes. You can use K or M to specify the extent size in kilobytes or megabytes. If you omit this parameter, Oracle7 determines the size based on the values of the table's STORAGE parameters.

DATAFILE specifies one of the data files in the table's tablespace to contain the new extent. If you omit this parameter, Oracle7 chooses the data file.

INSTANCE makes the new extent available to the freelist group associated with the specified instance. If the instance number exceeds the maximum number of freelist groups, the former is divided by the latter, and the remainder is used to identify the freelist group to be used. An instance is identified by the value of its initialization parameter INSTANCE_NUMBER. If you omit this parameter, the space is allocated to the table, but is not drawn from any particular freelist group. Rather the master freelist is used, and space is allocated as needed. For more information, see Oracle7 Server Concepts. Only use this parameter if you are using Oracle7 with the Parallel Server option in parallel mode.

Explicitly allocating an extent with this clause does affect the size for the next extent to be allocated as specified by the NEXT and PCTINCREASE storage parameters.

DEALLOCATE UNUSED

explicitly deallocate unused space at the end of the table and make the freed space available for other segments. You can free only unused space above the high-water mark. If KEEP is omitted, all unused space is freed. For more information, see the deallocate_clause.

KEEP specifies the number of bytes above the high-water mark that the table will have after deallocation. If the number of remaining extents are less than MINEXTENTS, then MINEXTENTS is set to the current number of extents. If the initial extent becomes smaller than INITIAL, then INITIAL is set to the value of the current initial extent.

ENABLE enable_clause

enables a single integrity constraint or all triggers associated with the table. See the ENABLE clause [*].

ENABLE TABLE LOCK

enables DML and DDL locks on a table in a parallel server environment. For more information, see Oracle7 Parallel Server Concepts & Administration.

DISABLE disable_clause

disables a single integrity constraint or all triggers associated with the table. See the DISABLE clause [*].

Integrity constraints specified in DISABLE clauses must be defined in the ALTER TABLE statement or in a previously issued statement. You can also enable and disable integrity constraints with the ENABLE and DISABLE keywords of the CONSTRAINT clause. If you define an integrity constraint but do not explicitly enable or disable it, Oracle7 enables it by default.

DISABLE TABLE LOCK

disables DML and DDL locks on a table to improve performance in a parallel server environment. For more information, see Oracle7 Parallel Server Concepts & Administration.

PARALLEL

specifies the degree of parallelism for the table. See the parallel_clause [*].

CACHE

Specifies that the blocks retrieved for this table are placed at the most recently used end of the LRU list in the buffer cache when a full table scan is performed. This option is useful for small lookup tables.

NOCACHE

Specifies that the blocks retrieved for this table are placed at the least recently used end of the LRU list in the buffer cache when a full table scan is performed. This is the default behavior.

Adding Columns

If you use the ADD clause to add a new column to the table, then the initial value of each row for the new column is null. You can add a column with a NOT NULL constraint only to a table that contains no rows.

If you create a view with a query that uses the asterisk (*) in the select list to select all columns from the base table and you subsequently add columns to the base table, Oracle7 will not automatically add the new column to the view. To add the new column to the view, you can re-create the view using the CREATE VIEW command with the OR REPLACE option.

Operations performed by the ALTER TABLE command can cause Oracle7 to invalidate procedures and stored functions that access the table. For information on how and when Oracle7 invalidates such objects, see the "Dependencies Among Schema Objects" chapter of Oracle7 Server Concepts.

Modifying Column Definitions

You can use the MODIFY clause to change any of the following parts of a column definition:

The MODIFY clause need only specify the column name and the modified part of the definition, rather than the entire column definition.

Datatypes and Sizes

You can change a CHAR column to VARCHAR2 (or VARCHAR) and a VARCHAR2 (or VARCHAR) to CHAR only if the column contains nulls in all rows or if you do not attempt to change the column size. You can change any column's datatype or decrease any column's size if all rows for the column contain nulls. However, you can always increase the size of a character or raw column or the precision of a numeric column.

Default Values

A change to a column's default value only affects rows subsequently inserted into the table. Such a change does not change default values previously inserted.

Integrity Constraints

The only type of integrity constraint that you can add to an existing column using the MODIFY clause with the column constraint syntax is a NOT NULL constraint. However, you can define other types of integrity constraints (UNIQUE, PRIMARY KEY, referential integrity, and CHECK constraints) on existing columns using the ADD clause and the table constraint syntax.

You can define a NOT NULL constraint on an existing column only if the column contains no nulls.

Example I

The following statement adds a column named THRIFTPLAN of datatype NUMBER with a maximum of seven digits and two decimal places and a column named LOANCODE of datatype CHAR with a size of one and a NOT NULL integrity constraint:

ALTER TABLE emp 
	ADD (thriftplan NUMBER(7,2),
 		 loancode CHAR(1) NOT NULL) 

Example II

The following statement increases the size of the THRIFTPLAN column to nine digits:

ALTER TABLE emp
 	MODIFY (thriftplan NUMBER(9,2)) 

Because the MODIFY clause contains only one column definition, the parentheses around the definition are optional.

Example III

The following statement changes the values of the PCTFREE and PCTUSED parameters for the EMP table to 30 and 60, respectively:

ALTER TABLE emp 
	PCTFREE 30
 	PCTUSED 60 

Example IV

The following statement allocates an extent of 5 kilobytes for the EMP table and makes it available to instance 4:

ALTER TABLE emp
	ALLOCATE EXTENT (SIZE 5K INSTANCE 4) 

Because this command omits the DATAFILE parameter, Oracle7 allocates the extent in one of the data files belonging to the tablespace containing the table.

Example V

This example modifies the BAL column of the ACCOUNTS table so that it has a default value of 0:

ALTER TABLE accounts
 	MODIFY (bal  DEFAULT 0) 

If you subsequently add a new row to the ACCOUNTS table and do not specify a value for the BAL column, the value of the BAL column is automatically 0:

INSERT INTO accounts(accno, accname)
 	VALUES (accseq.nextval, 'LEWIS') 
 SELECT * 
	FROM accounts
	WHERE accname = 'LEWIS' 
  ACCNO ACCNAME BAL 
------ ------- ---
815234 LEWIS     0 

Other Examples

For examples of defining integrity constraints with the ALTER TABLE command, see the CONSTRAINT clause beginning [*].

For examples of enabling, disabling, and dropping integrity constraints and triggers with the ALTER TABLE command, see the ENABLE clause [*], the DISABLE clause [*], and DROP clause [*].

For examples of changing the value of a table's storage parameters, see the STORAGE clause [*].

Related Topics

CREATE TABLE command [*] CONSTRAINT clause [*] DISABLE clause [*] DROP clause [*] ENABLE clause [*] STORAGE clause [*]


Contents Index Home Previous Next