Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

create auto increment id for oracle database

 
Ahmad Shekha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone :
I want to create auto increment id in oracle 11g in preparedStatement , I realize is no auto-increment in oracle

this is my code


but I have this run time error

note: I am do not use trigger and I do not know how I can use it
please help i'm beginner in programming
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Oracle you should define a sequence in your database schema e.g.

CREATE SEQUENCE emp_id_seq;

Then you can get the next value off this sequence in your SQL INSERT statement:

INSERT INTO employee (id, firstname, ...) VALUES(emp_id_seq.NEXTVAL, ?,....)

There are other ways to use a sequence e.g. set the ID via a trigger, or if you're using an ORM you can tell it to use the sequence for generating IDs for this table. But right now this is probably the simplest approach.

Oracle sequence values are guaranteed to be unique, and there are various additional options e.g. for caching sets of sequence values, specifying the incremental step, re-cycle point etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic