Why can't I specify both a Primary Key and a Unique Constraint on the same table? Why can't I specify both a Primary Key and a Unique Constraint on the same table? sql-server sql-server

Why can't I specify both a Primary Key and a Unique Constraint on the same table?


You missed a comma after the primary key constraint.

CREATE TABLE [MyTable](    [Id] [int] IDENTITY,    [Column1] [int] NOT NULL,    [Column2] [int] NOT NULL    CONSTRAINT [PK_MyTable_Id] PRIMARY KEY ([Id]),    CONSTRAINT [UQ_MyTable_Column1_Column2] UNIQUE ([Column1], [Column2]))


CREATE TABLE [MyTable](    [Id] [int] IDENTITY,    [Column1] [int] NOT NULL,    [Column2] [int] NOT NULL    CONSTRAINT [PK_MyTable_Id] PRIMARY KEY ([Id]))  ALTER TABLE [MyTable] ADD CONSTRAINT [UQ_MyTable_Column1_Column2] UNIQUE ([Column1], [Column2])


Or put a comma between the primary key declaration and unique constraint declaration:

CREATE TABLE [MyTable](    [Id] [int] IDENTITY,    [Column1] [int] NOT NULL,    [Column2] [int] NOT NULL    CONSTRAINT [PK_MyTable_Id] PRIMARY KEY ([Id]),    CONSTRAINT [UQ_MyTable_Column1_Column2] UNIQUE ([Column1], [Column2]))