views:

107

answers:

9

How do you know when to create a new table for very similar object types?

Example: To learn mysql I'm building a model solar system. For the purposes of my project, planets have many similar attributes to dwarf planets, centaurs, and comets. Dwarf planets are almost completely identical to planets. Centaurs and comets are only different from planets because their orbital path has more variation. Should I have a separate table for each type of object, or should they share tables?

The example is probably too simple, but I'm also interested in best practices. Like should I use separate tables just in case I want to make planets and dwarf planets different in the future, or are their any efficiency reasons for keeping them in the same table.

+1  A: 

It's called Database Normalization

There are many normal forms. By applying normalization you will go through metadata (tables) and study the relationsships between data more clearly. By using the normalization techniques you will optimize the tables to prevent redundancy. This process will help you understand which entities to create based on the relationsships between the different fields.

Christian Vik
A: 

If you will ever want to get planets and comets in one single query, they will pretty much have to be in the same table if you want the database to work efficiently. Inheritance should be handled inside your app itself :)

Matchu
+2  A: 

It depends on what type of information you want to store about the objects. If the information for all of them is the same, say orbit radius, mass and name, then you can use the same table. However, if there are different properties for each (say atmosphere composition for planets, etc.) then you can either use separate tables for each (not very normalized) or have one table for basic properties like orbit, mass and name and a second table for just the properties that are unique to planets (and a similar table for comets, etc. if needed). All objects would be in the first table but only planets would be in the second table and linked through a foreign key to the first table.

TLiebe
+3  A: 

Normal forms is what you should be interested with. They pretty much are the convention for building tables.

Any design that doesn't break the first, second or third normal form is fine by me. That's a pretty long list of requirement though, so I suggest you go read it off the Wikipedia links above.

zneak
+1  A: 

You should most likely split the data about a planet etc so that the shared (common) information is in another table.

E.g.

Common (Table)
  Diameter (Column)
  Mass (Column)

Planet
  Population

Comet
  Speed

Poor columns I know. Have the Planet and Comet tables link to the Common data with a key.

Nanook
or create a space objects table with common info and a classification table with different classifications and descriptions specific to object type. If you needed further info specific to classifications you could create more tables for different classification types.
Arthur Thomas
+1  A: 

This is definitely a subjective question. It sounds like you are already on the right lines of thinking. I would ask:

  1. Do these objects share many attributes? If so, it's probably worth considering at the very least a base table to list them all in.
  2. Does one object "extend" another - it has all the attributes of the other, plus some extras? If so, it might be worth adding another table with the extra attributes and a one-to-one mapping back to the base object.
  3. Do both objects have many shared attributes and unshared attributes? If this is the case, maybe you need a single table plus a "data extension" system where each object can have a type or category that specifies any amount of extra attributes that may be associated with it.
  4. Do the objects only share one or two attributes? In this case, they are probably dissimilar enough to separate into multiple tables.

You may also ask yourself how you are going to query the data. Will you ever want to get them all in the same list? It's always a good idea to combine data into tables with other data they will commonly be queried with. For example, an "attachments" table where the file can be an image or a video, instead of images and video tables, if you commonly want to query for all attachments. Don't split into multiple tables unless there is a really good reason.

Renesis
A: 

Here's my answer to a similar question, which I think applies here as well:

http://stackoverflow.com/questions/1931581/how-do-you-store-business-activities-in-a-sql-database/1931960#1931960

Tom H.
A: 

There are many different ways to express inheritance in your relational model. For example you can try to squish everything in to one table and have a field that allows you to distinguish between the different types or have one table for the shared attributes with relationships to a child table with the specific attributes etc... in either choice you're still storing the same information. When going from a domain model to a relational model this is what is called an impedance mismatch. Both choices have different trade offs, for example one table will be easier to query, but multiple tables will have higher data density.

In my experience it's best not to try to answer these questions from a database perspective, but let your domain model, and sometimes your application framework of choice, drive the table structure. Of course this isn't always a viable choice, especially when performance is concerned.

I recommend you start by drawing on paper the relationships you want to express and then go from there. Does the table structure you've chosen represent the domain accurately? Is it possible to query to extract the information you want to report on? Are the queries you've written complicated or slow? Answering these questions and others like them will hopefully guide you towards creating a good relational model.

I'd also suggest reading up on database normalization if you're serious about learning good relational modeling principals.

jonnii
A: 

I'd probably have a table called [HeavenlyBodies] or some such thing. Then have a look up table with the type of body, ie Planet, comet, asteroid, star, etc. All will share similar things such as name, size, weight. Most of the answers I read so far all have good advise. Normalization is good, but I feel you can take it too far sometimes. 3rd normal is a good goal.

nportelli