Solved

Oracle Sequence

Posted on 2014-07-31
9
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Security & Encryption in the MySQL world

Join Percona’s Solutions Engineer, Dimitri Vanoverbeke as he presents “Security and Encryption in the MySQL world” on Thursday, July 6, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 

Author Comment

by:jknj72
ID: 40231966
well to answer your question I am using Actual Sequences
0
 
LVL 35

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 35

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

Upcoming Webinar: Securing your MySQL/MariaDB data

Join Percona’s Chief Evangelist, Colin Charles as he presents Securing your MySQL®/MariaDB® data on Tuesday, July 11, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

695 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