Home » SQL & PL/SQL » SQL & PL/SQL » Need help on primary key
Need help on primary key [message #226759] Mon, 26 March 2007 08:22 Go to next message
kanis
Messages: 61
Registered: November 2006
Member
Need help on the below topic:

in one table has lots of data ,and have primary key on 5 columns.
Now in a procedure we need to insert values into the table.
In which way should we go ,when we insert same value(primary key) into the table....
1> Check the existance of the primary record by --select query.if dulpicate ,don't go .

2> Wait till primary key violation and trap it .

which will be the right way to implement in code .
pls suggest.
Re: Need help on primary key [message #226768 is a reply to message #226759] Mon, 26 March 2007 08:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68718
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

If the table has a primary key, there is no question.
You have to set the primary key. Point.

Regards
Michel
Re: Need help on primary key [message #226778 is a reply to message #226768] Mon, 26 March 2007 09:06 Go to previous messageGo to next message
kanis
Messages: 61
Registered: November 2006
Member
No this is not my question .
My question is that which way we should go ..
1> check before PK violation ,and do not insert record.
2> Go for PK violation and trap it
Re: Need help on primary key [message #226780 is a reply to message #226759] Mon, 26 March 2007 09:07 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Use an exception block, do NOT check for existance first.

....
...
begin
insert into my_table(col1,col2,col3...) values(....);
exception
when DUP_VAL_ON_INDEX THEN
..... HANDLE YOUR ERROR HERE;
END;
Re: Need help on primary key [message #226781 is a reply to message #226778] Mon, 26 March 2007 09:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68718
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You don't try to understand my answer.
What is the purpose of a PK?
Assert there is uniqueness and guarantee there is no duplicate.

If your application is correct, there should be no duplicate or very rarely.
So duplicate is an exception.
So don't previously check it and handle the exceptions.

Regards
Michel
Re: Need help on primary key [message #226831 is a reply to message #226781] Mon, 26 March 2007 13:33 Go to previous messageGo to next message
orausern
Messages: 826
Registered: December 2005
Senior Member
I am not too sure, but here is what I wish I know:
1)post ur table structure.
2)the code that u use to insert data..for which you have the question related to pk.
3) in that code -point 2- how did u arrive at the value of PK?did u hard code it? or assume it? or did u use some mechanism to calculate it?

just my 1 cent...
Re: Need help on primary key [message #226853 is a reply to message #226831] Mon, 26 March 2007 15:07 Go to previous message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
nirav_hyd wrote on Mon, 26 March 2007 14:33
I am not too sure, but here is what I wish I know:
1)post ur table structure.
2)the code that u use to insert data..for which you have the question related to pk.
3) in that code -point 2- how did u arrive at the value of PK?did u hard code it? or assume it? or did u use some mechanism to calculate it?

just my 1 cent...


Please use whole words.
Previous Topic: sql help
Next Topic: Field length..
Goto Forum:
  


Current Time: Thu Dec 05 00:13:54 CST 2024