Forum Moderators: coopster

Message Too Old, No Replies

OOP question

Inheritance and database tables

         

surfgatinho

11:27 am on Dec 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I’m using inheritance for several classes from a parent that has variables mapped to a database.
For example the main class is Listing and the child classes include Restaurant, Shop and Hotel.

What I’m wondering is what is the best practice for setting the tables up. Do I just extend the listing table to include every field for every child class or do I set up a new table for every child class?

Thanks in advance

eelixduppy

5:46 pm on Dec 19, 2007 (gmt 0)



Seems more logical to have a separate table for each child class. Data will be much more organized IMO.

mikesmith76

9:23 pm on Dec 19, 2007 (gmt 0)

10+ Year Member



I would say it depends how much data the 3 classes share. If they differ significantly (which i'm guessing so with three separate classes) then yeah 3 separate tables.

lammert

12:51 am on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In my opinion it depends if you want to list items twice or not. I can imagine that restaurants and hotels are quite often one company with one address and you have to define for yourself if it is easier to maintain one record which contains all shared fields ones, and additional fields for each class, or that is better to have duplicate information in two different tables. The best solution depends on how you want to maintain and use the data.

eelixduppy

1:02 am on Dec 20, 2007 (gmt 0)



>> I can imagine that restaurants and hotels are quite often one company with one address

Then there should be a separate table just for the parent class that contains the info for that class. 'address' should be inherited from the parent class if it's something that will be defined for each instance. There shouldn't be much duplication in the tables. If something can be part of any instance of a child class, it should be defined in the parent class and inherited.