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()
.