By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,197 Members | 972 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,197 IT Pros & Developers. It's quick & easy.

re-numbering pimary-key

P: n/a
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
Share this Question
Share on Google+
18 Replies


P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> >

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

P: n/a
> >

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.