Questions tagged [class-table-inheritance]

Class Table Inheritance is one of several techniques for designing SQL tables in situations where subclasses that extend classes would apply if SQL had a mechanism for inheritance, which it doesn't.

SQL as such has no formal mechanism for implementing inheritance on behalf of the database builder. However, there are ways to design tables that mimic, to some extent, the behavior you would get from subclasses that extend classes in an object oriented environment.

Class Table Inheritance is one such design technique. In this technique, there is one table for the class, and one table for each distinct subclass. Columns that are relevant to all members of the class go in the class table. Columns that are only relevant to some subclasses (often only one subclass) go in the appropriate subclass table(s). The concept can easily be extended to cases where the class is itself a subclass of some even more generic superclass.

The primary key of the class table and the primary key of the subclass tables are usually shared. This is described under the tag . Implementing shared primary key involves some work on the part of the programmer, because the primary key has to be propagated from the class table to the appropriate subclass table(s) by programmed action whenever new entries are made in the class table. Shared primary keys enforce the one-to-one nature of the relationship.

A join between one of the subclass tables and the class table is simple, easy, and fast. All rows in the class table that do not pertain to the subclass at hand will drop out of the join. For convenience, these joins might be kept in defined views.

There are circumstances where the subclasses are mutually exclusive. A pet is never both a dog and a cat. There are cases where the subclasses are not mutually exclusive. One person might be both a student and an instructor at a university.

A different but simpler technique for dealing with inheritance is described under the tag .

105 questions
2
votes
1 answer

Class table inheritance for shop products: How to place an order

I am trying to create a e-commerce/shop system, I've decided to choose the class table inheritance design in SQL design based on this great answer. Now I've got following table structure: product ( id [PK], name, description ) product_shirt ( id…
dan-lee
  • 14,365
  • 5
  • 52
  • 77
2
votes
1 answer

How to map collection of each subclass from same hierarchy onto one owning class?

Does anyone know how to map a collection of each subclass from the same hierarchy onto one owning class, using JPA 2.0 annotations, backed by Hibernate 4.0.0.Final? Bit convoluted, so here's an example. Hierarchy classes look like this: …
2
votes
3 answers

Discovering the subclass of a row in the superclass table

I have a table of electronic devices that serves as a generic superclass and then separate subclass tables for phones, laptops, cameras, etc. that extend the superclass and give specific information on the device (using the foreign key deviceID from…
JoeCool
  • 4,392
  • 11
  • 50
  • 66
1
vote
2 answers

How to prevent orphaned polymorphic records?

I have a database of polymorphic structure: a "base" type table and two "derived" types: CREATE TABLE ContactMethod( id integer PRIMARY KEY person_id integer priority integer allow_solicitation boolean FOREIGN KEY(person_id) REFERENCES…
1
vote
1 answer

Postgresql Simultaneous Insert into Parent/Child Table

Using Postgresql, I have tables with a parent/child or a general/detail relationship, a bit like Object-Oriented inheritance: CREATE TYPE person_kinds AS ENUM ('student', 'teacher'); CREATE TABLE person(id serial primary key, kind person_kinds,…
Christoph
  • 25
  • 4
1
vote
2 answers

SQL One-to-Many Table vs. multiple one-to-one relationships

I'm working on a project with the following objective: A User can create a Challenge and select an optional Rival to take part of this challenge. The Challenge generates Daily entries and will track stats on these. The basic User and Entry entities…
Cowlby
  • 651
  • 7
  • 16
1
vote
2 answers

How can I "split" a table using EF Core 6.0 to participate in two different relationships?

I need to add 'notes' to multiple entities in my EF Core 6 data model. Notes have exactly the same schema - a date/time, the name of the person who created the note, and a comment - so I'd like to use a single database table, with a discriminator…
Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
1
vote
1 answer

Finding All Child Entities using Query to Parent Entity in TypeORM

Consider a base entity as below: export abstract class Notification { @PrimaryGeneratedColumn() id: number; @Column({type: "date",nullable: false}) seenAt: Date; @Column({ type: "integer", nullable: false }) priority: number; } and…
dzdmmtf
  • 429
  • 3
  • 11
1
vote
1 answer

How to use "class table inheritance" in Doctrine 2 with XML mapping

I'm trying to use the "class table inheritance feature" of Doctrine 2 with XML Mapping (Symfony 2 PR 7). XML definition of the XML superclass CatalogProduct:
1
vote
2 answers

How to define the descriminator column as ENUM for Class Table Inheritance in Doctrine's?

I want to implement the Class Table Inheritance: /** * Foo * * @ORM\Table(name="foos", ...) * @ORM\Entity * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({ * "bar" = "Bar", …
automatix
  • 14,018
  • 26
  • 105
  • 230
1
vote
2 answers

Doctrine Class Table Inheritance can not remove() entity

I try to implement Doctrine's Class Table Inheritance. My Application needs a User Entity that is used to authenticate the user through Symfony's security system. On top of that, my application needs a special kind of User, a Doctor. Below you can…
1
vote
0 answers

Rails 4: How to implement class table inheritance?

How do I properly implement CTI in Rails 4? I tried by using dbview_cti but this gem creates a view with inherited attributes, not an inherited table. I would be grateful for an explanation and example. Most of the tutorials are based on STI.
Prezes Łukasz
  • 938
  • 1
  • 9
  • 30
1
vote
1 answer

Working with DBs, SQL: table "inheritance" separation decision

Let's say I have a database in which one entity (i.e. table) inherits from another one, for example: Table 1, named person: (name,surname) Table 2, named car_owner In this case, car_owner inherits from person, i.e. a car-owner IS a person. I'm now…
1
vote
2 answers

Database model with 3 kind of users

Im thinking about what is the better way to design a database with the following statements: There are 3 kinds of users (3 differents roles). They have some common fields, however they have differents fields too. Solution A: All user in the same…
1
vote
0 answers

Does it make sense to use a class table inheritance schema if there are no shared columns?

I'm considering creating a class table inheritance schema as described here: http://www.sqlteam.com/article/implementing-table-inheritance-in-sql-server However my reason for wanting this seems different than what most people are after. Most people…