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
318
votes
7 answers

How can you represent inheritance in a database?

I'm thinking about how to represent a complex structure in a SQL Server database. Consider an application that needs to store details of a family of objects, which share some attributes, but have many others not common. For example, a commercial…
111
votes
5 answers

Cannot use identity column key generation with ( TABLE_PER_CLASS )

com.something.SuperClass: @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class SuperClass implements Serializable { private static final long serialVersionUID = -695503064509648117L; long…
user14070
45
votes
5 answers

How to do Inheritance Modeling in Relational Databases?

My question is regarding Inheritance modeling in Relational Database Systems. I have canonical data model and in that I have some fields related to pricing of product inheriting certain attributes from product table and I want to model this…
Rachel
  • 100,387
  • 116
  • 269
  • 365
40
votes
8 answers

How do we implement an IS-A Relationship?

We implement an One-to-Many relationship by adding one Table's PK, as FK to the other Table. We implement a Many-to-Many relationship by adding 2 Table's PKs to a third Table. How do we implement an IS-A Relationship ? The Entities are TECHNICIAN…
athspk
  • 6,722
  • 7
  • 37
  • 51
33
votes
3 answers

Techniques for database inheritance?

What are the tips/techniques when you need to persist classes with inheritance to relational database that doesn't support inheritance? Say I have this classic example: Person -> Employee -> Manager -> Team lead …
chakrit
  • 61,017
  • 25
  • 133
  • 162
24
votes
6 answers

Something like inheritance in database design

Suppose you were setting up a database to store crash test data of various vehicles. You want to store data of crash tests for speedboats, cars, and go-karts. You could create three separate tables: SpeedboatTests, CarTests, and GokartTests. But a…
15
votes
2 answers

PHP doctrine 1.2 ORM - polymorphic queries with class table inheritance

I'm experimenting with the Doctrine ORM (v1.2) for PHP. I have defined a class "liquor", with two child classes "gin" and "whiskey". I am using concrete inheritance (class table inheritance in most literature) to map the classes to three seperate…
Josh Johnson
  • 8,832
  • 4
  • 25
  • 31
14
votes
2 answers

How do I implement class-table-inheritance in Rails?

I just finished working my way through the book Agile Web Development with Rails, fourth edition (http://pragprog.com/book/rails4/agile-web-development-with-rails), and need some help in understanding how to create a parent-child relationship…
user1002119
  • 3,692
  • 4
  • 27
  • 30
13
votes
4 answers

Class Table Inheritance in Rails 3

I'm currently working on a Rails 3 application that looks like it might need to use Class Table Inheritance for a couple of models. A simplified example of what's going on is this. I have a class called Person with general attributes like name,…
Ganesh Shankar
  • 4,826
  • 8
  • 43
  • 56
12
votes
5 answers

How to change and entity type in Doctrine2 CTI Inheritance

How (if possible at all) do you change the entity type with Doctrine2, using it's Class Table Inheritance? Let's say I have a Person parent class type and two inherited types Employe and Client. My system allows to create a Person and specify it's…
Jimmy
  • 928
  • 2
  • 9
  • 17
11
votes
3 answers

How to implement a super class, sub class relationship in the database?

If I have a class called animal, dog and fish is the subclass. The animal have attribute called "color". Dog have the attribute called "tail length", and the fish don't have this attribute. Fish have the attribute called "weight", the dog don't…
Tattat
  • 15,548
  • 33
  • 87
  • 138
11
votes
3 answers

Doctrine2: OneToMany on mapped superclass

My DB structure is as follows: work: CTI table Work MappedSuperclass table AbstractImageWork which extends Work final table PhotoWork which extends AbstractImageWork comment: MappedSuperclass table Comment final table WorkComment which extends…
10
votes
1 answer

Symfony2: Adding a collection based on a table-inheritance structure to a FormView

I am working on a Symfony2/Doctrine app which uses class-table-inheritance (http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#class-table-inheritance) to manage Complaints in a Consult. Each Consult can have many…
8
votes
2 answers

Avoid Circular Dependency

I am developing a travel management application. The design in question is something like following : Each person in a tour is designated as a Traveler. Each Traveler has a Passport. Now, a Traveler can be a MainMember or a SubMember, depending on…
7
votes
1 answer

Inherited Entity with self-referencing ManyToMany: EXTRA_LAZY fetch mode not working

I have to following setup: A parent class /** * @ORM\Entity() * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discr", type="string") */ abstract class DataCategory { /** * @ORM\Column(name="id", type="integer") *…
olidem
  • 1,961
  • 2
  • 20
  • 45
1
2 3 4 5 6 7