Foreign Keys, Part 1 - SQL School Video

  • Andy Warren

    SSC Guru

    Points: 119676

    Comments posted to this topic are about the item Foreign Keys, Part 1 - SQL School Video

  • Half Bubble

    SSCommitted

    Points: 1948

    Do you then build in index on the "foreign key" column, or does the foreign key constraint take care of that?

    [font="Arial"]Halfbubble ------------
    You just can't do this stuff unless you're at least half a bubble off center.[/font]

  • bkirk

    SSCrazy

    Points: 2824

    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

    SSCommitted

    Points: 1948

    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.

    [font="Arial"]Halfbubble ------------
    You just can't do this stuff unless you're at least half a bubble off center.[/font]

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply