Question

I am busy designing a log table which certain entries will be stored in, I have simplified the problem for this question.

Here is an example of a standard table:

bigInt: PK | int:client_id | datetime: time | string:data 

Now the way I see it, the PK in this case is not really needed in terms of me querying the data which I need? Is there a way you could create a virtual / compound primary key?

So in the case above, you could identity this row, buy a compound PK of client_id and time. This way you would save on having to store a useless integer for every entry?

Or an I missing something important about the primary key?

Was it helpful?

Solution

Yes, it's definitely possible to use a compound primary key in SQL server.

PK, on the other hand, is what's called a surrogate key: A key field which is not strictly necessary, but which provides easy access to a row.

More importantly, a surrogate key never changes: client_id and time (the natural key), have some business meaning, and, thus, might need to be modified, which can be a hassle when other tables reference them as a foreign key.

The Wikipedia article on Surrogate Key provides a lot of information on the pros and cons of both approaches.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top