Solved

update two tables in single query oracle

Posted on 2013-10-24
9
610 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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
 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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