1

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?

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

Comments

2

There's another way around..

SELECT consrc,con.*
       FROM pg_catalog.pg_constraint con
            INNER JOIN pg_catalog.pg_class rel
                       ON rel.oid = con.conrelid
            INNER JOIN pg_catalog.pg_namespace nsp
                       ON nsp.oid = connamespace
       WHERE nsp.nspname = 'public' and contype='c'

Try to find your constraint in this query result.. And then update 'conrelid' to 0 for that entry.

This will disable that constraint.

Hope this works..

And after your insertion work done.. Revert same value for 'conrelid'

2 Comments

Suggesting to manipulate the system tables directly is a really bad advice
Yeah.. But this is one of the solution for this problem. I'm just giving another approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.