Data modeling for Same tables with same columns

前端 未结 2 1640
星月不相逢
星月不相逢 2020-12-21 17:04

I have many tables that have same number of columns and names because they are all lookup tables. For example, there are LabelType and TaskType tables. LabelType and TaskTyp

相关标签:
2条回答
  • 2020-12-21 17:41

    Just because data has similar structure doesn't mean it has the same meaning or same constraints. Keep your lookup tables separate. This keeps foreign keys separate, so the database can protect itself from referencing the wrong kind of lookup data.1

    I wish relational DBMSes supported inheritance, where you could define the basic structure in the parent table and just add specific FKs in the child tables. As it stands now, you'll need to endure some repetition in your DDL...

    NOTE: One exception from "keep lookup tables separate" rule might be when your system needs to be dynamic (i.e. be able to add new kinds of lookup data without actually creating new physical tables in the database), but it doesn't look that way from your question.


    1 With one big lookup table, FKs alone won't stop (for example) the ShippingLog table from referencing a row meant for the EmployeeTask table. By using identifying relationships and migrating PKs, you can protect yourself from this, but not without introducing some redundancies and needing some careful constraining. It's cleaner and probably more performant to simply do the right thing and keep lookup tables separate.

    0 讨论(0)
  • 2020-12-21 17:55

    Keep your lookup tables separate. It's faster at lookup time, and you will do millions of lookups between times when you add a new lookup table.

    A lot of tables is not a big problem.

    0 讨论(0)
提交回复
热议问题