SlideShare une entreprise Scribd logo
1  sur  2
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à Columnrename

Similaire à Columnrename (20)

Tablerename
TablerenameTablerename
Tablerename
 
Data Definition Language (DDL)
Data Definition Language (DDL) Data Definition Language (DDL)
Data Definition Language (DDL)
 
Oracle
OracleOracle
Oracle
 
Columnrename9i
Columnrename9iColumnrename9i
Columnrename9i
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Les11
Les11Les11
Les11
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersSQL Database Performance Tuning for Developers
SQL Database Performance Tuning for Developers
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Oracle: Commands
Oracle: CommandsOracle: Commands
Oracle: Commands
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
Editdmpfile
EditdmpfileEditdmpfile
Editdmpfile
 
Ijetr012023
Ijetr012023Ijetr012023
Ijetr012023
 
Sql
SqlSql
Sql
 
Columndrop
ColumndropColumndrop
Columndrop
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating views
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 

Plus de oracle documents (20)

Applyinga blockcentricapproachtotuning
Applyinga blockcentricapproachtotuningApplyinga blockcentricapproachtotuning
Applyinga blockcentricapproachtotuning
 
Windowsosauthent
WindowsosauthentWindowsosauthent
Windowsosauthent
 
Whatistnsnames
WhatistnsnamesWhatistnsnames
Whatistnsnames
 
Whatisadatabaselink
WhatisadatabaselinkWhatisadatabaselink
Whatisadatabaselink
 
Varraysandnestedtables
VarraysandnestedtablesVarraysandnestedtables
Varraysandnestedtables
 
Usertracing
UsertracingUsertracing
Usertracing
 
Userpasswrd
UserpasswrdUserpasswrd
Userpasswrd
 
Userlimit
UserlimitUserlimit
Userlimit
 
Undo internalspresentation
Undo internalspresentationUndo internalspresentation
Undo internalspresentation
 
Undo internals paper
Undo internals paperUndo internals paper
Undo internals paper
 
Tablespacelmt
TablespacelmtTablespacelmt
Tablespacelmt
 
Sql scripting sorcerypresentation
Sql scripting sorcerypresentationSql scripting sorcerypresentation
Sql scripting sorcerypresentation
 
Sql scripting sorcerypaper
Sql scripting sorcerypaperSql scripting sorcerypaper
Sql scripting sorcerypaper
 
Sql for dbaspresentation
Sql for dbaspresentationSql for dbaspresentation
Sql for dbaspresentation
 
Sequencereset
SequenceresetSequencereset
Sequencereset
 
Rollbacksizes
RollbacksizesRollbacksizes
Rollbacksizes
 
Rollbackshrinks
RollbackshrinksRollbackshrinks
Rollbackshrinks
 
Rollbacklmt
RollbacklmtRollbacklmt
Rollbacklmt
 
Rollbackblocking
RollbackblockingRollbackblocking
Rollbackblocking
 
Rollback1555s
Rollback1555sRollback1555s
Rollback1555s
 

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