Click to See Complete Forum and Search --> : foreign key and primary key


dhawanmansi
03-27-2010, 02:29 PM
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

PAVB
03-27-2010, 07:04 PM
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

dhawanmansi
03-28-2010, 10:39 AM
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:confused:

PAVB
03-28-2010, 01:11 PM
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.

dhawanmansi
03-28-2010, 01:30 PM
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 ?????

PAVB
03-28-2010, 04:22 PM
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 :D

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.