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

sqlsql serversql-server-2012tsql

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

Best Answer

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