Search code examples
check-constraintsmssql-jdbc

SQL Server : Check Constraint expression


I have a check constraint in SQL Server that only allows 3 possible values, the expression is like this:

(([READ_WRITE] = 'H' OR [READ_WRITE] = 'W' OR [READ_WRITE] = 'R'))

I want to update this check constraint with a query because I don't have access to Management Studio.


Solution

  • You basically need to first drop the old check constraint:

    ALTER TABLE dbo.YourTable
        DROP CONSTRAINT CHK_YourTable_ReadWriteValues;
    

    (and fill in whatever actual names you have for your table and the check constraint on it), and then you need to create the new one:

    ALTER TABLE dbo.YourTable
        ADD CONSTRAINT CHK_YourTable_NewReadWriteValues
            CHECK ([READ_WRITE] IN ('X', 'Y', 'Z'));