I am creating a database for my company that will store many different types of information. The categories are Brightness, Contrast, Chromaticity, ect. Each category has a number of data points which my company would like to start storing.
Normally, I would create a table for each category which would store the corresponding data. (This is how I learned to do it). However, Sometimes these categories have “sub-data” which would change the number of fields required in each table.
My question is then how do people handle the inconsistency of data when structuring their databases? Do they just keep adding more tables for extra data or is it something else altogether?
There are a few (and thank goodness only a few) unbendable rules about relational database models. One of those is, that if you don’t know what to store, you have a hard time storing it. Chances are, you’ll have an even harder time retrieving it.
That said, the reality of business rules is often less clear cut than the ivory tower of database design. Most importantly, you might want or even need a way to introduce a new property without changing the schema.
Here are two feasable ways to go at this:
(NoSQL and friends). Explaining this in detail is a subject of a CS
Thesis, not a stackoverflow answer.
this goes:
Assuming for the sake of argument, your products allways have (unique string)
name, (integer)id,brightness,contrast,chromaticityplus sometimes (integer)fooand (string)bar, consider these tablesnow your “standard” properties would be in the
productstable as usual, while the “optional” properties would be stored in a row ofproduct_properties, that references the product id and property id, with the value being inintvalueorstringvalue.Selecting products including their
fooif any would look likeor even
Please understand, that this incurs a performance penalty – in fact you trade performance against flexibility: Adding another property is nothing more than
INSERTing a row inproperties, the schema stays the same.