Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd recommend <em>not</em> trying to expand the table horizontally, that's an operation that you should make a conscious decision to have.</p> <p>Instead, I'd recommend that you store the values as name/value pairs. You can have tables that have specific types (let's say you needed an integer value paired with a key), and then you would select those into a dictionary for the user.</p> <p>You'd also have a table which has the keys, if you are concerned about replicating key values.</p> <p>For example, you'd have a <code>UserDefinedKey</code> table</p> <pre><code>UserDefinedKeyId (int, PK) Key (varchar(?)) -------------------------- ---------------- 1 'My Website' 2 'My favorite color' </code></pre> <p>Then you would have a <code>UserDefinedString</code> table (for string values)</p> <pre><code>UserDefinedStringId UserId UserDefinedKeyId Value (int, PK) (int, FK) (int, FK) (varchar(max)) ------------------- --------- ---------------- -------------- 1 1 1 'http://stackoverflow.com' 2 1 2 'Blue' 3 2 2 'Red' </code></pre> <p>You'd probably want to place a unique index on the <code>UserId</code> and <code>UserDefinedKeyId</code> fields to prevent people from entering multiple values for the same key (if you want that, have a separate table <em>without</em> the unique constraint).</p> <p>Then, when you want to add a value for users, you add it to the <code>UserDefinedKey</code> table, and then drive your logic off that table and the other tables which hold the values.</p> <p>Another benefit of storing the values vertically is that you aren't wasting space for columns with values that aren't being used by <em>all</em> users.</p> <p>For example, assuming you take the approach of modifying the table, for the attributes above, you would get:</p> <pre><code>UserId WebSite Color ------ ------- ----- 1 http://stackoverflow.com Blue 2 (null) Red </code></pre> <p>Now let's say a third user comes along, and adds a <code>Favorite Sports Team</code> value, and they are the only one who uses it, the table then looks like:</p> <pre><code>UserId WebSite Color FavoriteSportsTeam ------ ------- ----- ------------------ 1 http://stackoverflow.com Blue (null) 2 (null) Red (null) 3 (null) (null) Yankees </code></pre> <p>As the number of users and attributes grows, the amount of sparse data that you have will increase dramatically.</p> <p>Now, assuming you are using SQL Server 2008, you could use <a href="http://msdn.microsoft.com/en-us/library/cc280604%28SQL.100%29.aspx" rel="nofollow">sparse columns</a>, if you don't, your table is going to get huge but not have much data.</p> <p>Also, using sparse columns doesn't take away from the fact that it's pretty dirty to use <a href="http://en.wikipedia.org/wiki/Data_Definition_Language" rel="nofollow">data definition language (DDL)</a> to change the schema on the fly.</p> <p>Additionally, Entity Framework isn't going to be able to adapt it's object model to account for the new attributes; every time you have an attribute added, you will have to go and add the attribute to your object model, recompile, and redeploy.</p> <p>With a vertical approach, it takes more work, granted, but it will be infinitely flexible, as well as utilize your database space more efficiently.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload