Rachel
Rachel

Reputation: 103397

How to map OO inheritence to a relational database scheme?

I have a canonical data model. Some fields are related to the pricing of a product. It inherits certain attributes from the product table. I want to model this inheritance in a MySQL relational database. How can we model object-oriented inheritance in a relational database scheme?

Upvotes: 48

Views: 52590

Answers (4)

RichardOD
RichardOD

Reputation: 29157

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.

Upvotes: 49

Walter Mitty
Walter Mitty

Reputation: 18940

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.

Upvotes: 6

Erwin Smout
Erwin Smout

Reputation: 18408

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.

Upvotes: 1

ChssPly76
ChssPly76

Reputation: 100686

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.

Upvotes: 4

Related Questions