Search code examples
sqlsql-serverprimary-keysqldatatypes

Unique identifier and long int comparison as primary key


I have seen two data types unique identifier and long int as primary keys of data table many times. Which one is better? Please share your opinions. Advantages and disadvantages over each other of both of them.


Solution

  • if you don't have a specific reason to use GUIDs, use INTs.
    

    ADVANTAGEOUS OF GUID:

    • unique identifier is better because guaranteed to always return a unique value across space and time

    • I don't know the full mechanics of creating a GUID, but I seem to remember that it has something to do with the MAC address on your network card and the system time.

    • This comes in handy if you're consolidating records from multiple SQL Servers into one table, as in a data warehousing situation.

      GUIDs are also used heavily by SQL Server replication to keep track of rows when they're spread out among multiple SQL Servers.

      DISADVANTAGEOUS OF GUID:

    • There is no way to determine the sequence in which uniqueidentifier values were generated. They are not suited for existing applications that depend on incrementing key values serially

    • The main disadvantage to using GUIDs as key values is that they are BIG. Indexes built on GUIDs are going to be larger and slower than indexes built on IDENTITY columns

    • GUIDs are problematic as clustered keys because of the high randomness