Introduction
A Surrogate Key in SQL Server is a unique identifier for each row in the table. It is just a key. Using this key we can identify a unique row. There is no business meaning for Surrogate Keys. This type of key is either database generated or generated via another application (not supplied by user).
A Surrogate Key is just a unique identifier for each row and it may use as a Primary Key. There is only requirement for a surrogate Primary Key, which is that each row must have a unique value for that column. A Surrogate Key is also known as an artificial key or identity key. It can be used in data warehouses.
A Surrogate Key should have the following characteristics:
- Unique Value
- The key is generated by the system, in other words automatically generated
- The key is not visible to the user (not a part of the application)
- It is not composed of multiple keys
- There is no semantic meaning of the key
Generally, a Surrogate Key is a sequential unique number generated by SQL Server or the database itself. The purpose of a Surrogate Key is to act as the Primary Key. There is a slight difference between a Surrogate Key and a Primary Key. Ideally, every row has both a Primary Key and a Surrogate Key. The Primary Key identifies the unique row in the database while the Surrogate Key identifies a unique entity in the model.
Note that Surrogate Keys are never used with any business logic other than simple Create, Read, Update and Delete (CRUD) operations.
Example of Surrogate Key
- Identity Column in SQL Server
- GUID (Globally Unique Identifier)
- UUID (Universally Unique Identifier)
How can we implement Surrogate Key?
There are several ways to implement Surrogate Keys as in the following:
- Auto Incremental key in Database
A Surrogate Key can be implemented by an auto-incremented key. SQL Server supports an IDENTITY column to perform the auto-increment feature. It allows a unique number to be generated when a new record is inserted into the database table.
- --Syntax for Introducing Auto identity column with Create Table.
- CREATE TABLE [dbo].[EmployeeMaster](
- [EmployeeId] [int]IDENTITY(1,1) NOT NULL,
- [EmployeeCode] [varchar](25) NULL,
- [EmployeeName] [varchar](50) NULL,
- [EmailAddress] [varchar](50) NULL,
- )
- --Syntax for Introducing Auto identity column with Create Table.
- ALTER TABLE EmployeeMasterADD ID INT IDENTITY(1,1)
- Manual Incremental key in Database
A Surrogate Key can be implemented by manual incremental key. Using the max() function we can find the maximum value of a column and this value is incremented by one. This approach suffers from a performance problem when a table has a large amount of data.
- --Example
- DECLARE @newId INT
- SELECT @newId = ISNULL(MAX(EmployeeId),0)+ 1 FROM EmployeeMaster
- PRINT @newId
- --The variable @newId can be used as identifier of newly inserted data.

Sam HobbsPosted Sep 24, 2013, 7:21 PM
I think that actually GUIDs and UUIDs are the same thing and UUIDs are an internet standard. See: http://www.ietf.org/rfc/rfc4122.txt