Solved

Oracle Sequence

Posted on 2014-07-31
9
298 Views
Last Modified: 2014-08-05
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!!
0
Comment
Question by:jknj72
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40231906
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
0
 

Accepted Solution

by:
jknj72 earned 0 total points
ID: 40231921
I managed to go around it for now. And what do you mean by "Assuming you are using actual sequences"?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40231935
I meant the actual objects called SEQUENCE and created by

CREATE SEQUENCE sequence_name;

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

Author Comment

by:jknj72
ID: 40231966
well to answer your question I am using Actual Sequences
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 34

Expert Comment

by:johnsone
ID: 40231981
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.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 40231984
FYI,

MAX + 1 will not work if multiple users insert records at the same time.  You will most likely see duplicate values.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40231990
As johnsone said, that is a big no-no.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40232804
You are making a mistake, listen to johnsone or .... SUFFER THE CONSEQUENCES.
0
 

Author Closing Comment

by:jknj72
ID: 40240748
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...
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now