database design - When/Why to use Cascading in SQL Server? -
when setting foreign keys in sql server, under circumstances should have cascade on delete or update, , reasoning behind it?
this applies other databases well.
i'm looking of concrete examples of each scenario, preferably has used them successfully.
summary of i've seen far:
- some people don't cascading @ all.
cascade delete
- cascade delete may make sense when semantics of relationship can involve exclusive "is part of" description. example, orderline record part of parent order, , orderlines never shared between multiple orders. if order vanish, orderline should well, , line without order problem.
- the canonical example cascade delete someobject , someobjectitems, doesn't make sense items record ever exist without corresponding main record.
- you should not use cascade delete if preserving history or using "soft/logical delete" set deleted bit column 1/true.
cascade update
- cascade update may make sense when use real key rather surrogate key (identity/autoincrement column) across tables.
- the canonical example cascade update when have mutable foreign key, username can changed.
- you should not use cascade update keys identity/autoincrement columns.
- cascade update best used in conjunction unique constraint.
when use cascading
- you may want strong confirmation user before allowing operation cascade, depends on application.
- cascading can trouble if set foreign keys wrong. should okay if right.
- it's not wise use cascading before understand thoroughly. however, useful feature , therefore worth taking time understand.
Comments
Post a Comment