Friday, February 8, 2013

Alter a table column that has primary key and foreign key


We may getting error when we try to alter a column in SQLServer table. To handle that situation we need to delete the Primary Key and Foreign Key from the table before alter. Below is a sample code to alter datatype of a column name id from int to bigint in a table PaymentsType.

ALTER TABLE PaymentsType drop CONSTRAINT PK_PaymentsType  
ALTER TABLE PaymentsType ALTER COLUMN id bigint not null  
ALTER TABLE PaymentsType ADD CONSTRAINT PK_PaymentsType PRIMARY KEY (id)  

No comments:

Post a Comment