Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use sequence or select max(field) from table to get next id?

 
alex han
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when insert a record to oracle, use sequence or use "select max(id) from table" to get next id??which is efficiently?
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the sequence table is probably safer, in that you are safe from dirty reads. In the Max query you might get a result, add one to it, and when you go to save, someone has already used that number and you get a Primary Key violation.
With a Sequence, you can have Oracle handle it all for you. just use NextVal and no other user will get that number. You can use it in the INSERT statement, or if you need the number first in a GUI, then call SELECT sequence.NextVal from Dual and get the next value.
Hope that helps.
Mark
 
alex han
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ,i will use sequence
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic