Database Design -- Rows vs Column

Status
Not open for further replies.

Netbrian

Wise, Aged Ars Veteran
100
Subscriptor
We have a database that includes information on various features of houses. In this context, what would be the best way to store something like the number of features (such as jacuzzis) a house has --

Create a new column like this?

Code:
ID,feature1,feature2,feature3,jacuzzi_count,etc

Or something like this?

Code:
ID,Feature_Type,Quantity

Are there good rules of the thumb for making this kind of decision, especially if there are lots of possible features? How much does it matter how common a given feature is (only a few homes have jacuzzis but chimneys are much more common.)

Thank you!
 
In this case you probably want 3 tables - "houses", "features", and "houseFeatureLink" or whatever name makes sense to you. "Houses" and "Features" are pretty self explanatory. Then in the link table, you create one row for every house-feature relationship. So if house1 has a chimney, a jacuzzi, and a furnished basement, in the link table you might see:

Code:
houseID     featureID
1                 1
1                 2
1                 3

Then if you want to see all the features of house1, you select that ID from this table and join with the features table to see what the feature IDs are actually equal to. Or, if you want to see all houses with jacuzzis, you select for that feature ID instead, and join back to houses on houseID. You get the idea.

To Blacken's point, this is pretty basic database normalization stuff, and is something I would expect a semi-competent dev to know even without having taken a databases class.
 
Status
Not open for further replies.