Reputation: 171
I have a table within a MySQL database with various data in it (for the sake of example, let's give it the following:
ID | Place | Phone | Email | Category
Now, I want to redesign the "Category" section to be able to include any given row in multiple (searchable) categories.
For example, let's say that there is a place called 'Lakeside Picnic Area.' Currently it is classified as 'Nature Reserve'. So, when people are searching for a nature reserve, it shows in their search results. However, I also want it to show up in "Food Area", "Wildlife", and "Viewing Point".
My question is this: How do I design my table/database structure to allow all of the data entries/rows to be searchable by multiple categories?
CONTINUATION: I don't suppose that anyone could help me to formulate the "Insert" command for my PHP could they?
Upvotes: 0
Views: 178
Reputation: 9302
You need a design like this:
Table: Places Indexes:
+----+-----------+-------+------------------+ ID: Primary Key; Auto-Increment
| ID | Place | Phone | Email |
+----+-----------+-------+------------------+
| 1 | Somewhere | 12345 | [email protected] |
| 2 | SomePlace | 54321 | [email protected] |
+----+-----------+-------+------------------+
Table: Categories Indexes:
+-------------+-----------------+ Category_id: Primary Key; Auto-Increment
| Category_id | Category_name |
+-------------+-----------------+
| 1 | Test Category 1 |
+-------------+-----------------+
Table: Category_relationships Indexes:
+-------------+----------+-------------+ record_id: Primary Key; Auto-increment
| record_id | place_id | category_id | place_id: Foreign Key
+-------------+----------+-------------+ category_id: Foreign Key
| 1 | 1 | 1 | place_id AND category_id: Unique
+-------------+------------------------+
| 1 | 2 | 1 |
+-------------+------------------------+
Then if you wanted to retrieve all the categories for place ID 1:
SELECT c.category_name
FROM categories c
INNER JOIN category_relationships cr
ON c.category_id = cr.category_id
INNER JOIN places p
ON cr.place_id = p.ID
WHERE p.ID = 1
Upvotes: 1
Reputation: 3848
You need a Catagories
table and a Place_category
table.
The Catagories table will be something like:
Category_ID int,
Category_Name varchar
The Place_category table should be like:
Category_ID int
Places_ID int
Then you would enter one entry in the Place_category table for each category you want to assign to each place.
Upvotes: 1
Reputation: 51565
This is called an n m relationship.
In other words, a place can have more than one category, and a category has more than one place.
Here's how you would model it.
Place
-----
Place ID
Place Name
Place Phone
Place Email
Category
--------
Category ID
Category Name
CategoryPlace
-------------
CategoryPlace ID
Category ID
Place ID
All three tables have an auto-incrementing int or long primary key.
The CategoryPlace
table would have a unique index on (Category ID, Place ID). This allows you to find all of the places for a given category.
You can also have a unique index on (Place ID, Category ID). This allows you to find all the categories for a given place.
Upvotes: 4
Reputation: 6947
Simply use a new table with 2 columns :
1 column for the id of your item "Lakeside Picnic area" 1 column for a category id
Multiple category would the result in multiple lines in that table, with the same item id but with a different category id.
That way you can easily retrieve all the items for a given category, and all the categories for a given item. What you're trying to represent is called a many-to-many relationship, cause each category can have many items, and each item can have many categories.
Upvotes: 0