Changing Data Type of a Primary Key in SQL Server
Wednesday, July 8, 2026
I'm helping my team changing primary key data type, but a simple ALTER command doesn't work due to the PK constraint. I was under impression that a table always need a PK, so I can't drop the constraint, but I tried it anyway.
ALTER TABLE [table]
DROP CONSTRAINT PK_Id;
And it works, I ended up with a table without any PK. Another ALTER to change the data type and one more ALTER to add back the PK constraint and voilà!
ALTER TABLE [table]
ALTER COLUMN Id INT;
ALTER TABLE [table]
ADD CONSTRAINT PK_Id PRIMARY KEY (Id);