SlideShare a Scribd company logo
Renaming a Column                                                              Administration Tips




How do you rename a column in Oracle 7, 8 or 8i?

The short answer is that, in anything earlier than 9i, you can't. A couple of workarounds
spring to mind, however.

You could create a view on the table, with the view definition containing the new column
name. For example, given an EMP table defined thus:

CREATE TABLE EMP(
EMPNO NUMBER,
JOB CHAR(10),
SAL NUMBER);

...we could create a view on it like this:

CREATE VIEW BLAHEMP AS SELECT
EMPNO EMPLOYEE,
JOB JOB_DESC,
SAL SALARY
FROM EMP;


The syntax is, effectively, 'select real_column <space> column_alias...'.

Now we can do a 'select * from blahemp' and get the following result:

  EMPLOYEE                 JOB_DESC              SALARY
----------                 ---------         ----------
     7369                  CLERK                    800
     7499                  SALESMAN                1600
     7521                  SALESMAN                1250
     7566                  MANAGER                 2975
     7654                  SALESMAN                1250
     7698                  MANAGER                 2850
     7782                  MANAGER                 2450

(Notice the new column aliases are displayed, as though they were the real column names).

Of course, creating a new object such as a view means that you'll need to re-jig all your
permission grants, so that Users have access to the new view (and probably lose direct
access to the base table -otherwise what was the point in creating the view in the first
place?!)

The other possible workaround is to use the CTAS command (Create Table ... As Select ...)
to create a completely new table containing the right column names:

Copyright © Howard Rogers 2001               10/17/2001                                 Page 1 of 2
Renaming a Column                                                                  Administration Tips




CREATE TABLE NEWEMP          (EMPLOYEE,   JOB_DESC,SALARY) AS SELECT EMPNO, JOB,SAL FROM EMP;


Assuming you still want the table to be known as "EMP" and not "NEWEMP", you then have
to peform the following actions:

DROP TABLE EMP;
CREATE TABLE EMP AS SELECT         *   FROM NEWEMP;
DROP TABLE NEWEMP;


...but this is an expensive option: it requires two full tablescans (one of the original table
and one of the new version), the new table has no constraints on it, nor any indexes, and
all Users that had rights to EMP have no rights whatsoever to the newly-minted EMP, even
though it's name is the same (because its object number has nevertheless changed) -so you
have to re-create all constraints and indexes, and re-grant all permissions. You can make
the exercise rather less expensive by sticking the "nologging" clause in the CTAS command:

CREATE TABLE NEWEMP NOLOGGING AS SELECT           ...   FROM EMP;


...but that still doesn't get around the business of full tablescans being done all over the
place, and introduces the requirement to perform a new backup of EMP, since "nologging"
renders an object effectively unrecoverable until a new backup has been successfuly taken.

For how you pull the trick off in 9i, see the "How do I rename a column in 9i?" tip.




Copyright © Howard Rogers 2001                   10/17/2001                                 Page 2 of 2

More Related Content

PPT
PPT
SQL WORKSHOP::Lecture 12
PPT
PPTX
Les04 Displaying Data From Multiple Table
PPT
PPTX
Les05 Aggregating Data Using Group Function

Similar to Columnrename (20)

PDF
Tablerename
PDF
Data Definition Language (DDL)
PDF
Oracle
PDF
Columnrename9i
PPT
Oracle tips and tricks
PPT
Creating Views - oracle database
DOC
Sql Queries
PPT
Les12[1]Creating Views
PPT
PPT
Aggregate Functions,Final
PDF
SQL Database Performance Tuning for Developers
PPTX
Creating database using sql commands
PPTX
Oracle: DDL
PPTX
Oracle: Commands
PDF
Editdmpfile
PDF
Ijetr012023
PDF
Columndrop
PPT
e computer notes - Creating views
PPT
Sql dml & tcl 2
Tablerename
Data Definition Language (DDL)
Oracle
Columnrename9i
Oracle tips and tricks
Creating Views - oracle database
Sql Queries
Les12[1]Creating Views
Aggregate Functions,Final
SQL Database Performance Tuning for Developers
Creating database using sql commands
Oracle: DDL
Oracle: Commands
Editdmpfile
Ijetr012023
Columndrop
e computer notes - Creating views
Sql dml & tcl 2
Ad

More from oracle documents (20)

PPT
Applyinga blockcentricapproachtotuning
PDF
Windowsosauthent
PDF
Whatistnsnames
PDF
Whatisadatabaselink
PDF
Varraysandnestedtables
PDF
Usertracing
PDF
Userpasswrd
PDF
Userlimit
PDF
Undo internalspresentation
PDF
Undo internals paper
PDF
Tablespacelmt
PDF
Sql scripting sorcerypresentation
PDF
Sql scripting sorcerypaper
PDF
Sql for dbaspresentation
PDF
Sequencereset
PDF
Rollbacksizes
PDF
Rollbackshrinks
PDF
Rollbacklmt
PDF
Rollbackblocking
PDF
Rollback1555s
Applyinga blockcentricapproachtotuning
Windowsosauthent
Whatistnsnames
Whatisadatabaselink
Varraysandnestedtables
Usertracing
Userpasswrd
Userlimit
Undo internalspresentation
Undo internals paper
Tablespacelmt
Sql scripting sorcerypresentation
Sql scripting sorcerypaper
Sql for dbaspresentation
Sequencereset
Rollbacksizes
Rollbackshrinks
Rollbacklmt
Rollbackblocking
Rollback1555s
Ad

Columnrename

  • 1. Renaming a Column Administration Tips How do you rename a column in Oracle 7, 8 or 8i? The short answer is that, in anything earlier than 9i, you can't. A couple of workarounds spring to mind, however. You could create a view on the table, with the view definition containing the new column name. For example, given an EMP table defined thus: CREATE TABLE EMP( EMPNO NUMBER, JOB CHAR(10), SAL NUMBER); ...we could create a view on it like this: CREATE VIEW BLAHEMP AS SELECT EMPNO EMPLOYEE, JOB JOB_DESC, SAL SALARY FROM EMP; The syntax is, effectively, 'select real_column <space> column_alias...'. Now we can do a 'select * from blahemp' and get the following result: EMPLOYEE JOB_DESC SALARY ---------- --------- ---------- 7369 CLERK 800 7499 SALESMAN 1600 7521 SALESMAN 1250 7566 MANAGER 2975 7654 SALESMAN 1250 7698 MANAGER 2850 7782 MANAGER 2450 (Notice the new column aliases are displayed, as though they were the real column names). Of course, creating a new object such as a view means that you'll need to re-jig all your permission grants, so that Users have access to the new view (and probably lose direct access to the base table -otherwise what was the point in creating the view in the first place?!) The other possible workaround is to use the CTAS command (Create Table ... As Select ...) to create a completely new table containing the right column names: Copyright © Howard Rogers 2001 10/17/2001 Page 1 of 2
  • 2. Renaming a Column Administration Tips CREATE TABLE NEWEMP (EMPLOYEE, JOB_DESC,SALARY) AS SELECT EMPNO, JOB,SAL FROM EMP; Assuming you still want the table to be known as "EMP" and not "NEWEMP", you then have to peform the following actions: DROP TABLE EMP; CREATE TABLE EMP AS SELECT * FROM NEWEMP; DROP TABLE NEWEMP; ...but this is an expensive option: it requires two full tablescans (one of the original table and one of the new version), the new table has no constraints on it, nor any indexes, and all Users that had rights to EMP have no rights whatsoever to the newly-minted EMP, even though it's name is the same (because its object number has nevertheless changed) -so you have to re-create all constraints and indexes, and re-grant all permissions. You can make the exercise rather less expensive by sticking the "nologging" clause in the CTAS command: CREATE TABLE NEWEMP NOLOGGING AS SELECT ... FROM EMP; ...but that still doesn't get around the business of full tablescans being done all over the place, and introduces the requirement to perform a new backup of EMP, since "nologging" renders an object effectively unrecoverable until a new backup has been successfuly taken. For how you pull the trick off in 9i, see the "How do I rename a column in 9i?" tip. Copyright © Howard Rogers 2001 10/17/2001 Page 2 of 2