Add primary key to existing table
drop constraint and recreate it
alter table Persion drop CONSTRAINT <constraint_name>alter table Persion add primary key (persionId,Pname,PMID)
edit:
you can find the constraint name by using the query below:
select OBJECT_NAME(OBJECT_ID) AS NameofConstraintFROM sys.objectswhere OBJECT_NAME(parent_object_id)='Persion'and type_desc LIKE '%CONSTRAINT'
I think something like this should work
-- drop current primary key constraintALTER TABLE dbo.persion DROP CONSTRAINT PK_persionId;GO-- add new auto incremented fieldALTER TABLE dbo.persion ADD pmid BIGINT IDENTITY;GO-- create new primary key constraintALTER TABLE dbo.persion ADD CONSTRAINT PK_persionId PRIMARY KEY NONCLUSTERED (pmid, persionId);GO
-- create new primary key constraintALTER TABLE dbo.persion ADD CONSTRAINT PK_persionId PRIMARY KEY NONCLUSTERED (pmid, persionId);
is a better solution because you have control over the naming of the primary_key.
It's better than just using
ALTER TABLE Persion ADD PRIMARY KEY(persionId,Pname,PMID)
which yeilds randomized names and can cause problems when scripting out or comparing databases