After reading my earlier blog post SQL SERVER – Prevent Constraint to Allow NULL. I recently received question from user regarding how to alter the constraint.
No. We cannot alter the constraint, only thing we can do is drop and recreate it.
Here is the CREATE and DROP script.
CREATE DATABASE TestDB
CREATE TABLE TestTable (ID INT, Col1 INT, Col2 INT)
-- Create Constraint on Col1
ALTER TABLE TestTable ADD CONSTRAINT CK_TestTable_Col1
CHECK (Col1 > 0)
-- Dropping Constraint on Col1
ALTER TABLE TestTable DROP CONSTRAINT CK_TestTable_Col1
-- Clean up
ALTER DATABASE TestDB
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE TestDB
If you try to alter the constraint it will throw error.
Reference: Pinal Dave (https://blog.sqlauthority.com)