Abhilash anand
Abhilash anand

Reputation: 127

Finding the right primary key

I have decided to set combination of three values as a primary key(composite key) for my database table.Most of the times the first two columns will be unique.But in a few cases they are both same at which time the third columns value will be always unique for that row.The problem is that the third column is a description which is any string that the user can enter.I know it is not recommended to have a user given string as a primary key.But is it okay to have it as a part of the composite key.I really see no other option.

Upvotes: 0

Views: 44

Answers (2)

Vishal Zanzrukia
Vishal Zanzrukia

Reputation: 4973

Add a separate column called id and make it auto increment and use this column as a primary key.

Hope this will solve your problem.

Cheers.

Upvotes: 0

morsor
morsor

Reputation: 1323

If at all possible, you should add a surrogate primary key, meaning a key that is unique and has no business meaning whatsoever.

Composite primary keys very often turn out to be less 'stable' than anticipated - but if you MUST use one, using one or more component columns over which the user has little or no control (like created_timestamp) would be preferable.

Upvotes: 1

Related Questions