0

This is two-part question involving CHECK statements, In my first CHECK for some reason it works but actually allows null to be inserted into the field... any ideas how to remedy this? In my second CHECK, It plain and simple does not work. It's allowing anything to be inserted.

ALTER TABLE table_name ADD (CONSTRAINT CK_CON (name IN ('Jon','Bill','Ted','Ron','Jeff')));

ALTER TABLE table_name ADD (CONSTRAINT CK_NAME CHECK(
         (name='John') OR (name='Mary') OR 
          (name='Jane') OR (name= 'Bruce') OR (name= '')));
2
  • 3
    Add a NOT NULL constraint. Commented Oct 19, 2015 at 12:10
  • 3
    In a WHERE clause, the entire predicate has to be TRUE in order to match. In a CHECK constraint, the predicate needs to not be FALSE. Where this makes a difference is precisely in the areas of NULLs and the fact that SQL has a three-valued logic which sometimes produces UNKNOWN when NULL crops up. Commented Oct 19, 2015 at 12:19

2 Answers 2

3

as for first question change column name to not null

as for second question

try do it such way

ALTER TABLE table_name ADD (CONSTRAINT CK_NAME CHECK(
         (name='John') OR (name='Mary') OR 
          (name='Jane') OR (name= 'Bruce') )); -- you have to remove this OR (name= '')
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting the error - USER.CK_NAME - check constraint violated. No ideaaa why?
because you have value name inserted related to the checking you provide. you have to clean your data or modify you checking depending on what you want
0

ALTER TABLE table_name ADD (CONSTRAINT CK_CON (name IN ('Jon','Bill','Ted','Ron','Jeff')));

ALTER TABLE table_name ADD (CONSTRAINT CK_NAME CHECK( (name='John') OR (name='Mary') OR (name='Jane') OR (name= 'Bruce')));

Can prove this

2 Comments

I think so but prove this
When u create a table and put constraints i think so but i dont sure. If not u can prove make a function for restrinctions

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.