Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 544
Miroslav Zadravec Avatar asked Feb 24 '23 11:02

Miroslav Zadravec


1 Answers

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.

like image 52
Sankar Reddy Avatar answered Mar 17 '23 01:03

Sankar Reddy