?
Solved

Autonumber PK ID - is it Possible in Oracle

Posted on 2003-03-25
2
Medium Priority
?
581 Views
Last Modified: 2013-12-11
Hello,
   I just wanted to know how can I have a Primary Key be an Auto number. In Access you can specify it to be a auto number and so it just increases with each record added. How can I do that in oracle. For Example

CREATE TABLE TEST
(
 tableID NUMBER(6) not null,
 column2 VARCHAR(3),
 column3 DATE,
 CONSTRAINT tableID_pk PRIMARY KEY (tableID));

so How can I have tableID increase it self with each record added.
Thanks in advance
Adil

 
0
Comment
Question by:AdilK
[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
  • 2
2 Comments
 
LVL 7

Expert Comment

by:BobMc
ID: 8206860
You can implement this functionality with a trigger and a sequence:

CREATE SEQUENCE TEST_SEQ START WITH <num of rows in table+1> NOCACHE;

CREATE TRIGGER FOR INSERT on TEST
CREATE TRIGGER TEST_TRIG
BEFORE INSERT ON TEST
FOR EACH ROW
BEGIN
      SELECT TEST_SEQ.NEXTVAL INTO :NEW.tableID
      FROM DUAL;    
END;    
/

Now each time you insert a row, the trigger will populate your column with the next number from the sequence.

HTH
Bob
0
 
LVL 7

Accepted Solution

by:
BobMc earned 100 total points
ID: 8206878
Oops - trugger should just read

CREATE TRIGGER TEST_TRIG
BEFORE INSERT ON TEST
FOR EACH ROW
BEGIN
     SELECT TEST_SEQ.NEXTVAL INTO :NEW.tableID
     FROM DUAL;    
END;    
/

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

752 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