简体   繁体   English

oracle sql检查约束条件满足

[英]oracle sql check constraint condition met

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. 例如,如果我不想在特定日期允许1100至1300之间的约会时间。

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. 但这显然会限制所有1100至1300之间的约会时间以及4月2日的所有约会。

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. 我反对使用此命令:“ '02-APR-2014'因为它可能取决于NLS设置。

Or just change and condition to or in your attempt. 或者只是改变and条件, or在你的尝试。 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM