Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Foreign Keys, Part 1 - SQL School Video


Foreign Keys, Part 1 - SQL School Video

Author
Message
Andy Warren
Andy Warren
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: Moderators
Points: 9511 Visits: 2728
Comments posted to this topic are about the item Foreign Keys, Part 1 - SQL School Video

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Half Bubble
Half Bubble
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 168
Do you then build in index on the "foreign key" column, or does the foreign key constraint take care of that?

Halfbubble ------------
You just can't do this stuff unless you're at least half a bubble off center.

bkirk
bkirk
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 346
Nice Video and a good intro to foreign keys.

My interest in FK's are more specifically related to the deletion of data between joined tables.

Eg.

Table 1 - Orders
Table 2 - OrdersDetails

(FK most likely would be the OrderID i assume?)

A new follow up video would be very helpful to show how the deletion of status changes of this type of data would work?

If you deleted an Order from the Orders primary table, do FK's automatically delete the corresponding rows of data? Or does an error occur and stop the deletion of the primary record from the Orders table?

I guess i am looking for the best method to adopt when updating data between multiples tables.
Half Bubble
Half Bubble
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 168
For that, I make the OrderID an internal column the users never see. This way, they'll never update it. Then, for the foreign key, specify Enforce Foreign Key Constraints and Cascade for the delete rule.

This way, the database will forbid an Order Detail that's not connected to an Order, and if an order is deleted, the details will go away too.

Sometimes, you want to restrict a delete, like, don't delete a customer that has orders, but the foreign key constraint doesn't do that for you.

Halfbubble ------------
You just can't do this stuff unless you're at least half a bubble off center.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search