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 > How to delete a column in the existing table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-05, 04:56
act7656 act7656 is offline
Registered User
 
Join Date: Mar 2004
Posts: 24
Question How to delete a column in the existing table?

Hi there,


DB version : DB2 UDB 7.2
OS : Win 2K Server

what is the SQL syntax to delete a column in the existing table?
__________________
Hello
Reply With Quote
  #2 (permalink)  
Old 02-25-05, 05:19
sordax sordax is offline
Registered User
 
Join Date: Feb 2005
Posts: 12
Well, all you have to do is

ALTER TABLE table_name DROP COLUMN column_name CASCADE

CASCADE option will delete depending indexes and constraints. You can specify RESTRICT as well if you dont want this column to be deleted if those indexes or constraints exist.
Reply With Quote
  #3 (permalink)  
Old 02-25-05, 08:17
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
There is no command in DB2 to drop a column--in any version. You will have to:
1) export date from table using select statement without column
2) drop table
3) create table minus column
4) import data from step 1
5) recreate any indexes, RI, PKs

Andy
Reply With Quote
  #4 (permalink)  
Old 02-25-05, 09:55
prithvi_raj prithvi_raj is offline
Registered User
 
Join Date: Apr 2004
Posts: 36
AR ,
How do you get the FKeys. Do we have to take db2look.
is there any other work around to easily identiy the FKs on that table and from that to another table.

If we are droping a column on more than one table. Its going to be a lot of work.
Reply With Quote
  #5 (permalink)  
Old 02-25-05, 10:00
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes, use db2look to generate the DDL for the FKs, indexes and PKs.
Edit the output as necessary.

Yes, dropping a column this way can be a lot of work.

Andy
Reply With Quote
  #6 (permalink)  
Old 02-25-05, 12:42
sordax sordax is offline
Registered User
 
Join Date: Feb 2005
Posts: 12
Quote:
Originally Posted by ARWinner
There is no command in DB2 to drop a column--in any version.
And whats up with ALTER TABLE ... DROP COLUMN???
Reply With Quote
  #7 (permalink)  
Old 02-25-05, 12:49
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Sordax,
There is no such command in DB2 UDB for LUW.
Andy
Reply With Quote
  #8 (permalink)  
Old 02-25-05, 12:53
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
There is no ALTER TABLE ... DROP COLUMN in DB2 for LUW.

However, in version 8.2 (I am using FP 8) the Control Center has a function to drop a column.

I believe that it automatically exports the data, drops the table, recreates the table and keys without the column to be dropped, and reloads the data. It uses a stored procedure with multiple input parameters to do this. You could look at the SQL it generates and do this in batch.
__________________
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
  #9 (permalink)  
Old 02-25-05, 17:33
sordax sordax is offline
Registered User
 
Join Date: Feb 2005
Posts: 12
Oh, I don't know about DB2 for LUW, I'm working with DB2 for iSeries.
Reply With Quote
  #10 (permalink)  
Old 02-25-05, 17:48
jsdba jsdba is offline
Registered User
 
Join Date: Sep 2004
Posts: 12
Are you using any Change Management software on your iSeries? If you had something like TurnOver from SoftLanding Systems, then making a change like this would be easy - however, the underlying steps would still be:

1. Copy the existing data to a temp file
2. Drop the current table definition
3. Recreate the new table without the column
4. Create the indexes
5. Build any FK / RIs
6. Copy back in the data saved off in the temp file

Doing it manually can be risky so make sure you have good backups.
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