Link to home
Start Free TrialLog in
Avatar of jknj72
jknj72

asked on

Oracle Sequence

I have a table that Im working with and it has a sequence to get the next value for the ID field on the table. The ID field is NOT a primary key. I want to change it but for now I see it has dependencies and I dont want to mess anything up. I noticed that when I did an Insert it is inserting a duplicate value of 3 into the this field. I opened the Sequence and the Last Number is now 3 so I understand how this happened. My question is, can I have the LastNumber look at my ID field and determine the next number? Or does this have to be a primary key in order for that to happen?

Thanks for any help!!
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Assuming you are using actual sequences,  they have nothing to do with primary keys (except that they are frequently used for setting their values).

The same sequence could be used for multiple tables, and sequences can be reset by dropping and recreating.

This site describes how the value stored in a sequence can be changed:

http://www.gokhanatil.com/2011/01/how-to-set-current-value-of-a-sequence-without-droppingrecreating.html
ASKER CERTIFIED SOLUTION
Avatar of jknj72
jknj72

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I meant the actual objects called SEQUENCE and created by

CREATE SEQUENCE sequence_name;

statements (rather than just a logical sequence of numbers).
Avatar of jknj72
jknj72

ASKER

well to answer your question I am using Actual Sequences
Avatar of johnsone
Also, be aware that a sequence can get to its maximum value and then start again at the minimum value.  This is controlled by specifying CYCLE or NOCYCLE when creating the sequence.
FYI,

MAX + 1 will not work if multiple users insert records at the same time.  You will most likely see duplicate values.
As johnsone said, that is a big no-no.
You are making a mistake, listen to johnsone or .... SUFFER THE CONSEQUENCES.
Avatar of jknj72

ASKER

FYI, I simply did a MAX + 1 on the ID field and used that for my next value. I know, not the ideal way to do things but until I can decipher whether changing the table and putting a primary key on it, possibly with a trigger for incrementing, then this will have to do...