I have a problem that I cannot seem to find a solution for. I have browsed similar answers around here, but nothing was exactly what I was looking for. I am fairly familiar with SQL, but not an expert by any means.
I have a table with information about apartment buildings – name, area, neighborhood, phone number, address, zip, etc. Currently, I have several amenities defined inside this table, such as “doorman”, “elevator”, “gym” (and few others) as tinyints to indicate whether the building does indeed have that amenity or not. However, I am in the process of incorporating many more amenity types and I figured that this might the time to change the structure as adding a column to a huge table of buildings does not make too much sense.
So I am looking for a solution that would provide me with the best ability to do the following:
1) search for a building based on user defined amenities (for example user selects that he wants to see buildings that must have a gym and a doorman, doesn’t care about the others)
2) return the data about the building including all of the amenities that the building contains (for displaying information)
I was thinking about separating these structures into a many to many relationship and doing the following:
1) creating new table called “amenities” that would contain all of the amenities have the following structure:
id -> unique id of the amenity
name -> text description of the amenity
value -> for example “pet_friendly” (for searching in forms, not sure if necessary)
2) creating a relationship table between buildings and amenities in another table called “building_amenities” with the following structure:
id -> unique id of the relationship
building_id -> refers to an id of a building
amenity_id -> refers to an id of an amenity
value -> tinyint, whether the building has the amenity or not
In this table each building would have a relationship to each amenity and indication whether it has it or not.
My problem is that I am not sure if this is the correct structure for this situation and I certainly have no clue how I would implement search queries if I had this structure.
Thanks a ton for any help with this!
I would recommend that you create a buildings table, an amenities table and a relational table. Each building has an ID, as does each amenity. To select all buildings with certain amenities present, you would simply write a query as follows
To return a building with its amenities you simply change the WHERE clause to WHERE id_building = x and then all the amenities should be selected in.