Solved

update two tables in single query oracle

Posted on 2013-10-24
9
603 Views
Last Modified: 2013-12-05
Hi,

I have 2 tables, dp_cardprofile and dp_simapplication. Both tables have the same identifier, DPCP_ID and they have a foreign key relation between them.

I need to update this dpcp_id in both tables, but if I update in just one, I receive an error.

ORA-02291: integrity constraint (DPOWNER.DP_SIMAPPLICATION_FK_DP_CARDPR)
violated - parent key not found

How can I do this update in DPCP_ID in Oracle 10?


desc dp_cardprofile
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DPCP_ID                                   NOT NULL NUMBER(38)
 DPCP_NAME                                 NOT NULL VARCHAR2(50)


desc dp_simapplication
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DPSA_ID                                   NOT NULL NUMBER(38)
 DPCP_ID                                   NOT NULL NUMBER(38)
0
Comment
Question by:joaotelles
  • 4
  • 3
9 Comments
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 400 total points
ID: 39597047
The need to change PKs indicate a "weak" DB design/pattern... Re-think what you are doing ;-)

To your problem: take a look here

In Oracle there is no 'on update cascade', but there is a workaround (you'll have to change your FK to a deferrable constraint):

http://www.orafaq.com/wiki/Foreign_key#ON_UPDATE_CASCADE

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:5773459616034
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 100 total points
ID: 39597048
Don't know the Oracle answer, but since you also posted in the SQL Server zone, the SQL Server answer is:

Given that the FK value between the two tables is known.
You CAN'T update multiple tables at the same time.  
What you CAN do is create a transaction that updates two tables with two separate UPDATE statements.  That way, both updates either succeed or fail together.
BEGIN TRAN tr
   BEGIN TRY
       UPDATE #1 goes here
       UPDATE #2 goes here

       COMMIT TRAN tr    -- If code execution makes it here, we're all good, so commit
   END TRY

   BEGIN CATCH
       ROLLBACK TRAN tr   -- If code execution makes it here, an error occured, so kill the transaction
   END CATCH

Open in new window

0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39598250
so, this was a "SQL Server" question?!?

In Oracle you can update 2 or more tables within 1 statement...
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39601252
@EE admins

SQL Server????????

ORA-02291: integrity constraint (DPOWNER.DP_SIMAPPLICATION_FK_DP_CARDPR)
violated - parent key not found

How can I do this update in DPCP_ID in Oracle 10?


desc dp_cardprofile
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DPCP_ID                                   NOT NULL NUMBER(38)
 DPCP_NAME                                 NOT NULL VARCHAR2(50)

SQL Server doesn't know ORA errors or VARCHAR2 data types, neither the code provided above!!!!

So, where the heck is the connection (apart from awarding a solution that totally differs from the origin subject)?!?!?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 65

Expert Comment

by:Jim Horn
ID: 39601287
Relax dude.  

The asker included the SQL Server zone in this question, so it's appropriate to give a SQL answer, although I didn't expect it to get full points as the question had Oracle-specific language.

I would think though that everything else being equal an Oracle-specific answer would be more relevant than a SQL Server-specific answer for this question.   So .. it seems appropriate that the asker should chime in on that.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39601419
I didn't mean to bother anyone, so sorry for my offensive reaction ;-)
Moreover, I am not that points addicted, I just wanted to state that something was kind of wrong here...
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39601455
It's a valid point.
0
 

Author Closing Comment

by:joaotelles
ID: 39698841
tks and sry the zone confusion.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now