0

Is it bad to implement supertype and subtype to the entire data in a database? I need some advice on this before moving to this direction...

For instance,

I have these tables as objects and they are related,

users
pages
images

entities table as the supertype

entity_id     entity_type
1             page
2             page
3             user
4             user
5             image
6             image

users table

user_id     entity_id
1           3
2           4

pages table

page_id     entity_id
1           1
2           2

images table

image_id     entity_id
1            5
2            6

here is the table to map images table with entities table because some images belong to certain page (maybe to blog posts, etc in the future),

map_entity_image table

entity_id    image_id
1            1
1            2

so, I will insert a row into the entities table when I have a page, an image, an user, etc to be created.

in the end of the day the rows in this tables will increase in a great numbers. so my worry is that can it cop with large numbers of rows? will this database gets slow and slower in time?

after all, are these a bad structure?

or maybe I am doing supertype/ subtype incorrectly?

edit:

I think the entity should have these data only,

entity_id     entity_type
1             page
2             page

unless I want to attach images to users, etc. then it should be like this,

entity_id     entity_type
1             page
2             page
3             user
4             user

maybe I am wrong...

EDIT:

so this is the query how I find out how many images attached to the page id 1,

SELECT E.*, P.*, X.*,C.*
FROM entities E

LEFT JOIN pages P ON (P.entity_id = E.entity_id)

LEFT JOIN map_entities_images X ON (X.entity_id = E.entity_id)

LEFT JOIN images C ON (C.image_id = X.image_id)
WHERE P.page_id = 1

returns 2 images.

Run
  • 54,938
  • 169
  • 450
  • 748
  • How do you expect to use this entities tables? What does it give you that standard SQL on the base tables will not? Why are you bothering with this when it has no apparent function? – James Anderson Feb 24 '12 at 01:51
  • thanks. please have a look on my edit. thanks. – Run Feb 24 '12 at 02:48
  • 1
    You can have an image table which is referenced by both "page" and "comment". You don't need any extra tables (assuming there is only one reference to an image in each comment) you just need a column to hold the image-id and everything works! If there is a many to many relationship then you need one intersection table "page-contains-image" with page-id and image-id, and, one table "user-has-image" with user-id and image-id. Keep it simple! – James Anderson Feb 24 '12 at 03:22

2 Answers2

1

If all you need is to attach images to users and pages, I'm not sure a full-blown category (aka. "subclass", "subtype", "inheritance") hierarchy would be optimal.

Assuming pages/users can have multiple images, and any given image can be attached to multiple pages/users, and assuming you don't want to attach images to images, your model should probably look like this:

enter image description here


You could use category hierarchy to achieve similar result...

enter image description here

...but with so few subclasses I'd recommend against it (due potential maintainability and performance issues). On the other hand, if there is a potential for adding new subclasses in the future, this might actually be the right solution (ENTITY_IMAGE will automatically "cover" all these new subclasses, so you don't need to introduce a new "link" table for each and every one of them).

BTW, there are 3 major ways to implement the category hierarchy, each with its own set of tradeoffs.

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Thanks so much for the answer, Branko. I think I would go for category hierarchy as the subclasses may grow in the future. FK in images table is definitely a deadend. an off topic - what programme do you use to draw these beautiful diagrams? :-) – Run Feb 24 '12 at 13:27
  • @lauthiamkok Microsoft Visio. Pick "Database Model Diagram" when creating a new document, draw your diagram, export it to PNG, and you are ready for upload to StackOverflow :) – Branko Dimitrijevic Feb 24 '12 at 13:39
  • sorry Branko, I seem to have another bigger issue, http://stackoverflow.com/questions/9433330/duplications-of-a-relational-table have a look. thanks. – Run Feb 24 '12 at 15:30
0

Not exactly an answer to your question, but, what you are describing is not what most modelers would refer to as a "supertype".

This is analogous to super/sub classes in OOP. The supertype is a genric entity, and, the subtype is a more specialized version of the generic entity

The classic example is vehicles. A "vehicle" has a common set of attributes like "owner" , "price", "make", "model". It doesn't matter whether its a car, a bicycle or a boat. However cars have "wheels", "doors" "engine-size" and "engine-type", bicycles have "number-of-gears" and "terrain-type" (BMX, road etc.) and boats have "propellers", "sails" and "cabins".

There are two ways of implementing this.

Firstly there is a "rollup", you have one table which holds all the common attributes for a "vehicle" plus optional attibutes for each type of vehicle.

Secondly there is a "rolldown", you have one table which holds only the common attributes for every vehicle. And one table for each vehicle type to hold the attibutes specific to "cars", "bicycles" and "boats".

James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • thanks for the answer. maybe i havent got it right yet on making these tables into supertype/ subtype. I am trying to implement this from the answer given in this question - http://stackoverflow.com/questions/9381261/an-image-table-to-serve-two-foreign-tables maybe you can tell me how I can work from that? thanks. – Run Feb 24 '12 at 02:59