Can you define a new column in a SQL Server table which auto-generates Unique Identifiers for new rows?

sql, sql-server, sql-server-2012, t-sql

Solution

You can use a column with Uniqueidentifier type with default value of NEWID()

Problem

Auto-increment columns in SQL Server get populated automatically; is it possible to define a UniqueIdentifier column to auto-generate on insert without the use of a trigger? This will be a secondary unique key on the table. I need to create it because we need a public primary key now which can be used within a query string. Legacy infrastructure still relies on the old int primary key. Given that the old infrastructure creates the record in the first place, I would like SQL Server to transparently create the secondary GUID key on insert - if at all possible. Many thanks

Original source