简体   繁体   English

Oracle检查约束违规

[英]Oracle Check Constraint violation

I created a constraint on the date column of the table X . 我在表X的日期列上创建了约束。 When I created a check constraint on date as date > '01-jan-2000' there is an error message (" Check constraint violated "). 当我按date > '01-jan-2000'创建日期的检查约束时,出现错误消息(“ 检查约束违反 ”)。 When I tried again to check the date if it comes after 1996 ( date > '01-jan-1996' ), it worked well. 当我再次尝试检查日期是否在1996年之后( date > '01-jan-1996' )时,它运行良好。

Is there a reason for it? 有什么理由吗?

I used this code: 我使用以下代码:

ALTER TABLE X 
ADD CONSTRAINT DATE_CONST CHECK(DATE>'01-JAN-2000')     --  ERROR

ALTER TABLE X 
ADD CONSTRAINT DATE_CONST CHECK(DATE>'01-JAN-1996')     --  WORKED

there are two things you are done wrong. 您做错了两件事。 a) You can not assing name to column keyword like "DATE". a)您不能将名称与“ DATE”之类的列关键字相关联。 b) you must to convert date when you add CONSTRAINT like this : b)当添加这样的CONSTRAINT时,必须转换日期:

ALTER TABLE test ADD CONSTRAINT DATE_CONST CHECK(testdate>to_date('01-JAN-2000', 'dd-mon-yyyy'));

http://sqlfiddle.com/#!4/98198/9 is example of this question. http://sqlfiddle.com/#!4/98198/9是此问题的示例。

Edit : If your table has record before specified date, Check constraint will be compiled but it will be invalid. 编辑:如果您的表在指定日期之前有记录,则将编译Check约束,但是它将无效。 You must be update before specied date records to newly date. 您必须在指定日期记录到新日期之前进行更新。

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

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