Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Please help with no-gap autoincrement field
Mark D Powell wrote:
> Mark Townsend wrote:
>> Malcolm Dew-Jones wrote: >>> g_chime_at_yahoo.com (g_chime_at_yahoo.com) wrote: >>> : I am converting from MySQL to ORACLE and having problems with an >>> : autoincrement field. >>> : MySQL has a built-in autoincrement feature ORACLE doesn't seem to. >>> >>> The fact that oracle sequences are sequential numbers is just an (un)happy >>> coincidence. They would actually have been better off to generate large >>> very random numbers instead of sequential numbers because then noone would >>> be confused as to how to use the result - i.e. as a unique index that has >>> very little other meaning. >>> >> And in a data warehouse you are probably better of using a random >> character string than a random number (or even a number stored as a >> character string)
The chance of losing numbers from a sequence can be dropped to virtually zero with simple error handling.
CREATE TABLE unused_seq_numbers (
seqno NUMBER,
when TIMESTAMP(9),
reason VARCHAR2(250));
CREATE SEQUENCE seq;
DECLARE
i NUMBER;
x unused_seq_numbers.reason%TYPE;
BEGIN
SELECT seq.NEXTVAL
INTO i
FROM dual;
RAISE ZERO_DIVIDE;
EXCEPTION
WHEN OTHERS THEN
x := SQLERRM; INSERT INTO unused_seq_numbers (seqno, when, reason) VALUES (i, SYSTIMESTAMP, x); COMMIT;
SELECT * FROM unused_seq_numbers;
-- Daniel A. Morgan University of Washington damorgan_at_x.washington.edu (replace x with u to respond) Puget Sound Oracle Users Group www.psoug.orgReceived on Wed Jun 14 2006 - 11:42:38 CDT