• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

ORA-01779: cannot modify a column which maps to a non key-preserved table on update

Consider the following table:
create table order_line_package(
  Order_Line_Package_Id number(19,0) not null,
  Order_Package_Id number(19,0) not null,
  Order_No number(10,0) not null,
  Project_No varchar(8) not null,
  Entity varchar(15) not null,
  Line_No number (6,2) not null,
  Packed_Quantity number (10,2) not null
)  
ALTER TABLE order_line_package 
    ADD CONSTRAINT order_line_package_PK PRIMARY KEY ( Order_Line_Package_Id ) ;
ALTER TABLE order_line_package 
    ADD CONSTRAINT order_line_package_unq 
      unique( Order_No, Project_No, Entity, Line_No, order_package_id ) ;

Open in new window


I'm trying to execute the following update in order to combine the quantity of a like items:

    update (
      select sl.packed_quantity source_package_qty, dl.packed_quantity dest_package_qty
      from order_line_package sl --source lines
        join order_line_package dl --destination lines
          on sl.Order_No = dl.Order_No
          and sl.Project_No = dl.Project_No
          and sl.Entity = dl.Entity
          and sl.Line_No = dl.Line_No
          and sl.order_package_id = source_package_rec.order_package_id
          and dl.order_package_id = dest_package_rec.order_package_id
    ) set dest_package_qty = dest_package_qty + source_package_qty
    ;

Open in new window


Why do i get the 01779 error in this case?  To me, the destination table is key preserved since all columns of the unique constraint are part of the join.
0
b_levitt
Asked:
b_levitt
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this happens when the relationship is not necessarily one-to-one between the tables.

better to a update/join syntax:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html
0
 
b_levittAuthor Commented:
I guess that's part of my question.  I'm using all columns in a unique constraint so how could it not be one to one?  I think the fact that I'm providing a constant for either side of the final column in that constraint rater than joining it together (which of course is impossible since it would violate the constraint) and that is confusing oracle.

I do appreciate the alternative methods though, thank you.
0
 
GunasekaraneswaranCommented:
I guess the join sl.order_package_id = dl.order_package_id is missing and without this join the values may not be unique.

May be the query should be

 update (
      select sl.packed_quantity source_package_qty, dl.packed_quantity dest_package_qty
      from order_line_package sl --source lines
        join order_line_package dl --destination lines
          on sl.Order_No = dl.Order_No
          and sl.Project_No = dl.Project_No
          and sl.Entity = dl.Entity
          and sl.Line_No = dl.Line_No
          and sl.order_package_id = dl.order_package_id
          and sl.order_package_id = source_package_rec.order_package_id
--          and dl.order_package_id = dest_package_rec.order_package_id
    ) set dest_package_qty = dest_package_qty + source_package_qty
    ;
0
 
b_levittAuthor Commented:
In the end I'm a little disappointed that oracle only supports very simple equijoins with this syntax.  I ended up using the update via correlated query with the exists clause in angelIII's article.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now