Two tables with same columns or one table with additional column?

前端 未结 3 874
清歌不尽
清歌不尽 2021-01-12 04:12

Say I have two tables (Apples and Oranges) with the same columns and just a different table name. Would there be any advantages/disadvantages to turning this into one table

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 05:07

    Depends on constraints:

    • Do you have foreign keys or CHECKs on apples that don't exist on oranges (or vice-versa)?
    • Do you need to keep keys unique across both tables (so no apple can have the same ID as some orange)?

    If the answers on these two questions are: "yes" and "no", keep the tables separate (so constraints can be made table-specific1).

    If the answers are: "no" and "yes", merge them together (so you can crate a key that spans both).

    If the answers are: "yes" and "yes", consider emulating inheritance2:

    enter image description here


    1 Lookup data is a typical example of tables that look similar, yet must be kept separate so FKs can be kept separate.

    2 Specifically, this is the "all classes in separate tables" strategy for representing inheritance (aka. category, subclassing, subtyping, generalization hierarchy etc.). You might want to take a look at this post for more info.

提交回复
热议问题