1

Beginner question: Is there a way to write a check constraint that restricts data where a condition is met. For instance, if I want to not allow appointment times between 1100 and 1300 on a specific day.

ALTER TABLE appointment_info
 ADD CONSTRAINT ap_time_ck CHECK (ap_time NOT BETWEEN 11 and 13);

I tried

ALTER TABLE appointment_info
 ADD CONSTRAINT ap_time_ck CHECK (ap_time NOT BETWEEN 11 and 13 AND ap_date != '02-APR-2014');

But obviously that will restrict all appointment times between 1100 and 1300 and any appointments on April 2nd.

1
  • thats what i thought but my check constraint is violated because i have an appointment set for april 2nd at 1400. Commented Mar 10, 2014 at 14:59

1 Answer 1

1

That's possible solution:

ALTER TABLE appointment_info
ADD CONSTRAINT ap_time_ck CHECK (not (ap_time BETWEEN 11 and 13 and ap_date = date '2014-04-02'));

I am against using this: '02-APR-2014' because it can depend on NLS settings.

Or just change and condition to or in your attempt. It would also work.

ALTER TABLE appointment_info
ADD CONSTRAINT ap_time_ck CHECK (ap_time NOT BETWEEN 11 and 13 OR ap_date != '02-APR-2014');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I used the first solution you gave.

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.