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 5 36730 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
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.
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
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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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 ...
|
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:...
|
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 =...
|
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.
-->
...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |