views:

530

answers:

9

I have a table with sets of settings for users, it has the following columns:

UserID INT
Set VARCHAR(50)
Key VARCHAR(50)
Value NVARCHAR(MAX)
TimeStamp DATETIME

UserID together with Set and Key are unique. So a specific user cannot have two of the same keys in a particular set of settings. The settings are retrieved by set, so if a user requests a certain key from a certain set, the whole set is downloaded, so that the next time a key from the same set is needed, it doesn't have to go to the database.

Should I create a primary key on all three columns (userid, set, and key) or should I create an extra field that has a primary key (for example an autoincrement integer called SettingID, bad idea i guess), or not create a primary key, and just create a unique index?

----- UPDATE -----

Just to clear things up: This is an end of the line table, it is not joined in anyway. UserID is a FK to the Users table. Set is not a FK. It is pretty much a helper table for my GUI. Just as an example: users get the first time they visit parts of the website, a help balloon, which they can close if they want. Once they click it away, I will add some setting to the "GettingStarted" set that will state they helpballoon X has been disabled. Next time when the user comes to the same page, the setting will state that help balloon X should not be shown anymore.

+1  A: 

Should I create a primary key on all three columns (userid, set, and key)

Make this one.

Using surrogate primary key will result in an extra column which is not used for other purposes.

Creating a UNIQUE INDEX along with surrogate primary key is same as creating a non-clustered PRIMARY KEY, and will result in an extra KEY lookup which is worse for performance.

Creating a UNIQUE INDEX without a PRIMARY KEY will result in a HEAP-organized table which will need an extra RID lookup to access the values: also not very good.

Quassnoi
+4  A: 

Having composite unique keys is mostly not a good idea.

Having any business relevant data as primary key can also make you troubles. For instance, if you need to change the value. If it is not possible in the application to change the value, it could be in the future, or it must be changed in an upgrade script.

It's best to create a surrogate key, a automatic number which does not have any business meaning.

Edit after your update:

In this case, you can think of having conceptually no primary key, and make this three columns either the primary key of a composite unique key (to make it changeable).

Stefan Steinegger
Yes. Imagine the following problems that could arise from a composite key situation. What happens if some of the data in the key needs to change? What if you need to join to this table? You would then need to duplicate the business data in the joining table
1800 INFORMATION
@1800: why change the data in the key? If you want to assign a new value to the new (user, set, key), you just insert or update it, you do not reassign the existing value from other user :) And a surrogate PRIMARY KEY will just result in an extra KEY LOOKUP/RID LOOKUP.
Quassnoi
I've added an update to my question, stating that the information in the settings table, will not be used by any other table, there will be no joins. In the words of Jonathan: this is an end of the line table.
Gidon
Updated my answer according to your update.
Stefan Steinegger
I still think it would not be a good idea to use those columns as a primary key, even if there are no joins. At least there is the "join" to the GUI. If you later decide one of (user,set,key) is mutable you might be in trouble. If you put this data in a cookie this might be a bigger security risk than putting a surrogate primary key there. And so forth. The teensy bit of performance might not be worth it.
hstoerr
A: 

You could create a compound (composite) key on all three columns. But i would be tempted to add an identity field, as using a compound key would give you a mix of an INT field with 2 VARCHAR fields making up that key, which doesn't sit well with me.

kevchadders
A: 

I would probably try to make sure that UserID was a unique identifier, rather than having duplicates of UserID throughout the code. Composite keys tend to get confusing later on in your code's life.

I'm assuming this is a lookup field for config values of some kind, so you could probably go with the composite key if this is the case. The data is already there. You can guarantee it's uniqueness using the primary key. If you change your mind and decide later that it isn't appropriate for you, you can easily add a SettingId and make the original composite key a unique index.

Jonathan
A: 

Create one, separate primary key. No matter what how bussines logic will change, what new rules will have to be applied to your Key VARCHAR(50) field - having one primary key will make you completly independent of bussines logic.

smok1
A: 

In my experience it all depends how many tables will be using this table as FK information. Do you want 3 extra columns in your other tables just to carry over a FK?

Personally I would create another FK column and put a unique constraint over the other three columns. This makes foreign keys to this table a lot easier to swallow.

rein
A: 

Better have UserID as 32 bit newid() or unique identifier bcoz UserID as int gives hint to the User of the probable UserID. this will also solve your issue of composite key.

Vinay Pandey
A: 

How many Key's and Set's do you have? Do these need to be varchar(50) or can they point to a lookup table? If you can convert this Set and Key into SetId and KeyId then you can create your primary key on the 3 integer values which will be much faster.

Robin Day
The number of keys and sets are not set in advance, it's pretty dynamic. I understand your lookup table idea, but for manageability purposes went for this "quick-and-dirty" solution.
Gidon
A: 

I'm not a proponent of composite keys, but in this case as an end of the line table, it might make sense. However, if you allow nulls in any of these three fields becasue one or more of the values is not known at the time of the insert, there can be difficulty and a unique index might be better.

HLGEM