Oracle 8i | Oracle 9i | Oracle 10g | Oracle 11g | Oracle 12c | Miscellaneous | PL/SQL | SQL | Oracle RAC | Oracle Apps | Linux

Renaming Columns And Constraints

In addition to renaming tables and indexes Oracle9i Release 2 allows the renaming of columns and constraints on tables. In this example once the the TEST1 table is created it is renamed along with it's columns, primary key constraint and the index that supports the primary key.

SQL> CREATE TABLE test1 (
  2    col1  NUMBER(10) NOT NULL, 
  3    col2  VARCHAR2(50) NOT NULL);

Table created.

SQL> ALTER TABLE test1 ADD (
  2    CONSTRAINT test1_pk PRIMARY KEY (col1));

Table altered.

SQL> DESC test1
 Name                 Null?    Type
 -------------------- -------- --------------------
 COL1                 NOT NULL NUMBER(10)
 COL2                 NOT NULL VARCHAR2(50)

SQL> SELECT constraint_name
  2  FROM   user_constraints
  3  WHERE  table_name      = 'TEST1'
  4  AND    constraint_type = 'P';

CONSTRAINT_NAME
------------------------------
TEST1_PK

1 row selected.

SQL> SELECT index_name, column_name
  2  FROM   user_ind_columns
  3  WHERE  table_name = 'TEST1';

INDEX_NAME            COLUMN_NAME
--------------------  --------------------
TEST1_PK              COL1

1 row selected.

SQL> -- Rename the table, columns, primary key
SQL> -- and supporting index.
SQL> ALTER TABLE test1 RENAME TO test;

Table altered.

SQL> ALTER TABLE test RENAME COLUMN col1 TO id;

Table altered.

SQL> ALTER TABLE test RENAME COLUMN col2 TO description;

Table altered.

SQL> ALTER TABLE test RENAME CONSTRAINT test1_pk TO test_pk;

Table altered.

SQL> ALTER INDEX test1_pk RENAME TO test_pk;

Index altered.

SQL> DESC test
 Name                 Null?    Type
 -------------------- -------- --------------------
 ID                   NOT NULL NUMBER(10)
 DESCRIPTION          NOT NULL VARCHAR2(50)

SQL> SELECT constraint_name
  2  FROM   user_constraints
  3  WHERE  table_name      = 'TEST'
  4  AND    constraint_type = 'P';

CONSTRAINT_NAME
--------------------
TEST_PK

1 row selected.

SQL> SELECT index_name, column_name
  2  FROM   user_ind_columns
  3  WHERE  table_name = 'TEST';

INDEX_NAME            COLUMN_NAME
--------------------  --------------------
TEST_PK               ID

1 row selected.

Hope this helps. Regards Tim...

Back to the Top.