Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: SQL question

RE: SQL question

From: Rick Stephenson <RStephenson_at_Ovid.com>
Date: Mon, 24 Feb 2003 12:54:19 -0800
Message-ID: <F001.00557610.20030224125419@fatcity.com>


Sorry, I guess I could have been a little more clear.

Another example:

Table Employee:
  Emp_id number primary key -- generated with a sequence   Emp_name varchar2(20) unique

Table Employee_log:
  Emp_id number primary key
  Time_stamp date primary key
  Emp_stats varchar2(50)

A process receives the employee name, and other information that needs to be stored in the table employee_log. The process needs to retrieve the emp_id from the employee table, so it does a lookup. If the employee exists, the emp_id is retrieved and the information is then inserted into the employee_log table. If the employee does not currently exist, a new employee is added to the table employee.

We run into problems when we have many concurrent processes running and more than one process receives the same employee name. They both do a lookup and they both conclude the employee does not exist. Thus, they both try and do an insert into the employee table. One will succeed and the other will fail.

Is there away to avoid this scenario?

I hope I made this a little clearer.

Thanks,

Rick Stephenson

-----Original Message-----
Sent: Monday, February 24, 2003 1:05 PM
To: Multiple recipients of list ORACLE-L

Rick - What about selecting the primary key for your table from a sequence? Oracle will ensure each session receives a unique number.  

What is your overall goal?

Dennis Williams
DBA, 40%OCP, 100% DBA
Lifetouch, Inc.
dwilliams_at_lifetouch.com

-----Original Message-----
Sent: Monday, February 24, 2003 1:50 PM
To: Multiple recipients of list ORACLE-L

OS: Solaris 2.8

Database: Oracle 9.2.0.2  

Situation in chronological order

Connection A: select * from table A where id = 1; Result: no rows returned
-- This means I need to insert the row, as it does not exists yet.

Connection B: select * from table A where id = 1; Result: no rows returned
-- This means I need to insert the row, as it does not exists yet.

Connection A: insert into table A(id) values = 1; Result: 1 row inserted

Connection B: insert into table A(id) values = 1; Result: Unique constraint violated -- This is the problem. How do I avoid this happening?  

Question: How can I force connection B to wait for connection A to insert the new row before it does the select?  

If I were updating the row, I could use the "for update" clause to force the wait. Is there a clean way to do that for an insert?  

Thanks for your help,  

Rick Stephenson  

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, forwarding or otherwise distributing or taking any action in reliance on the contents of this information is strictly prohibited.

--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author: DENNIS WILLIAMS
  INET: DWILLIAMS_at_LIFETOUCH.COM

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, forwarding or otherwise distributing or taking any action in reliance on the contents of this information is strictly prohibited.

--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author: Rick Stephenson
  INET: RStephenson_at_Ovid.com

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Mon Feb 24 2003 - 14:54:19 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US