<p>Say you have an entity like a vehicle that you are capturing detailed information about. The car you want to capture is painted red, black and white. The front tires are Bridgestone 275/35-18 and the rear tires are 325/30-19. And sometimes you can have just two tires (yes this would be considered a motorcycle which is a type of vehicle) and sometimes 18 tires that could all be different. Then there are some fields that are always single valued like engine size (if we let our imaginations run wild we can think of multi-engined vehicles but I am trying to keep this simple). </p>

<p>Our current strategy for dealing with this is to have a table for each of the fields that can have multiple values. This will spawn a large number of tables (we have a bunch of different entities with this requirement) and smells a little bad. Is this the best strategy and if not, what would be better?</p>

[{'answer_id': 65836, 'author': 'Jerph', 'author_id': 1701, 'author_profile': 'https://Stackoverflow.com/users/1701', 'pm_score': 1, 'selected': False, 'text': '<p>If it\'s a possibility for your app, you might want to look into <a href="http://incubator.apache.org/couchdb/" rel="nofollow noreferrer">couchdb</a>.</p>\n'}, {'answer_id': 65858, 'author': 'Jason Punyon', 'author_id': 6212, 'author_profile': 'https://Stackoverflow.com/users/6212', 'pm_score': 0, 'selected': False, 'text': '<p>You\'re describing a <a href="http://en.wikipedia.org/wiki/Star_schema" rel="nofollow noreferrer">Star Schema</a>.  I think its fairly standard practice in your kind of case</p>\n\n<p>Edit:  Actually your schema is slightly modified from the Star Schema, you use the primary key of the fact table in each of the dimension tables to join on so you can have multiple paint colors etc.  Either way I think it\'s a fine way to deal with your entity.  You may go one step further and normalize the dimension tables and then you\'d have a <a href="http://en.wikipedia.org/wiki/Snowflake_schema" rel="nofollow noreferrer">Snowflake Schema</a></p>\n'}, {'answer_id': 65866, 'author': 'squadette', 'author_id': 7754, 'author_profile': 'https://Stackoverflow.com/users/7754', 'pm_score': 0, 'selected': False, 'text': '<p>It seems like you may be looking at something called <a href="http://en.wikipedia.org/wiki/Hierarchical_model" rel="nofollow noreferrer">Hierarchical Model</a>.</p>\n\n<p>Or maybe a simple list of (attr, value) pairs will do?</p>\n'}, {'answer_id': 65922, 'author': 'Jarrett Meyer', 'author_id': 5834, 'author_profile': 'https://Stackoverflow.com/users/5834', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re using SQL Server, don\'t be afraid to store the <a href="http://davidhayden.com/blog/dave/archive/2006/04/11/2909.aspx" rel="nofollow noreferrer">XML Data Type</a>. I have found that it makes things like this much, much easier.</p>\n'}, {'answer_id': 65932, 'author': 'Christian P.', 'author_id': 9479, 'author_profile': 'https://Stackoverflow.com/users/9479', 'pm_score': 0, 'selected': False, 'text': "<p>It really depends on whether the variables themselves only have one variable (example: you can have a variable number of tires that are all the same type, or a set number of tires that are of variable type).</p>\n\n<p>Since you seem to need to have multiple variables (eg. specific type for each tire, with a variable number of tires), I am afraid the best solution is to have specific tables for each specific area of the car you wish to customize.</p>\n\n<p>If you have some fields that simply have a set of values to chose between (say, 2, 4 or 6 windows), you can simply use an enum or define a new field-type using User-Defined Domains (depending on which DBMS you're using).</p>\n"}, {'answer_id': 66042, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re using a relational database, your suggestion is pretty much the only way to do it. The <a href="http://en.wikipedia.org/wiki/Database_normalization" rel="nofollow noreferrer">theory of normal forms</a> will give you more information about it - the Wikipedia articles about it are quite good, though slightly heavy going simply because it is a tricky theoretical subject when you get into the higher normalisation levels. The examples are mostly common sense though.</p>\n\n<p>Assuming you have a Vehicle table, a Colour table and a TyreType table (sorry for the British spelling), you are presumably defining a VehicleTyre and VehicleColour table which acts as a join between the relevant pairs of tables. This structure is actually quite healthy. It not only encapsulates the information you want directly, but also lets you capture in a natural way things like which tyre is which (e.g. front left is Bridgestone 275/35-18) or how much of the car is painted red (e.g. with a percentage field on the VehicleColour table).</p>\n\n<p>You may want to model a vehicle type entity which could govern how many tyres there are. While this is not necessary in order to get working SELECT queries out of the system, it is probably going to be useful both in your user interface and figuring out how many tyres to insert into your tables.</p>\n\n<p>My company has lots of schemas which operate on exactly this basis - indeed our object-relational framework creates them automatically to manage many-to-many relationships (and sometimes even for one-to-many relationships depending on how we model them). Several of our apps have over 150 entities and over 100 of these join tables. There are no performance problems and no meaningful impact on manageability of the data, except that a few of the table names are annoyingly long.</p>\n'}, {'answer_id': 77398, 'author': 'Seun Osewa', 'author_id': 6475, 'author_profile': 'https://Stackoverflow.com/users/6475', 'pm_score': 0, 'selected': False, 'text': "<p>Your current strategy is the correct one.  You're tracking so many kinds of data, so you'll need lots of tables.  That's just how it is.  Is the DBMS complaining?</p>\n"}]
['https://Stackoverflow.com/questions/65805', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']