954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Autogenerated Primary Key.. Pls Help !!

When some datas are inserted in to a DB table from a jsp page, how do i get the primary key in my table autogenerated?? finiding out the highest value for the primary key in the table? Pls help me wid the coding in JAVA or HTML.. I use SQL*PLus.

lakshma.sugunan
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

you need to create a sequence. As you can guess this is an object that gives you numbers from sequence. then you should get next value from sequence. so you will always have different pk. here is a link that shows you how to create and use it.
after creating it you will probably do something like that

insert into a(pk,col1,col2) values (sqn.nextval,'value1','value3')

yilmazhuseyin
Light Poster
48 posts since Oct 2006
Reputation Points: 31
Solved Threads: 5
 

you can use either a sequence or the max value of PK field +1 for the next unique not null value.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

I dont want to revive a 2008 post but i think ill save somene time, if the table is empty ull get an error simply using max+1, i finally choose to use the NVL2 handy function to get this:

SELECT NVL2(MAX(CODE_COLUMN),MAX(CODE_COLUMN)+1,1) NEXT_CODE_COLUMN FROM TABLE_NAME

emsm
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You