Single Table Inheritance
Single table inheritance is a way to emulate object-oriented inheritance in a relational database. When mapping from a database table to an object in an object-oriented language, a field in the database identifies what class in the hierarchy the object belongs to.{{cite book |last=Fowler |first=Martin |authorlink=Martin Fowler (software engineer) |others=Contributions by Dave Rice, Matthew Foemmel, Edward Hieatt, Robert Mee, and Randy Stafford |date=2003 |title=Patterns of Enterprise Application Architecture |series=The Addison-Wesley Signature Series |publisher=Addison-Wesley |isbn=0-321-12742-0 |page=278}} All fields of all the classes are stored in the same table, hence the name "Single Table Inheritance". In Ruby on Rails the field in the table called 'type' identifies the name of the class. In Hibernate (Java) and Entity Framework this pattern is called Table-Per-Class-Hierarchy and Table-Per-Hierarchy (TPH) respectively.,{{cite web |date=January 21, 2019 |title=Tutorial: Implement Inheritance with EF in an ASP.NET MVC 5 app |url=http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application |accessdate=November 3, 2015}}{{cite web |last1=King |first1=Gavin |last2=Bauer |first2=Christian |last3=Andersen |first3=Max Rydahl |last4=Bernard |first4=Emmanuel |last5=Ebersole |first5=Steve |others=Graphics design by James Cobb and Cheyenne Weaver |date=September 15, 2010 |title=Chapter 9. Inheritance mapping |edition=Version 3.5.6-Final |work=HIBERNATE - Relational Persistence for Idiomatic Java |url=https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/inheritance.html |accessdate=November 3, 2015}} and the column containing the class name is called the Discriminator column.
Example
class="wikitable"
|+ Blogs | |||
BlogId | Discriminator | Url | RssUrl |
---|---|---|---|
1 | Blog | {{Null result}} | |
2 | RssBlog |
The table have the Url
which is used by all blogs but only blogs of type RssBlog have a value assigned in the RssUrl
column, other rows have NULL
.
See also
References
{{Reflist}}
External links
- [http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html Single Table Inheritance]
- [http://www.yiiframework.com/wiki/198/single-table-inheritance/ Single Table Inheritance in Yii]
- [https://github.com/craigds/django-typed-models Single Table Inheritance in Django]
{{software-eng-stub}}