Search code examples
sqldatabase-designrelational-databaseclass-table-inheritance

understanding class table inheritance


I have a product table called products It has 3 fields (name,model(PK), and class_name). The class corresponds to a table. So here is an example:

Product table:

model | name | class_Name
z123  | Abcd | AMPS

AMPS table:

model | attribute_1 | attribute_2
z123  | blah blah   | blah blah

Question:

Should I have a table that holds the PK (model) and its corresponding class name, and then use the class ID in my product table? Would it be more efficient to have a table that holds all of the model's and their classes?


Solution

  • This looks like a "subclass" (aka. "category") hierarchy. If you have and always will have only AMPS and no other "child class", then you may consider merging it with Product. Otherwise, it looks good.

    BTW, there are 3 ways to implement class hierarchies in relational databases, each with strengths and weaknesses. Keeping all classes in a single table is one of them, but can be hard to maintain and can be problematic for certain kinds of referential integrity. The model that you are already using ("class per table") should probably be your default choice unless there is a compelling reason otherwise...