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 36774 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
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.
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.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 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 Inventory..pc in
%%i -fc:\bcp\bcp.fmt -T -S CHICKYy
where CHICKYy is the server
bcp.fmt
|
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.
|
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 2 3 4 < Column Number.
ABC DEF GHI JKL
Did briefly try programming a counter but can't find where to put
|
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:
| |
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:
|
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
|
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...
|
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...
|
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,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |