Unix Technical Forum

Oracle sequence - primary key in cycle

This is a discussion on Oracle sequence - primary key in cycle within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Hi, I have a table in a data warehouse which has reached its limit for autogenerated primary key - ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 08:21 AM
ijj@o2.pl
 
Posts: n/a
Default Oracle sequence - primary key in cycle

Hi,

I have a table in a data warehouse which has reached its limit for
autogenerated primary key - a signed 32 bit integer which can be a
decimal number from -2147483648 to 2147483647 (I use Informatica
PowerCenter, ETL tool, which creates it like that).

However, many rows are deleted from this table and because of that some
values of primary key are currently not used, so it looks like:
1,2,5,10, 24, 15, ..... and so on.

I have a question how Oracle sequence should look like when I would
like to generate primary keys in cycle, checking if the next value of
the PK is already used in the table (no insert/update) or not (insert).

I would be very grateful for answer.

Iza

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 08:21 AM
Mark C. Stock
 
Posts: n/a
Default Re: Oracle sequence - primary key in cycle


<ijj@o2.pl> wrote in message
news:1135073069.189985.277890@g44g2000cwa.googlegr oups.com...
> Hi,
>
> I have a table in a data warehouse which has reached its limit for
> autogenerated primary key - a signed 32 bit integer which can be a
> decimal number from -2147483648 to 2147483647 (I use Informatica
> PowerCenter, ETL tool, which creates it like that).
>
> However, many rows are deleted from this table and because of that some
> values of primary key are currently not used, so it looks like:
> 1,2,5,10, 24, 15, ..... and so on.
>
> I have a question how Oracle sequence should look like when I would
> like to generate primary keys in cycle, checking if the next value of
> the PK is already used in the table (no insert/update) or not (insert).
>
> I would be very grateful for answer.
>
> Iza
>


the sequence can't handle that in and of itself -- you'll need to code a
check against the PK or trap the exception.

something like (just an example, not saying this is the best way to handle
it):

create or replace function get_next_deptno_pk
return number
is
n_retval number;
n_exists number := 0;
begin
loop
select detno_seq.nextval into n_retval from dual;
select count(*) into n_exists from dept where deptno = n_retval;
exit when n_exists = 0;
end loop;
return n_retval;
end get_next_deptno_pk;

once you hit the sequence's maxvalue, this code will raise ORA-08004 and
you'll be back in the same boat

++ mcs


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 08:22 AM
DA Morgan
 
Posts: n/a
Default Re: Oracle sequence - primary key in cycle

ijj@o2.pl wrote:
> Hi,
>
> I have a table in a data warehouse which has reached its limit for
> autogenerated primary key - a signed 32 bit integer which can be a
> decimal number from -2147483648 to 2147483647 (I use Informatica
> PowerCenter, ETL tool, which creates it like that).
>
> However, many rows are deleted from this table and because of that some
> values of primary key are currently not used, so it looks like:
> 1,2,5,10, 24, 15, ..... and so on.
>
> I have a question how Oracle sequence should look like when I would
> like to generate primary keys in cycle, checking if the next value of
> the PK is already used in the table (no insert/update) or not (insert).
>
> I would be very grateful for answer.
>
> Iza


Mark has answered your question with respect to sequence generated
numbers. But what why not deal with the root cause: A bad design.
Why do you need a surrogate key at all? And if you do ... it seems
one that is non-numeric would make a lot more sense.
--
Daniel A. Morgan
http://www.psoug.org
damorgan@x.washington.edu
(replace x with u to respond)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:22 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com