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.

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

No comments:

Post a Comment