I'm designing my first relational database, and it's only a few tables, but I'm curious what the rules regarding foreign keys are.
In this specific case I have a have a table for a comment and a table for a reply to that comment. Should the foreign key be in the comment table, pointing to the reply id, or should the foreign key be in the reply table, pointing to the comment id?
To me it seems like there's no real difference, but I was wondering if there's a convention that specifies how it should be done.
The easy answer is the answer to the question "what depends on what"? In your case, does there need to be a comment before there is a reply? Is there functional dependency? If yes, then the "parent" is the comment table, and the "child" is the reply table. So then you'd want to put a FK on the reply table pointing to the comment table.
So you are enforcing that there is a comment that always exists that the reply points to. If that lines up with the reality of the system, then the FK makes sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With