Solved

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

Posted on 2012-04-13
8
639 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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. …
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

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