views:

217

answers:

9

Hi,

I need to store info about county, municipality and city in Norway in a mysql database. They are related in a hierarchical manner (a city belongs to a municipality which again belongs to a county).

Is it best to store this as three different tables and reference by foreign key, or should I store them in one table and relate them with a parent_id field?

What are the pros and cons of either solution? (both structural end efficiency wise)

+3  A: 

County, Municipality, and City don't sound like they are the same kind of data ; so, I would use three different tables : one per data-type.

And, then, I would indeed use foreign keys between those.


Efficiency-speaking, not sure it'll change much :

  • you'll do joins on 3 tables instead of joining 3 times on the same table ; I suppose it's quite the same.
  • it might make a little difference when you need to work on only one of those three type of data ; but with the right indexes, the differences should be minimal.

But, structurally speaking, if those are three different kind of entities, it makes sense to use three different tables.

Pascal MARTIN
+1  A: 

I would recommend for using three different tables as they are three different entities.

I would use only one table in those cases you don´t know the depth of the hierarchy, but it is not case.

pedromarce
+1  A: 

I would put them in three different tables, just on the grounds that it is 3 different concepts. This will hamper speed and will complicate your queries. However given that MySQL does not have any special support for hirachical queries (like Oracle's connect by statement) these would be complicated anyway.

Lars Tackmann
+3  A: 

three different tables:

  • more efficient, if your application mostly accesses information about only one entity (county, municipality, city)
  • owner-member-relationship is a clear and elegant model ;)
ptikobj
+4  A: 

If you've really got a limit of these three levels (county, municipality, city), I think you'll be happiest with three separate tables with foreign keys reaching up one level each. This will make queries almost trivial to write.

Using a single table with a parent_id field referencing the same table allows you to represent arbitrary tree structures, but makes querying to extract the full path from node to root an iterative process best handled in your application code.

The separate table solution will be much easier to use.

Drew Hall
+1  A: 

Different tables: it's just "right". I doubt you'll see any performance gains/losses either way but this is one where modelling it properly up-front will probably save you lots of headaches later on. For one thing it'll make SQL SELECTs easier to write and read.

Brian
+1  A: 

You'll get different opinions coming back to you on this but my personal preference would be to have separate tables because they are separate entities.

In reality you need to think about the queries you will doing on this data and usually your answer will come from that. With separate tables your queries will look much cleaner and in the end your not saving yourself anything because you'll still be joining tables together, even if they are the same table.

CResults
A: 

I would use three separate tables, since you know exactly what categories of information you are working with, and won't need to dynamically alter the 'depth' of your hierarchy.

It'll also make the data simpler to manage, as you'll be able to tell if the data is for a city, municipality or a county just by knowing the table (and without having to discern the 'depth' of a record in the hierarchy first!).

Since you'll probably be doing self joins anyway to get the hierarchy to work, I'd doubt there would be any benefits from having all the data in a single table.

Stephen
A: 

In dataware housing applications, adherents of the Kimball methodology might place these fields in the same attribute table:

create table city (
   id int not null, 
   county varchar(50) not null,
   municipality varchar(50),
   city varchar(50),
   primary key(id) 
);

The idea being that attibutes should never be more than l join away from the fact table.

I just state this as an alternative view. I would go with the 3 table design personally.

Martin