2

Table locations contains locID, lat & long & name values of a certain location.

Table categories contains ID, category-name & locID.

How do I make categories assignable to multiple locations?

It doesn't make sense to have the same category stored multiple times, once for every locID.

1

1 Answer 1

2

You can use a join table to model the many-to-many relationship.

location_category
location_id   category_id
1             1
1             2
2             1
2             3
3             4

Add foreign key constraints to prevent invalid values from being entered into the table:

  • From location_category.location_id to locations.locID
  • From location_category.category_id to categories.ID

Also make (location_id, category_id) the primary key for the table to prevent adding a category to the same location multiple times.


When reading the data from the table, use JOINs to get the related data from the main tables:

SELECT ...
FROM location_category
JOIN locations ON location_category.location_id = locations.locID
JOIN categories ON location_category.category_id = categories.ID
WHERE ....
Sign up to request clarification or add additional context in comments.

2 Comments

Huh, I don't really understand that, what kind of fields do I need to create to link the catyegory to the location?
Ah, got it, that's awesome, I just need a table (foreign key) to associate one key to the other, awesome! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.