Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Default values
How to auto-generate values in a database
15. NEWSEQUENTIALID()
Summary

Instruction

Another function returning a unique identifier is NEWSEQUENTIALID(). Like NEWID(), it doesn't take any arguments.

Unlike NEWID(), NEWSEQUENTIALID() can't be used directly in the INSERT statement or SELECT statement. NEWSEQUENTIALID() can only be used with DEFAULT constraints on table columns of type uniqueidentifier:

CREATE TABLE TableName (
  Id uniqueidentifier PRIMARY KEY DEFAULT NEWSEQUENTIALID(), 
  ...
)

The statement below inserts information about an author named Eva Rayan. The Id column is of the uniqueidentifier data type and has NEWSEQUENTIALID() as its default value. To insert an auto-generated unique value, we use DEFAULT.

INSERT INTO Author (Id, FirstName, LastName, Photo)
VALUES (DEFAULT, N'Eva', N'Rayan', N'imgs/lisa3.jpg');

What's the difference between NEWID() and NEWSEQUENTIALID()? NEWSEQUENTIALID() generates GUIDs in sequential order and works faster in INSERT operations than NEWID().

Exercise

Insert new record into the Author table, putting uniqueidentifier in the Id column using NEWSEQUENTIALID() function. Use data about Anne Willson with the link to the photo imgs/willson.jpg.