Wednesday, April 8, 2015

sql newid()

Yes, newid() is unique. newid() returns a globally unique identifier. newid() will be unique for each call of newid().
Thus, newid() can be used as the value of a primary key of a sql server table. Values returned by newid() can be inserted into a primary key column of type "uniqueidentifier". Here is an example of a "uniqueidentifier" primary key column, with newid() used as the default value:


CREATE TABLE [tblUsers] (
    [UserId] [uniqueidentifier] NOT NULL DEFAULT (newid()),
    [UserName] [varchar](256) NOT NULL,
    PRIMARY KEY ([UserId])
)

No comments:

Post a Comment