The uniqueidentifier type
CREATE TABLE orders (
id uniqueidentifier NOT NULL DEFAULT NEWID() PRIMARY KEY,
created_at datetime2 NOT NULL DEFAULT SYSUTCDATETIME()
);
INSERT INTO orders DEFAULT VALUES;
SELECT id, created_at FROM orders;
uniqueidentifier stores 16 bytes and shows them in the usual text form with hyphens. SQL Server Management Studio shows it in upper case. NEWID() creates a new random value (version 4) every time it is called.
NEWID() or NEWSEQUENTIALID()
NEWID() values are random. That is fine for most columns, but a problem for a clustered primary key: every insert lands at a random place in the table, which causes page splits and a fragmented index on large tables.
NEWSEQUENTIALID() creates values that grow with each call:
CREATE TABLE orders (
id uniqueidentifier NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY CLUSTERED,
created_at datetime2 NOT NULL DEFAULT SYSUTCDATETIME()
);
INSERT INTO orders (created_at)
OUTPUT inserted.id
VALUES (SYSUTCDATETIME());
It has limits:
- It works only as a
DEFAULTon auniqueidentifiercolumn. You can’t call it in a query or a variable.OUTPUT inserted.idreturns the new value. - The values grow only on the same machine and only until Windows restarts. After a restart they can start lower again, but stay unique.
- The next value is easy to guess. Do not use it for IDs that the public sees, such as links or tokens.
A common alternative: keep NEWID() for the primary key, but make it NONCLUSTERED and put the clustered index on a growing column such as created_at or an IDENTITY column.
Why v7 UUIDs don’t sort by time here
A v7 UUID starts with the time, so it sorts by time almost everywhere. Not in SQL Server: it compares the last six bytes of a uniqueidentifier first. A v7 created in your application, for example with Guid.CreateVersion7() in C#, therefore lands at random places in a clustered index, just like NEWID().
SELECT id FROM orders ORDER BY id;
-- sorted by the last group (…-1f489e0ea1c4) first, not by the time at the start
v7 values are still good unique IDs in SQL Server. If the insert order matters for the clustered index, use NEWSEQUENTIALID() or the clustered index on another column as shown above. The guide UUIDs as primary keys explains the background.
Checking and converting
SELECT TRY_CONVERT(uniqueidentifier, '01a183c6-500e-71fb-8e3a-1f489e0ea1c4'); -- the value
SELECT TRY_CONVERT(uniqueidentifier, 'not-a-uuid'); -- NULL
SELECT LOWER(CONVERT(char(36), NEWID())); -- lower case text
TRY_CONVERT returns NULL instead of an error when the text is not a UUID. Careful: SQL Server cuts text after 36 characters before converting, so '0E984725-C51C-4BF4-9960-E1C80E27ABA0wrong' is accepted as 0E984725-C51C-4BF4-9960-E1C80E27ABA0. Check the length too if the input comes from outside:
SELECT CASE
WHEN LEN(@input) = 36 AND TRY_CONVERT(uniqueidentifier, @input) IS NOT NULL THEN 1
ELSE 0
END AS is_valid;
For many values at once, the UUID validator shows for every line why it is valid or not.
Things to watch out for
CAST(id AS binary(16))returns the first three groups byte-reversed, like .NET does. Other systems that read the bytes must know that. The converter shows both byte orders.- SQL Server shows upper case, most other systems use lower case. Compare values as
uniqueidentifier, not as text. - In SQL Server and .NET a UUID is called GUID. It is the same thing. The GUID generator creates them in the usual Microsoft form.