Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Oracle Help: Changing Column Names
In article <5cam8bINNkh7_at_retriever.cs.umbc.edu>, jmessi1_at_cs.umbc.edu says...
> I'm currently looking for a method to change column
>names in a table without effecting the data already in the
>table. I'm currently using Oracle version 7 and I'm stumped.
>
> I've tried using Alter Table, but altering a
>column name does not appear to be an option (changing
>datatypes, constraints, etc. yes, column names no).
Oracle doesn't support Alter Table to change column names; Rdb does, and there may be a few others.
The most straightforward approach to this is:
**Warning: back up your table first. If you are just doing one table, I'd be inclined to export it somewhere.
CREATE TABLE NEW_TABLE
(NEW_COL_A, NEWCOL_B, NEWCOL_C)
AS
SELECT COL_A, COL_B, COL_C
FROM OLD_TABLE;
See the description of "CREATE TABLE AS subquery" on
p. 4-211 (or thereabouts) of the Oracle SQL Language
Reference Manual (at least, that's where I found it
in my copy dated Dec 1992).
Now you have two copies of the table, one with new column names and one with old. You can drop the old table:
DROP TABLE OLD_TABLE; and then transfer the data back into the original table name with the new column names:
CREATE TABLE OLD_TABLE
AS
SELECT * FROM NEW_TABLE;
and finally get rid of the intermediate table:
DROP TABLE NEW_TABLE; The main problems with this approach are:
> I've also tried duplicating the table, copying
>over the data and then droping the original table.
>Unfortunately, I can not drop the original table since
>I have several tables who have foreign keys which originate
>in the table I'm currrently trying to drop.
Yep, that's the catch. Try
DROP TABLE OLD_TABLE CASCADE CONSTRAINTS; Hopefully you have a SQL script to recreate these afterwards. See also the notes in the SQL Lang. Ref. Man. for DROP TABLE about procedures/functions/packages becoming invalid after a DROP TABLE.
-- Jeffery Boes jboes_at_qtm.net DBA/Developer/Web Engineer for hire "Sucking up bandwidth since 1994."Received on Mon Jan 27 1997 - 00:00:00 CST