How do you effectively model inheritance in a database?

后端 未结 9 1604
南旧
南旧 2020-11-22 05:37

What are the best practices for modeling inheritance in databases?

What are the trade-offs (e.g. queriability)?

(I\'m most interested in SQL Server and .NET,

9条回答
  •  深忆病人
    2020-11-22 06:19

    Using SQL ALchemy (Python ORM), you can do two types of inheritance.

    The one I've had experience is using a singe-table, and having a discriminant column. For instances, a Sheep database (no joke!) stored all Sheep in the one table, and Rams and Ewes were handled using a gender column in that table.

    Thus, you can query for all Sheep, and get all Sheep. Or you can query by Ram only, and it will only get Rams. You can also do things like have a relation that can only be a Ram (ie, the Sire of a Sheep), and so on.

提交回复
热议问题