• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

updating primary key

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to update a column say(employee ID), in a table say (table-A) which is primary key...and it is also foreign key of (table-B) ..when i try to change the primary key(employee Id) in table A it throws error child records found in table -B...is there any way to update a primary key without deleting its child records?
 
Ranch Hand
Posts: 51
Hibernate Eclipse IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well one way is to insert a copy of the parrent record with he new primary key then update the child fkey with he new parents key , then delete the old parent.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasanna Kumaar wrote:i need to update a column say(employee ID), in a table say (table-A) which is primary key...and it is also foreign key of (table-B) ..when i try to change the primary key(employee Id) in table A it throws error child records found in table -B...is there any way to update a primary key without deleting its child records?



Gene is right. An update of a PK is not really a logical operation to try in a relational database. The PK identifies the row, change the identifier and you are (effectively) changing the thing it identifies. Most, if not all, databases will implement this as requiring an insert and delete, not an update.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One work around can be like this ....

Drop the foreign key constraint of (table-B) and after updating again add the foreign key constraint.

Regards,
Deo Swaroop
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DDL is not transactional. What happens if another thread inserts invalid data while the constraint was disabled? Gene's suggestion is a better bet.
 
Prasanna Kumaar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gene Hilpert wrote:Well one way is to insert a copy of the parrent record with he new primary key then update the child fkey with he new parents key , then delete the old parent.




Pardon me!!I don't get you,can you explain a bit clearly?
 
Gene Hilpert
Ranch Hand
Posts: 51
Hibernate Eclipse IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I have not been watching this forum.

1) first create a copy of the orginal parent record with a new Primary key.
2) change the FK on the child record to the primary key of the new parent record you just added.
3) delete the old parent record because you dont need it any more.
4) ask your DBA why he or she would ever use as a primary key data that might need to be changed. (well ... maybe you should not ask this question)
 
Prasanna Kumaar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gene Hilpert wrote:Sorry I have not been watching this forum.

1) first create a copy of the orginal parent record with a new Primary key.
2) change the FK on the child record to the primary key of the new parent record you just added.
3) delete the old parent record because you dont need it any more.
4) ask your DBA why he or she would ever use as a primary key data that might need to be changed. (well ... maybe you should not ask this question)




It worked ....Thanks
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic