- I do not want the orders to be deleted when a customer is deleted. (On Delete Cascade)
- I use identity columns so I do not need On Update Cascade
- It should be possible to delete a customer table although there exist orders pointing/referencing to a customer. I do not care when the customer is gone because I still need the order table for other tables.
Does a foreign key make sense in this scenario when I do not use Referential Integrity with On Delete/Update Cascade ?
Yes. The foreign key is not in place only to clean up after yourself but primarily to make sure the data is right in the first place (it can also assist the optimizer in some cases). I use foreign keys all over the place but I have yet to find a need to implement on cascade actions. I do understand the purpose of cascade but I’ve always found it better to control those processes myself.
EDIT even though I tried to explain already that you can work around the cascade issue (thus still satisfying your third condition), I thought I would add an illustration:
You can certainly still allow for orders to remain after you’ve deleted a customer. The key is to make the
Orders.CustomerIDcolumn nullable, e.g.Now when you want to delete a customer, assuming you control these operations via a stored procedure, you can do it this way, first setting their
Orders.CustomerIDtoNULL:If you can’t control ad hoc deletes from the Customers table, then you can still achieve this with an instead of trigger:
That all said, I’m not sure I understand the purpose of deleting a customer and keeping their orders (or any indication at all about who placed that order).