allDroid
allDroid

Reputation: 415

GUID_PK in SQL Server

When I execute this query, I am getting an error

Cannot find data type dbo.GUID_PK

For this should I create table for CandidateRoleID or what else should I do here?

But, when I googled it I found it saying SQL Server stores GUID. How could I access it or what is the correct way of declaring this table? I searched in google about GUID_PK.

But didn't find any syntax or explanation. Thanks in advance.

CREATE TABLE [dbo].[tbl]
(
    [CandidateRoleID] [dbo].[GUID_PK] NOT NULL,
    [CandidateID]    [uniqueidentifier] NOT NULL,
    [RoleID]           [int] NOT NULL
);

Upvotes: 0

Views: 135

Answers (3)

bmsqldev
bmsqldev

Reputation: 2735

There is no data type 'GUID_PK' in sql server.

GUID is an acronym for Global Unique ID(entifier) and is a unique 16 byte number. The term GUID and UNIQUEIDENTIFIER are often interchangeable within the SQL Server community.

If you need to auto-generate uniqueidentifier value in your table during data insert, the consider adding default value to the uniqueidentifier data-type.

CREATE TABLE #tbl(
[CandidateRoleID] [uniqueidentifier]   NOT NULL DEFAULT NEWID(),
[CandidateID]    [uniqueidentifier] NOT NULL   DEFAULT  NEWID(),
[RoleID]           [int] NOT NULL);

GO

INSERT INTO #tbl (ROLEID) SELECT  (1)

CandidateRoleID                          CandidateID                            RoleID
F20AE15E-8D97-4042-8AA8-DD7BCB0EB6D6    FAE29358-BD34-4BFE-800B-E332375E020E    1

Upvotes: 0

Adarsh H D Dev
Adarsh H D Dev

Reputation: 618

CREATE TABLE [dbo].[tbl](
[CandidateRoleID] [uniqueidentifier] primary key NOT NULL,
[CandidateID] [uniqueidentifier] NOT NULL,
[RoleID] [int] NOT NULL,
);

In SQL server GUID is uniqueidentifier data type.

Upvotes: 0

Paddy
Paddy

Reputation: 33867

The SQL server data type for a GUID is UNIQUEIDENTIFIER.

Your script should be:

CREATE TABLE [dbo].[tbl](
[CandidateRoleID] [uniqueidentifier] NOT NULL,
[CandidateID] [uniqueidentifier] NOT NULL,
[RoleID] [int] NOT NULL,
CONSTRAINT PK_Tbl PRIMARY KEY (CandidateRoleID));

If you want it to be 'automatically' created, similar to an integer identity column, give it a default:

CREATE TABLE [dbo].[tbl](
    [CandidateRoleID] [uniqueidentifier] NOT NULL DEFAULT NEWID(),
    [CandidateID] [uniqueidentifier] NOT NULL,
    [RoleID] [int] NOT NULL,
    CONSTRAINT PK_Tbl PRIMARY KEY (CandidateRoleID));

Upvotes: 3

Related Questions