Question

I would like to ask about how to handle this relationship. For better view I've tried to simplify and convert my situation into how I think StackOverflow question voting has (sorry if not :-)

enter image description here

I would like to add a constraint with CASCADE DELETE rule however I don't know if it's possible for this case. Another option is to create an ON DELETE trigger, so...

My question is:
What would you prefer, CASCADE DELETE constraint rule (if it's possible here) or ON DELETE trigger ?
Or would you create a DB model for this simple case (imagine StackOverflow's voting scheme) different way ?

Why am I asking:
I've read somewhere that CASCADE DELETE constraint rules are internally nothing else than triggers and that some people here don't prefer the constraint rules at all. I know, quite vague question, but I would like to have my DB clean from a not existing relationship records.

The important thing:
I'm creating the application which should be able to run on the SQL Server as well as on MySQL database engines, but both should support all, the CASCADE DELETE constraint rules as well as ON DELETE triggers, I guess.

Thanks!

Was it helpful?

Solution

I don't think you will see a performance difference between trigger and using cascade. I would prefer ON CASCADE DELETE just because in my opinion it better describes model and reveals creator's intention. Also, if you later use one or another ORM, it will be able to build relationship between entities (surely, I'm talking about 'Data First' approach)if it's implemented through foreign key constraints, but not through the triggers.

OTHER TIPS

The OP already mentioned this in a comment, but it is important enough to bring out in answer to the question.

In MySQL ON DELETE CASCADE will not activate any triggers on the child rows that were deleted in this manner. So, if you have any triggers on the child table, you will probably be better off using after delete triggers on the parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top