473,320 Members | 1,958 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,320 software developers and data experts.

re-numbering pimary-key

I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Anyone have any lessons leaned that I need to watch in doing this?

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


Jul 19 '05 #1
18 4038
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Anyone have any lessons leaned that I need to watch in doing this?

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


Mike, I have actually gone through and migrated all related rows in an
application where the parent table key was a sequence value.
Logically it was not that hard to write the pl/sql code I used to
perform the processing but it was a fairly expensive process from the
point of view of the work that has to be done by Oracle, indexes
updated, redo generated, etc....

Here is an alternate that we are about to do today. Due to a sequence
that is about to recycle (due to size limitation of external system we
feed) and where we must first remove existing data to allow the reuse
of the sequence value, but where the customer does not want any data
removed until after year-end since this will distort their
year-to-date information, is to substitute our own home-grown
sequence.

What I did was write a function as an anonymous transaction that reads
one row from an IOT that was populated with the missing (skipped)
sequence numbers using select for update, delete, commit, return the
selected value.

It seems to test just fine. As long as you are on Oracle version 8i
and the function is not called as part of a distributed transaction
this may be a simplier route to take. With version 9 the distributed
transaction restriction should be removed. We just substituted our
function name for the sequence, recompiled the four programs that call
the sequence, and tested.

HTH -- Mark D Powell --
Jul 19 '05 #2
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Anyone have any lessons leaned that I need to watch in doing this?

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


Mike, I have actually gone through and migrated all related rows in an
application where the parent table key was a sequence value.
Logically it was not that hard to write the pl/sql code I used to
perform the processing but it was a fairly expensive process from the
point of view of the work that has to be done by Oracle, indexes
updated, redo generated, etc....

Here is an alternate that we are about to do today. Due to a sequence
that is about to recycle (due to size limitation of external system we
feed) and where we must first remove existing data to allow the reuse
of the sequence value, but where the customer does not want any data
removed until after year-end since this will distort their
year-to-date information, is to substitute our own home-grown
sequence.

What I did was write a function as an anonymous transaction that reads
one row from an IOT that was populated with the missing (skipped)
sequence numbers using select for update, delete, commit, return the
selected value.

It seems to test just fine. As long as you are on Oracle version 8i
and the function is not called as part of a distributed transaction
this may be a simplier route to take. With version 9 the distributed
transaction restriction should be removed. We just substituted our
function name for the sequence, recompiled the four programs that call
the sequence, and tested.

HTH -- Mark D Powell --
Jul 19 '05 #3
> I just tried to change some in test and of course it wouldn't let me change any because of the
constraints. I was afraid of that.
Mike, I have actually gone through and migrated all related rows in an
application where the parent table key was a sequence value.
Logically it was not that hard to write the pl/sql code I used to
perform the processing but it was a fairly expensive process from the
point of view of the work that has to be done by Oracle, indexes
updated, redo generated, etc....

Here is an alternate that we are about to do today. Due to a sequence
that is about to recycle (due to size limitation of external system we
feed) and where we must first remove existing data to allow the reuse
of the sequence value, but where the customer does not want any data
removed until after year-end since this will distort their
year-to-date information, is to substitute our own home-grown
sequence.

So all you are doing is re-using the holes that exist. Do you have an example of that code. This may be a
better idea.

What I did was write a function as an anonymous transaction that reads
one row from an IOT that was populated with the missing (skipped)
sequence numbers using select for update, delete, commit, return the
selected value.

It seems to test just fine. As long as you are on Oracle version 8i
and the function is not called as part of a distributed transaction
this may be a simplier route to take. With version 9 the distributed
transaction restriction should be removed. We just substituted our
function name for the sequence, recompiled the four programs that call
the sequence, and tested.

HTH -- Mark D Powell --


Jul 19 '05 #4
> I just tried to change some in test and of course it wouldn't let me change any because of the
constraints. I was afraid of that.
Mike, I have actually gone through and migrated all related rows in an
application where the parent table key was a sequence value.
Logically it was not that hard to write the pl/sql code I used to
perform the processing but it was a fairly expensive process from the
point of view of the work that has to be done by Oracle, indexes
updated, redo generated, etc....

Here is an alternate that we are about to do today. Due to a sequence
that is about to recycle (due to size limitation of external system we
feed) and where we must first remove existing data to allow the reuse
of the sequence value, but where the customer does not want any data
removed until after year-end since this will distort their
year-to-date information, is to substitute our own home-grown
sequence.

So all you are doing is re-using the holes that exist. Do you have an example of that code. This may be a
better idea.

What I did was write a function as an anonymous transaction that reads
one row from an IOT that was populated with the missing (skipped)
sequence numbers using select for update, delete, commit, return the
selected value.

It seems to test just fine. As long as you are on Oracle version 8i
and the function is not called as part of a distributed transaction
this may be a simplier route to take. With version 9 the distributed
transaction restriction should be removed. We just substituted our
function name for the sequence, recompiled the four programs that call
the sequence, and tested.

HTH -- Mark D Powell --


Jul 19 '05 #5
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.
read this in the same tone of voice that Morpheus said to Neo: "you
think that's air you're breathing?" 8^)
you think this will keep your primary key values in sequence without
gaps?

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Why? is there a business reason, or do you just prefer to be neat?
Anyone have any lessons leaned that I need to watch in doing this?
Let's see you either disable all foreign key constraints involved
while doing this or dump your data to files or temp tables, empty your
target tables and reload, forcing the primary and foreign key values
to be what you want.

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


Why the two-phase approach? (12->98->4) why not just renumber lowest
to highest? there is no overlap involved, just do one PK then all it's
child FKs. It's a simple algorithm.

Be I really cannot emphasize enough that you DON'T NEED to do this,
unless there is a real business reason.

and a final comment: This really should be posted in
comp.databases.oracle.misc

the comp.databases.oracle group is outdated and being phased out.

HTH,
Ed Prochak

-- there are no stupid questions.
Jul 19 '05 #6
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.
read this in the same tone of voice that Morpheus said to Neo: "you
think that's air you're breathing?" 8^)
you think this will keep your primary key values in sequence without
gaps?

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Why? is there a business reason, or do you just prefer to be neat?
Anyone have any lessons leaned that I need to watch in doing this?
Let's see you either disable all foreign key constraints involved
while doing this or dump your data to files or temp tables, empty your
target tables and reload, forcing the primary and foreign key values
to be what you want.

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


Why the two-phase approach? (12->98->4) why not just renumber lowest
to highest? there is no overlap involved, just do one PK then all it's
child FKs. It's a simple algorithm.

Be I really cannot emphasize enough that you DON'T NEED to do this,
unless there is a real business reason.

and a final comment: This really should be posted in
comp.databases.oracle.misc

the comp.databases.oracle group is outdated and being phased out.

HTH,
Ed Prochak

-- there are no stupid questions.
Jul 19 '05 #7
User wants to be neat


Why? is there a business reason, or do you just prefer to be neat?

Disable them? Don't you mean drop them and then rebuild them afterwards? How can they be disabled?


Let's see you either disable all foreign key constraints involved
while doing this or dump your data to files or temp tables, empty your
target tables and reload, forcing the primary and foreign key values
to be what you want.

yes, what am I thinking .....

Why the two-phase approach? (12->98->4) why not just renumber lowest
to highest? there is no overlap involved, just do one PK then all it's
child FKs. It's a simple algorithm.

There are only 80 records in the table, but the numbering is up to 1046.

Be I really cannot emphasize enough that you DON'T NEED to do this,
unless there is a real business reason.

Ok next time ...

and a final comment: This really should be posted in
comp.databases.oracle.misc

the comp.databases.oracle group is outdated and being phased out.

HTH,
Ed Prochak

-- there are no stupid questions.


Jul 19 '05 #8
User wants to be neat


Why? is there a business reason, or do you just prefer to be neat?

Disable them? Don't you mean drop them and then rebuild them afterwards? How can they be disabled?


Let's see you either disable all foreign key constraints involved
while doing this or dump your data to files or temp tables, empty your
target tables and reload, forcing the primary and foreign key values
to be what you want.

yes, what am I thinking .....

Why the two-phase approach? (12->98->4) why not just renumber lowest
to highest? there is no overlap involved, just do one PK then all it's
child FKs. It's a simple algorithm.

There are only 80 records in the table, but the numbering is up to 1046.

Be I really cannot emphasize enough that you DON'T NEED to do this,
unless there is a real business reason.

Ok next time ...

and a final comment: This really should be posted in
comp.databases.oracle.misc

the comp.databases.oracle group is outdated and being phased out.

HTH,
Ed Prochak

-- there are no stupid questions.


Jul 19 '05 #9
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Anyone have any lessons leaned that I need to watch in doing this?

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


By the way the cache size is how many numbers are stored in memory so
that an IO to disk does not have to be done until the cache is
exhausted (or flushed). The increment is what determines the number
by which the sequence values vary from each other.

And Ed will probably tell you this but the alter table command
includes an option to enable/disable constraints. See the SQL manual.
This explnation should be in the Concepts manual section on
constraints.

HTH -- Mark D Powell --
Jul 19 '05 #10
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
I have a series of tables that have primary-key / foreign key
relationships where the primary key was being incremented in error by 20
instead of by 1. The default cache value was set to 20. It has since
been re-set to 1.

I'd like to re-number the primary key and then re-set the sequence then
back to the correct number to increment.

Anyone have any lessons leaned that I need to watch in doing this?

Example of proposed numbering change:
If I have 1,4,8,12,18

I would change:
18 to 99,
12, to 98.

Then change
4 to 2,
8 to 3,
98 to 4,
99 to 5.

Then change the next sequence number to 6.

Anyone see any problem with this?

Mike


By the way the cache size is how many numbers are stored in memory so
that an IO to disk does not have to be done until the cache is
exhausted (or flushed). The increment is what determines the number
by which the sequence values vary from each other.

And Ed will probably tell you this but the alter table command
includes an option to enable/disable constraints. See the SQL manual.
This explnation should be in the Concepts manual section on
constraints.

HTH -- Mark D Powell --
Jul 19 '05 #11
> >

Disable them? Don't you mean drop them and then rebuild them afterwards? How can they be disabled?

I use Toad for that.
Jul 19 '05 #12
> >

Disable them? Don't you mean drop them and then rebuild them afterwards? How can they be disabled?

I use Toad for that.
Jul 19 '05 #13
So, I am at a lost at how a cache size of 20 was causing my numbers to increment by 20, and then changing it
to 0 is incrementing my numbers correctly.


By the way the cache size is how many numbers are stored in memory so
that an IO to disk does not have to be done until the cache is
exhausted (or flushed). The increment is what determines the number
by which the sequence values vary from each other.

And Ed will probably tell you this but the alter table command
includes an option to enable/disable constraints. See the SQL manual.
This explnation should be in the Concepts manual section on
constraints.

HTH -- Mark D Powell --


Jul 19 '05 #14
So, I am at a lost at how a cache size of 20 was causing my numbers to increment by 20, and then changing it
to 0 is incrementing my numbers correctly.


By the way the cache size is how many numbers are stored in memory so
that an IO to disk does not have to be done until the cache is
exhausted (or flushed). The increment is what determines the number
by which the sequence values vary from each other.

And Ed will probably tell you this but the alter table command
includes an option to enable/disable constraints. See the SQL manual.
This explnation should be in the Concepts manual section on
constraints.

HTH -- Mark D Powell --


Jul 19 '05 #15
Okay, you seem to prefer top posting, but I don't. Comments follow
below.

Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
User wants to be neat


Why? is there a business reason, or do you just prefer to be neat?


Here's the followup question:
What are you going to do tomorrow when gaps reappear?

<and the oracle continues, saying>
you are about to ask, Why will the gaps come back?
<end of another Matrix reference>

Since you are using a sequence, there are several values cached
(essentially preallocated) with each session that uses that sequence.
When you close that session, the unused values are "lost". As
mentioned elsewhere in this thread, you need to read and understand
the Concepts manual.

[]
There are only 80 records in the table, but the numbering is up to 1046.

Be I really cannot emphasize enough that you DON'T NEED to do this,
unless there is a real business reason.


But using this approach will require you to renumber after more
records are added. So unless these tables are static, you will get
gaps as new records are added. And what does the user want if some
records geet deleted?

There are ways to get the sequence without gaps, but they are
EXPENSIVE in terms of overhead and scalability. You really need to
convince your client not to do this. They probably shouldn't need to
see these values anyway. Don't do this just to be neat.

Ed
Jul 19 '05 #16
Okay, you seem to prefer top posting, but I don't. Comments follow
below.

Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
User wants to be neat


Why? is there a business reason, or do you just prefer to be neat?


Here's the followup question:
What are you going to do tomorrow when gaps reappear?

<and the oracle continues, saying>
you are about to ask, Why will the gaps come back?
<end of another Matrix reference>

Since you are using a sequence, there are several values cached
(essentially preallocated) with each session that uses that sequence.
When you close that session, the unused values are "lost". As
mentioned elsewhere in this thread, you need to read and understand
the Concepts manual.

[]
There are only 80 records in the table, but the numbering is up to 1046.

Be I really cannot emphasize enough that you DON'T NEED to do this,
unless there is a real business reason.


But using this approach will require you to renumber after more
records are added. So unless these tables are static, you will get
gaps as new records are added. And what does the user want if some
records geet deleted?

There are ways to get the sequence without gaps, but they are
EXPENSIVE in terms of overhead and scalability. You really need to
convince your client not to do this. They probably shouldn't need to
see these values anyway. Don't do this just to be neat.

Ed
Jul 19 '05 #17
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
So, I am at a lost at how a cache size of 20 was causing my numbers to increment by 20, and then changing it
to 0 is incrementing my numbers correctly.


If your database is shut down or your sequence ages out of the shared
pool, the cached numbers are lost. Setting a cache size of 0 incurs a
fair amount of performance overhead, but it does prevent the loss of
sequence numbers to these two processes. There are still ways to
"lose" sequence numbers where the cache is 0, though-- you're never
guaranteed to have a gap-free sequence in Oracle.

Justin Cave
Jul 19 '05 #18
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<3F***************@ram.lmtas.lmco.com>...
So, I am at a lost at how a cache size of 20 was causing my numbers to increment by 20, and then changing it
to 0 is incrementing my numbers correctly.


If your database is shut down or your sequence ages out of the shared
pool, the cached numbers are lost. Setting a cache size of 0 incurs a
fair amount of performance overhead, but it does prevent the loss of
sequence numbers to these two processes. There are still ways to
"lose" sequence numbers where the cache is 0, though-- you're never
guaranteed to have a gap-free sequence in Oracle.

Justin Cave
Jul 19 '05 #19

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

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.