Search code examples
postgresqlddlcheck-constraints

how to disable check constraint in postgresql


I have below table with check constraint for salary column. I want to disable the check constraint temporarily. How to disable and enable the check constraints?

CREATE TABLE "Employee_Salary_Details"(
  empno int,
  ename varchar(100),
  sal numeric CONSTRAINT CK_SAL CHECK(sal>3500)
)

INSERT INTO "Employee_Salary_Details" VALUES(101,'RAM',200);

ALTER TABLE "Employee_Salary_Details" DISABLE  CONSTRAINT CK_SAL 

I tried, but it is showing an error message. Is it possible to disable and enable heck constraints?


Solution

  • Is it possible to disable and enable check constraints?

    No, that's not possible. You need to drop and re-create it.

    Use:

    ALTER TABLE Employee_Salary_Details DROP CONSTRAINT CK_SAL;
    

    Do your business, and then add it back:

    ALTER TABLE Employee_Salary_Details ADD CONSTRAINT CK_SAL CHECK (sal > 3500);