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 > altter table add column .... identity

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-26-04, 08:29
b_moni b_moni is offline
Registered User
 
Join Date: Aug 2003
Posts: 16
Red face altter table add column .... identity

I need to create a table which has an identity column and some columns from another table.
I tried many ways, I searched the forums and of course I read the documentation, but I didn't succeed.

I tried something like:

create table t as (select 0 as id generated by default as identity, col1,..coln from table) definition only

it didn't work!

I tried to create first the new table from the old table, and after theat to alter the table an add the identity column, like this:

alter table t add column id int generated always as identity

but it didn't worlk also !

I don't know what to do anymore.
Please help!

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 03-26-04, 09:29
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Couldn't you just db2look the original table def, save it to a file, add your column/change the name and run?
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #3 (permalink)  
Old 03-26-04, 11:43
b_moni b_moni is offline
Registered User
 
Join Date: Aug 2003
Posts: 16
No, I can't use db2look, because I'm using Java, so I must create the table using JDBC.
Reply With Quote
  #4 (permalink)  
Old 03-26-04, 13:00
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally posted by b_moni
No, I can't use db2look, because I'm using Java, so I must create the table using JDBC.
I'm not sure there's an easy way to do this without the original table definition. As you said, ALTER won't work with IDENTITY, and you can't add columns when you're using the "AS" clause.

You might get away with using the MetaData classes JDBC provides, but that's fairly ugly... and won't give you the whole definition, probably just the data types.

ie. DatabaseMetaData's getColumns() method...

Anyone else have any ideas?
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #5 (permalink)  
Old 03-26-04, 13:03
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally posted by J Petruk

You might get away with using the MetaData classes JDBC provides, but that's fairly ugly... and won't give you the whole definition, probably just the data types.
As it turns out, getColumns() gives a LOT of information beyond the data types...

Even so, not sure it'll make it easy to build a CREATE TABLE from.
__________________
--
Jonathan Petruk
DB2 Database Consultant
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