If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 Alter Command on existing table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-04, 10:08
sks sks is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Unhappy DB2 Alter Command on existing table

Please help!

I have a table that has 32Million records and I need to extend the lenght of the Primary Key field to 31 instead of 24.

I am not able to alter table since the it's a Primary Key and there is an index on this field.

Can you please guide me on what might be the best option to change this field without dropping the table?

Thx!

SKS
Reply With Quote
  #2 (permalink)  
Old 02-26-04, 10:23
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Re: DB2 Alter Command on existing table

Quote:
Originally posted by sks
Please help!

I have a table that has 32Million records and I need to extend the lenght of the Primary Key field to 31 instead of 24.

I am not able to alter table since the it's a Primary Key and there is an index on this field.

Can you please guide me on what might be the best option to change this field without dropping the table?

Thx!

SKS
If your original PK column is a varchar then you can simply alter column definition (you may need to drop and re-create PK). If it's a char then you could do it this way:

1) add a new column with the necessary datatype (I assume it's char(31))
2) copy contents of an existing PK column to the new column
3) drop PK constraint on the original PK column
4) created a unique index on the new column
5) add PK constraint on the new column

Don't forget to do a runstats afterwards in either case
Reply With Quote
  #3 (permalink)  
Old 02-26-04, 11:07
sks sks is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Exclamation

n_i

That's what we originally thought of doing, but DB2 DBA from IBM said on Version 7.2.6 DB2 UDB there is no Drop syntax for a column. Is that true? Can you please confirm!
Reply With Quote
  #4 (permalink)  
Old 02-26-04, 12:20
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by sks
n_i

That's what we originally thought of doing, but DB2 DBA from IBM said on Version 7.2.6 DB2 UDB there is no Drop syntax for a column. Is that true? Can you please confirm!
I never said "drop column" - indeed there's no such functionality, which you could confirm by RTFM. Your old column will stay there to haunt you forever :-)

However, you can drop a PK constraint and then create it based on the new column.
Reply With Quote
  #5 (permalink)  
Old 02-26-04, 13:43
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Obviously you will take a hit on performance and disk space utilization this way ...

Quote:
I never said "drop column" - indeed there's no such functionality, which you could confirm by RTFM. Your old column will stay there to haunt you forever :-)

However, you can drop a PK constraint and then create it based on the new column.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 02-26-04, 14:08
sks sks is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Hello Sathyaram,

Is there a better way?
Reply With Quote
  #7 (permalink)  
Old 02-26-04, 14:16
disaster disaster is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
Hi,

another idea is to create a new table with hte structure u need.
load the data from the old table into the table. extract all ddl's for view's etc. created on the old table. drop the old table and rename the new table the old table name and create all the other objects neede for the new table. sorry of my english but i'm german.
hope this will help.

regards

marc

Quote:
Originally posted by sks
Hello Sathyaram,

Is there a better way?
Reply With Quote
  #8 (permalink)  
Old 02-26-04, 14:18
sks sks is offline
Registered User
 
Join Date: Sep 2003
Posts: 19
Thx! That might be the best way at this point!
Reply With Quote
  #9 (permalink)  
Old 02-26-04, 14:20
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Again, don't forget to RUNSTATS and ReBIND the exisiting packages

Good luck

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #10 (permalink)  
Old 02-26-04, 14:33
dollar489 dollar489 is offline
Registered User
 
Join Date: Sep 2002
Posts: 456
why not drop the primary key and then alter the column, re-create the primary key again?

dollar

Quote:
Originally posted by sathyaram_s
Again, don't forget to RUNSTATS and ReBIND the exisiting packages

Good luck

Sathyaram
Reply With Quote
  #11 (permalink)  
Old 02-26-04, 14:37
Marcus_A Marcus_A is online now
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
You can't alter the column unless it is varchar.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On