473,765 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 36776
ke**@tjohoo.se (Ken1) wrote in message news:<3b******* *************** ****@posting.go ogle.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@nextva l;

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.new sguy.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 "Expression s" 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
14399
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 Inventory..pc in %%i -fc:\bcp\bcp.fmt -T -S CHICKYy where CHICKYy is the server bcp.fmt
6
8724
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 following would work: update foo set bar = rownum; ....but 'rownum' does not seem to exist in mssql. The numbers do not need to be in order, but I would like to keep them somewhat small.
2
1470
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
1718
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 3 4 < Column Number. ABC DEF GHI JKL Did briefly try programming a counter but can't find where to put
2
1740
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: http://www.grozzler.com/dump/dt_want.png See how that final column is empty and stretches the entire width instead of leaving a giant hole like this:
3
1925
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 = tempA.Table.Columns(5).ColumnName() I can't figure out the code to return the column number if I have the column name. E.g., something like this:
0
1573
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. --> <AffectedBillingComponentsRequest
5
451
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 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...
0
9566
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6646
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.