This is a simplified example of a design issue that I am facing: I have 3 tables: Car
, Ship
and Bicycle
. I need to add an "activity logging" table that records user actions such as deletion and user comments etc. I was thinking of creating just one table. Rather than 3 tables. The problem is with ensuring referential integrity. Should I create 3 separate columns that link to these tables? Should there be one column and I use it when needed? What is the general recommendation in such case? or should I just create 3 separate tables?
Referential Integrity – Re-using table
database-designreferential-integrity
Related Question
- PostgreSQL – Correct Way to Map Relationships Between Similar Comments Tables
- Database Design – Is Marking a Row as Removed or Deleted a Good Practice?
- SQL Server – Referential Integrity When Two FK Columns Need to Match in Same Table
- Database Design – Representing Ownership of Hierarchical Tree Structure
- Do any of the graph based/aware databases have good mechanisms for maintaining referential integrity
Best Answer
I work with a database that has this solution that I outline below. I don't like it because the base table has an entry for each type and it ends up being a huge table that is slow to query.
--base table
--table for "Car" logging
--another child table
to get all the ship logs you would query for
this is a clean design but if each type of thing generates ten or twenty log entries per thing you don't have to do too much to have a huge table for application_logging with a million entries. Users complain it's slow to see the activity log.
The real question is:
As @Steb says "it all depends". Your application, your users, number of transactions....