# Single Table Inheritance

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Single_Table_Inheritance
> Markdown URL: https://mediated.wiki/source/Single_Table_Inheritance.md
> Source: https://en.wikipedia.org/wiki/Single_Table_Inheritance
> Source revision: 1303149147
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

**Single table inheritance** is a way to emulate [object-oriented](/source/Object-oriented_programming) [inheritance](/source/Inheritance_(object-oriented_programming)) in a [relational database](/source/Relational_database). When [mapping](/source/Map_(mathematics)) from a [database](/source/Database) table to an object in an [object-oriented language](/source/Object-oriented_language), a field in the database identifies what class in the [hierarchy](/source/Hierarchy) the object belongs to.[1] All fields of all the classes are stored in the same table, hence the name "Single Table Inheritance". In [Ruby on Rails](/source/Ruby_on_Rails) the field in the table called 'type' identifies the name of the [class](/source/Class_(computer_science)). In [Hibernate (Java)](/source/Hibernate_(Java)) and [Entity Framework](/source/Entity_Framework) this pattern is called Table-Per-Class-Hierarchy and Table-Per-Hierarchy (TPH) respectively.,[2][3] and the column containing the class name is called the Discriminator column.

## Example

Blogs BlogId Discriminator Url RssUrl 1 Blog https://blogs.example.com/pets NULL 2 RssBlog https://blogs.example.com/cars https://blogs.example.com/cars.rss

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

- [Object–relational mapping](/source/Object%E2%80%93relational_mapping)

- [ActiveRecord (Rails)](/source/ActiveRecord_(Rails))

## References

1. **[^](#cite_ref-1)** [Fowler, Martin](/source/Martin_Fowler_(software_engineer)) (2003). *Patterns of Enterprise Application Architecture*. The Addison-Wesley Signature Series. Contributions by Dave Rice, Matthew Foemmel, Edward Hieatt, Robert Mee, and Randy Stafford. Addison-Wesley. p. 278. [ISBN](/source/ISBN_(identifier)) [0-321-12742-0](https://en.wikipedia.org/wiki/Special:BookSources/0-321-12742-0).

1. **[^](#cite_ref-2)** ["Tutorial: Implement Inheritance with EF in an ASP.NET MVC 5 app"](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). January 21, 2019. Retrieved November 3, 2015.

1. **[^](#cite_ref-3)** King, Gavin; Bauer, Christian; Andersen, Max Rydahl; Bernard, Emmanuel; Ebersole, Steve (September 15, 2010). ["Chapter 9. Inheritance mapping"](https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/inheritance.html). *HIBERNATE - Relational Persistence for Idiomatic Java*. Graphics design by James Cobb and Cheyenne Weaver (Version 3.5.6-Final ed.). Retrieved November 3, 2015.

## External links

- [Single Table Inheritance](http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html)

- [Single Table Inheritance in Yii](http://www.yiiframework.com/wiki/198/single-table-inheritance/)

- [Single Table Inheritance in Django](https://github.com/craigds/django-typed-models)

This software-engineering-related article is a stub. You can help Wikipedia by adding missing information.

- [v](https://en.wikipedia.org/wiki/Template:Software-eng-stub)
- [t](/source/Template_talk%3ASoftware-eng-stub)
- [e](https://en.wikipedia.org/wiki/Special:EditPage/Template:Software-eng-stub)

---
Adapted from the Wikipedia article [Single Table Inheritance](https://en.wikipedia.org/wiki/Single_Table_Inheritance) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Single_Table_Inheritance?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
