Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple "ID" columns in SQL Server database?

Via this link, I know that a GUID is not good as a clustered index, but it can be uniquely created anywhere. It is required for some advanced SQL Server features like replication, etc.

Is it considered bad design if I want to have a GUID column as a typical Primary Key ? Also this assumes a separate int identity column for my clustering ID, and as an added bonus a "user friendly" id?

update

After viewing your feedback, I realise I didn't really word my question right. I understand that a Guid makes a good (even if its overkill) PK, but a bad clustering index (in general). My question more directly asked, is, is it bad to add a second "int identity" column to act as the clustering index?

I was thinking that the Guid would be the PK and use it to build all relationships/joins etc. Then I would instead of using a natural key for the Cluster Index, I would add an additional "ID" that not data-specific. What I'm wondering is that bad?

like image 749
Nate Avatar asked Jan 13 '10 21:01

Nate


People also ask

Can we have multiple identity columns in SQL?

Only one identity column can be created per table.

How many columns can be defined as identity column?

Any column in a table can be an identity column, but there can only be one identity column per table.

Can a table have two IDs?

Yes, it is possible for a table to have more than one column which can uniquely identify a row of data. A column that can uniquely identify a record of data is known as a "Candidate Key" .

Should every table have an ID column?

Every table should have a PK,a an Unique identifier, or and ID as you are calling it.


2 Answers

If you are going to create the identity field anyway, use that as the primary key. Think about querying this data. Ints are faster for joins and much easier to specify when writing queries.

Use the GUID if you must for replication, but don't use it as a primary key.

like image 86
HLGEM Avatar answered Sep 20 '22 06:09

HLGEM


What are you intending to accomplish with the GUID? The int identity column will also be unique within that table. Do you actually need or expect to need the ability to replicate? If so, is using a GUID actually preferable in your architecture over handling identity columns through one of the identity range mangement options?

If you like the "pretty" ids generated using the Active Record pattern, then I think I'd try to use it instead of GUIDs. If you do need replication, then use one of the replication strategies appropriate for identity columns.

like image 31
tvanfosson Avatar answered Sep 22 '22 06:09

tvanfosson