user3693954
user3693954

Reputation: 43

How to update a column which is part of a composite key and acting as primary key for one table and as a foreign key in other table in oracle SQL

I have two tables TableA and TableB where TableA has columns col1, col2, col3, col4, col5 and col1, col2 and col3 combine to form its primary key. TableA and TableB has one-to-many relationship on the same column(col1, col2 and col3) as its foreign key constraint. Now how can I update just col2 values in both TableA and TableB in SQL and in Hibernate?

Upvotes: 4

Views: 2939

Answers (1)

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48197

Doesn't matter if is hibernate or the number of columns on the primary key. You cant remove/edit a primary key value if is already use as a foreign key. That is a CONSTRAINT FK VIOLATION, and the constraint function is precisely there to avoid any row become orphan by mistake and keep data integrity.

This need to be done in three steps:

  • Insert a new row with your new PK
  • Update all your FK related to old PK to your new PK
  • Delete old PK

Upvotes: 4

Related Questions