| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> comp.databases.oracle -> Re: How to fill empty column with number sequence?
"steve" <me_at_me.com> wrote in message
news:0001HW.BC5A003200008C96F03055B0_at_news.newsguy.com...
| On Fri, 13 Feb 2004 16:11:25 +0800, Ken1 wrote
| (in article <3bb6578e.0402130011.75bd6b51_at_posting.google.com>):
|
| > I am going to drop a primary key from one column and create a new
| > column to be used as primary key in an existing database.
| > The old column was a date column which someone earlier though was a
| > good candidate for a primary key which we all know it's not.
| >
| > Now I want to add a new field, i.e. called ID, with a normal number
| > sequence as primary key.
| >
| > I have dropped the primary key, created the new column, created the
| > new sequence and created a trigger to generate the primary key for new
| > inserts.
| >
| > But I don't know how to generate/update the new ID column with
| > sequence numbers for the already existing records.
| >
| > Any ideas?
| >
| > /Kenneth
|
| create a sequence.
| when you add the new col to the table ,
| in the default value for the col , query the sequence.
|
| this will add a new col with the values already installed.
|
| then for future additions add a trigger. that calls the sequence whenever
the
| table is inserted.
|
| or you could be real lazy an just leave the default value in place.
|
|
|
you can't use a sequence as a default value
from the SQL manual:
note also that the default value can be overridden, including with an explicit null (making default values basically useless)
but you can use the sequence in an update clause
update x
set id = x_seq.nextval;
![]() |
![]() |