Miroslav Zadravec
Miroslav Zadravec

Reputation: 3730

Constraint with Primary Key

Is there a difference between these two create table snippets? One includes CONSTRAINT keyword, other does not.

CREATE TABLE [dbo].[Person](
    [ID] [bigint] NOT NULL,
    [Name] [varchar](255) NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC))


CREATE TABLE [dbo].[Person](
    [ID] [bigint] NOT NULL,
    [Name] [varchar](255) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([ID] ASC))

I have a database with tables defined in both ways, I'm wondering if I should do something about it.

Upvotes: 4

Views: 2281

Answers (2)

alex
alex

Reputation: 3720

The CONSTRAINT [PK_Person] part is optional. You can read more about it here; from the MSDN page:

CONSTRAINT

Is an optional keyword indicating the beginning of a PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY, or CHECK constraint definition. Constraints are special properties that enforce data integrity and they may create indexes for the table and its columns.

Upvotes: 0

Sankar Reddy
Sankar Reddy

Reputation: 1499

There is no difference apart from the naming of the constraint. If you don't specify one, SQL Server will create it one for you but the name of the constraint is NOT easily recognizable.

I would prefer to give all my database objects good naming conventions instead of relying on the SQL Engine generated names.

Upvotes: 8

Related Questions