DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: foreign key and primary key

  1. #1
    Join Date
    Mar 2010
    Posts
    3

    foreign key and primary key

    Hi,
    I have a table DEPTT and STAFF .In DEPTT there is a primary key D_ID which is foreign key in STAFF.Changes made in DEPTT table for D_ID results in
    ."violated - child record" error.This is working correctly.However
    when I change D_ID in STAFF the record gets updated and no error is thrown.
    My understanding is that it should reflect back in the main parent table (DEPTT) also.

    Please let me know if I am thinking right.
    Thanks in advance,
    dhawanmansi

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by dhawanmansi View Post
    My understanding is that it should reflect back in the main parent table.
    Wrong assumption - doesn't work that way.

    The only reason you are not getting an error when updating STAFF.D_ID is because the new value already exists in DEPTT.D_ID
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Mar 2010
    Posts
    3
    thanx 4 your rply.
    But i am still not able to understand that according to the definition of a database, if one change is made in some table then that change must be reflected back in other table as well in order to maintain data consistency.
    But if i am making any changes in the DEPTT table then why those changes are not made in the staff table. In fact, it is giving an error even when i am making changes in the parent(deptt) table and not allowing me to change the values in my table

  4. #4
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by dhawanmansi View Post
    But i am still not able to understand that according to the definition of a database, if one change is made in some table then that change must be reflected back in other table as well in order to maintain data consistency.
    Oracle takes care of protecting data integrity by returning and error when issued DML is bound to violate defined referential integrity.

    If you want to change data you have to do it - Oracle does not do it for you.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  5. #5
    Join Date
    Mar 2010
    Posts
    3
    But even when i m trying to change my data in every table where it is required, it gives an error. So how can i change my data ?????

  6. #6
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by dhawanmansi View Post
    But even when i m trying to change my data in every table where it is required, it gives an error. So how can i change my data ?????
    That's the beauty of referential integrity. Oracle will protect your data even from you

    Here is a way...
    1- Insert new row in parent table.
    2- Update row in child table.
    3- Delete old row in parent table - providing it has no other childs and you don't need it anymore.

    Please do it in a single transaction.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width