473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to fill empty column with number sequence?

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
Jul 19 '05 #1
5 36725
ke**@tjohoo.se (Ken1) wrote in message news:<3b**************************@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


A simple PL/SQL process can do it.
open the table for all rows,
count each row fetched,
and update the ID to the count value.

HTH,
ed
Jul 19 '05 #2
You can create a temporary trigger that fires on updates. It will be
somewhat similar to your insert trigger, e.g.
if (:new.id = -1) then
:new.id := seq_name@nextval;

Then, just update your whole table:
update tablename
set id = -1;

Remove the temporary update trigger after that.
Jul 19 '05 #3
boa
Ken1 wrote:
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?
How about using rownum to generate id's for the existing rows?

alter table foo add bar number;
update foo set bar = rownum;

Remember to create the sequence with the proper "START WITH" number,
which should be max(bar) + 1.

HTH
Boa

/Kenneth

Jul 19 '05 #4
On Fri, 13 Feb 2004 16:11:25 +0800, Ken1 wrote
(in article <3b**************************@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.

Jul 19 '05 #5
"steve" <me@me.com> wrote in message
news:00*****************************@news.newsguy. com...
| On Fri, 13 Feb 2004 16:11:25 +0800, Ken1 wrote
| (in article <3b**************************@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:

--------------------------------------------------------------------------
DEFAULT specifies a value to be assigned to the column if a subsequent
INSERT statement omits a
value for the column. The datatype of the expression must match the datatype
of the
column. The column must also be long enough to hold this expression. For the
syntax of
expr, see "Expressions" on page 5-1.
Restriction: A DEFAULT expression cannot contain references to other
columns, the
pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM, or date constants that
are not
fully specified.
--------------------------------------------------------------------------

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;

-- mcs
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michelle Hillard | last post by:
Hi guys, would appreciate if you can shed some light on this. Sorry to be a pain, can you tell me what is wrong with the following: for /F %%i in ('dir /b /on c:\bcp\pc*.txt') do bcp...
6
by: python1 | last post by:
I apologize if this is redundant. How would one fill an empty column with a sequence of numbers? The column exists in a table with aproximately 1000000 rows of data. I believe in oracle the...
2
by: edurne murgiondo via DBMonster.com | last post by:
I have a table with a column as identity. I don't know how can migrate a identity column to a normal column using sequence. Thnaks -- Message posted via http://www.dbmonster.com
0
by: Greg Reed | last post by:
I have a 4 column report and wondered if there is column number property. I have more complicated uses for it but basically could I Have column number shown at ctop of each column 1 ...
2
by: GroZZleR | last post by:
Hey all, How do things like Windows Explorer and other applications get an empty column at the end of a data grid that stretches the entire width? Look at this picture:...
3
by: Jeff | last post by:
In .net2 using VB (Visual Web), I have the following code that properly determines the column name from the column number (5 in this example) from a single row table. ColName =...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
5
by: Ken1 | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.