Solved

How do I alter a Table definition but preserve data on a live ORACLE DB?

Posted on 2012-04-13
8
638 Views
Last Modified: 2012-04-24
Hi:

I'm a relative newbie to the wonderful world of ORACLE.

I've got a table definition:
=====================
TABLE "FOO"
  (
    "USER_ID"          NUMBER NOT NULL ENABLE,
    "UPDATE_DATETIME" TIMESTAMP (6),
    "SOURCE"  VARCHAR2(200 BYTE) NOT NULL ENABLE,
    "DETAILS" VARCHAR2(1000 BYTE) NOT NULL ENABLE
  )

I want to change it to the following:
============================
TABLE "FOO"
  (
    "USER_ID"          NUMBER NOT NULL ENABLE,
    "UPDATE_DATETIME" TIMESTAMP (6),
    "SOURCE"  VARCHAR2(500 BYTE) NOT NULL ENABLE,
    "DETAILS" VARCHAR2(4000 BYTE) NOT NULL ENABLE
  )

The table is in an ORACLE DB with live customer data. So, I need to alter the table defiinition and preserve the existing data. The most obvious way I can think of is to create a copy of the existing table (definition & data); truncate the existing table, alter the table  and copy the data back.

Is there a more elegante/ORACLE standard practices way of doing this?

Thanks in advance for your help,

JohnB
0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37843009
Increasing a varchar2 size can be done live:

alter table foo modify details varchar2(4000);
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37843023
I shoudl also add that if your database is running a multi-byte character set you might not be able to use "4000 BYTE".  "2000 BYTE" might be the max.
0
 
LVL 1

Author Comment

by:jxbma
ID: 37843094
My mistake in the original post:

The original table definition is:
========================
TABLE "FOO"
  (
    "USER_ID"          NUMBER NOT NULL ENABLE,
    "UPDATE_DATETIME" TIMESTAMP (6),
    "SOURCE"  VARCHAR2(200 BYTE) NOT NULL ENABLE,
    "DETAILS" NVARCHAR2(1000 BYTE) NOT NULL ENABLE
  )

I want to change it to the following:
============================
TABLE "FOO"
  (
    "USER_ID"          NUMBER NOT NULL ENABLE,
    "UPDATE_DATETIME" TIMESTAMP (6),
    "SOURCE"  VARCHAR2(500 BYTE) NOT NULL ENABLE,
    "DETAILS" VARCHAR2(4000 BYTE) NOT NULL ENABLE
  )

Thanks,
JohnB
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 37843106
Again, increasing column widths can be done live without data loss.

Just try it on a test database:

create table tab1(col1 varcahr2(1));
insert into tab1 values('a');
commit;
alter table tab1 modify col1 varchar2(4000);
select * from tab1;
0
 
LVL 32

Expert Comment

by:awking00
ID: 37844251
I think slightwv may have missed the source modification. Just to add that and enclose in parentheses -
alter table foo modify(source varchar2(500), details varchar2(4000));
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37844261
>>think slightwv may have missed the source modification

Didn't miss it.  Just figured it was self-explainitory since both columns are varchar2, there is no problem extending both.
0
 
LVL 32

Expert Comment

by:awking00
ID: 37844575
>>Just figured it was self-explainitory since both columns are varchar2<<
That's what I figured and it should be self_explanatory. It's just that not knowing their experience level, unfortunately some askers are looking for an exact solution to their questions and not necessarily concerned with how (and I'm not saying that's the case here, jxbma). Please, no points.
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 37887729
Thanks for the quick response.

JohnB
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question