• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 46
  • 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?
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ,i will use sequence
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic