views:

289

answers:

4

Hello everyone,

I am confused about what means the update and delete rule in SQL Server 2008 Management Studio when we define foreign key constraints. I also did not find related help documents (e.g. F1 help).

Here is the screen snapshot. Appreciate if anyone could describe what do they mean and recommend some related documents to read. :-)

http://i31.tinypic.com/jt8apf.jpg

thanks in advance, George

+1  A: 

A foreign key field can only store null or a value defined by the primary key field.

If you try to change the foreign key value to something not defined by a primary key you will get an error. Likewise if you try to change a primary key which has foreign key dependencies you will get an error... as an example

Models table
modelID (primary key)   model
1                       Jeep   
2                       Ford

Customer table
id    customer   modelID (foreign key of Models.modelID)
1     1234       1
2     2345       2

If you try to delete the Jeep record from Models you will get an error because customer 1234 has a modelID set to 1, and this if the foreign key. Likewise if I try to update customer 1234 to have a modelID of 3 it will throw an error because there is no primary key in the Models table having a value of 3

Check this out

Chris Klepeis
Thanks Chris, but the link you recommended does not mention what means update and delete rule in SMSS? It talks about general forrign key theory.
George2
Chris Klepeis
Can we print Google book content? I tried hard but can not print. :-)
George2
Unfortunately I dont believe so... since its just a sample from a book you need to pay for :(Try the MSDN link posted by Aakash
Chris Klepeis
Thanks Chris, I am learning from this MSDN page, http://msdn.microsoft.com/en-us/library/ms186973%28SQL.90%29.aspx One confusion, it is mentioned "If cascading referential actions have also been defined on the target tables ...", my confusion is seems beyond the four actions (NO ACTION | CASCADE | SET NULL | SET DEFAULT), we could define some other actions as well?
George2
+2  A: 

The MSDN page looks like a good start.

AakashM
Thanks AakashM, I found this page is very helpful. One confusion, it is mentioned "If cascading referential actions have also been defined on the target tables ...", my confusion is seems beyond the four actions (NO ACTION | CASCADE | SET NULL | SET DEFAULT), we could define some other actions as well?
George2
I don't believe so - and I would guess that one of those actions, rather than any more complex behaviour, would generally be what we actually wanted to do.
AakashM
+3  A: 

The foreign key defines a parent - child relationship between two tables. The primary key in the parent table is the foreign key in the up to n child table rows.

Now if that primary key in the parent table gets UPDATE, the UPDATE RULE kicks in. Either all the child rows are also updated, set to NULL or whatever. Best practice however is to have a primary key that NEVER changes (a fixed ID or something), so that's the less important rule.

The more important one is the DELETE rule - what if the parent row is deleted (e.g. the Order is deleted)? You can either also delete all child rows (all the Order line items) with CASCADE DELETE, or you can set their foreign key to NULL (they don't have a parent anymore) - that's totally up to your concrete scenario.

In the Order/order lines scenario, it might be totally useful to delete the order lines when the complete order gets deleted, but you probably don't want to delete a product, just because an order that references it has been deleted - there's no one single CORRECT answer - it depends on your scenario and your app.

Marc

marc_s
Thanks Marc, I am learning from this MSDN page, http://msdn.microsoft.com/en-us/library/ms186973%28SQL.90%29.aspx One confusion, it is mentioned "If cascading referential actions have also been defined on the target tables ...", my confusion is seems beyond the four actions (NO ACTION | CASCADE | SET NULL | SET DEFAULT), we could define some other actions as well?
George2
No, you can define one of those four actions for the UPDATE and the DELETE scenario. What the article talks about is the scenario where TableB references TableA and has a "ON DELETE CASCADE", so if a row from TableA gets deleted, all children in TableB will be deleted as well. If in this case, there's a TableC referencing TableB with the ON DELETE CASCADE action, then this deletion in TableA goes to TableB and then on to TableC.
marc_s
Thanks Marc, I like your answer!
George2
+2  A: 

It looks like the documentation is at Foreign Key Relationships Dialog Box.

BTW, F1 help worked fine for me in SSMS 2008. It took me right to the above page (after I searched for 1/2 hour online, of course).

John Saunders
Thanks John, unfortunately, F1 help does not help for me. :-(
George2
I right-clicked a table and chose "Design". I then right-clicked the designer and chose "Relations". I then pressed F1 and got help. What version of SQL Server are you using?
John Saunders