45

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 inheritance in MySQL relational database and so,

"How can we do Inheritance Modeling in Relational Databases ?"

Thanks.

5 Answers 5

46

Martin Fowler discusses this extensively in his book Patterns of Enterprise Application Architecture book. Get this book and look into:

  1. Single Table Inheritance

Blockquote

  1. Class Table Inheritance

enter image description here

  1. Concrete Table Inheritance

Blockquote

The Website should give you some idea. You might also want to read the section on inheritance mappers. Each of the different approaches have their pros and cons so choose wisely.

2
  • 46
    This answer involves buying a book to be completely realized. Any chance you could add similar web resources?
    – David Reis
    Oct 27, 2010 at 23:11
  • While this answer points the OP in the right direction regarding Generalization/Specialization, it does not answer does now answer OP's question on how to model it in MySQL. Martin Fowler doesn't actually explain how to write SQL for developing these models.
    – ike5
    Feb 26, 2022 at 19:06
8

Look at these articles for some ideas:

Mapping Objects to Relational Databases

Inheritance in O/R Mapping

6

If you just want to look at some web articles instead of reading a book, you can find some good articles by Googling on:

Generalization Specialization Relational Modeling

The gen-spec pattern covers a lot of the same ground that inheritance does in OOP environments.

If you google on

Generalization Specialization Object Modeling

you'll get a whole new batch of articles, most of which mention inheritance explicitly.

There is a design technique that's summarized in the following tag under the info tag. This allows you to use subclass tables to "extend" a class table, if you'll allow a strange use of the word "extend". There's some work involved, but it's worth it.

1
  • 1
    While this isn't really an answer in an idealized S.O. sense, I found it very useful.
    – derekv
    Feb 9, 2018 at 21:54
5

Relational databases don't deal with objects (and, thus, inheritance) - they deal with relationships. What you're really asking is how to map your object structure to your database - and the answer to that is "it depends on your ORM layer".

Take a look at Mapping Objects to Relational Databases: O/R Mapping In Detail article for some details. If you tell us what software stack you're using, you'll likely get a more to-the-point answer.

3
  • 3
    This may sound pedantic but I don't think it's accurate to say that relational databases deals with relationships, but rather they are called such because they deal with relations as defined here: en.wikipedia.org/wiki/Relation_(database) Oct 14, 2009 at 18:17
  • 1
    @Ralph: Wiki is an abscess; the confabulators have no certification. Relational Databases deal with Entities and Relations, not objects. Objects can be mapped easily if the RDb is truly Relational; and not if it is not. Nov 11, 2010 at 4:06
  • 3
    You can have table inheritance without an ORM Dec 23, 2013 at 20:19
1

Chapter 6 of "Practical Issues in Database Management" is probably an interesting read for you.

As are all the other chapters, probably, but those don't relate directly to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.