开发者

Constraint with Primary Key

开发者 https://www.devze.com 2023-02-26 10:10 出处:网络
Is there a difference between these two create table snippets? One includes CONSTRAINT keyword, other does not.

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]开发者_如何学Go [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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号