I’m currently building a database and I’m not sure when to use foreign keys on the tables.
Most of the tables are for transaction, logging, and history.
Is there a general rule in which tables to put the foreign keys?
I have this table for the general details of a specific transaction, then another table for the specific details. Should I put a foreign key on the table for the specific details?
There’s also this table used for storing user info and another for logging user activities. Should I also place a foreign key to the table that logs user activities?
If there is a relation between two tables you should always enforce that using a foreign key constraint.
I prevents logically “corrupted” data (e.g. details for a transaction that doesn’t actually exist) and – equally important – it documents your database model.