Click to See Complete Forum and Search --> : constraint and relations


cgtalk
September 5th, 2008, 06:26 AM
hi
i was trying to understand something in database design from many decades ( ohh ****.. ami i that old :) )

i was playing with sql server 2005, i have created twp tables. i used the diagram to create a FK relation between two columns
table 1 ---> phoneID
table 2 ---> phoneID

what i understood from the books is that if you create a FK relation between one column and another in two different tables and you update or insert a new value in the primary column , the other column should get the data immediatly.

assume this
table 1 column phoneID has a new value of 3 inserted incremently by insering a data in another column using c# code
now table 2 should insert this ID also but not using a code, it should insert it automatically becouse of the relation that is between the two columns


am i wrong?

Alsvha
September 5th, 2008, 09:26 AM
Partly yes, but not entirely - if I read your question correct.
Basically..
Cascading when updating, means that when you update a value in table1, if it is present in table2 it will be updated.
Cascading when deleting, means if you delete from table1, it will delete relevant rows from table2.

Enforcing a FK restraint means that you can't insert values in table2's FK column which doesn't exists in table1 PK.
But it doesn't mean it automatically will create an entry in table2 if you enter something in table1.

Hope it helps.

cgtalk
September 5th, 2008, 02:24 PM
thanks for replying

so you mean there is no way to do a automatic insertion, just cascade delete and update?

Alsvha
September 6th, 2008, 04:14 AM
If you utilize triggers you can.
I generally however recommend not using triggers much as they can quickly form a "black magic box" type behaviour in a database if not documented and controlled fiercely :D
They are also annoying to debug if something goes wrong.

But triggers can "automatically" (you have to make the functionality yourself) insert content into other tables based on input into a table.