Search code examples
mysqlentity-relationship

How to store a "location" for an "event" in MySQL


I have two entities: Event and Location. The relations are:

1 Event can have 1 Location. 1 Location can have many Events.

Basically I want to store events. And each event is hosted in a specific location. When I say specific location I mean:

Street, Number, City, Zip Code, State, Country

I basically have a design question, that I would like some help with:

1 - Right now I am thinking on doing the following:

Event table will have a location_id that will point to a specific location row in the locations table. What happens with this is that: I will have many repeated values in each row. For example, if an event is happening in 356 Matilda Street in San Francisco, and another one is happening in 890 Matilda Street in San Francisco. The values Matilda Street and San Francisco will be duplicated many times in the location table. How can I redesign that to normalize this?

So, basically I would love to hear a good approach to solve this question in terms of a relational database, like MySQL.


Solution

  • If you want a strictly normalized database, you could have a table for street names, another for cities, another for states, and so on. You might even have an additional location table that holds unique combinations of street, city, and state; you'd add rows to this table each time an event occurs at a previously unknown location. Then each of your events would reference the appropriate row in the location table.

    In practice, though, it's sometimes better simply to store the location data directly within the events table and tolerate the extra memory usage; there's always a trade-off between speed and memory use.

    Another consideration: what happens if a street is renamed? Do you want old events to be associated with the old name or the new name?