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: 64152
Registered: March 2007
Location: Nanterre, France, http://...
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: 1484
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: 64152
Registered: March 2007
Location: Nanterre, France, http://...
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: 817
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: 4644
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: Sat Dec 10 03:30:48 CST 2016

Total time taken to generate the page: 0.07736 seconds