简体   繁体   English

SQL oracle将检查约束添加到现有表

[英]SQL oracle add check constraint to an existing table

Im using SQL/PL developer and I have a table named Appeal, that has 2 attributs OpenDate and CloseDate. 我正在使用SQL / PL开发人员,并且我有一个名为Appeal的表,该表具有2个属性OpenDate和CloseDate。 And I want to add a constraint to ensure that open date will be smaller than close date. 我想添加一个约束以确保开放日期小于结束日期。 I have a lot of records in this table. 我在这张桌子上有很多记录。

this is my code: 这是我的代码:

alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate)

and I get en error saying: ORA-02293: cannot validate (STSTEM.CHECK_DATES) - check constraint violated 而我却收到一条错误消息:ORA-02293:无法验证(STSTEM.CHECK_DATES)-违反了检查约束

any ieads? 有没有小爱? Thanx 感谢名单

Your constraint looks right, I have tested it: 您的约束看起来不错,我已经测试过:

create table appeal ( OpenDate date,  CloseDate date);

alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate);

insert into appeal values ( sysdate, sysdate - 1 );

And here the result: 结果如下:

Schema Creation Failed: ORA-02290: check constraint (USER_4_44096.CHECK_DATES) violated 架构创建失败:ORA-02290:违反了检查约束(USER_4_44096.CHECK_DATES)

Problem is than you have already rows with OpenDate < CloseDate values in your database. 问题是您的数据库中已经有OpenDate <CloseDate值的行。 Fix it before create constraint. 在创建约束之前对其进行修复。 Look behavior changing sentences order: 看行为改变句子的顺序:

create table appeal ( OpenDate date,  CloseDate date);

insert into appeal values ( sysdate, sysdate - 1 );

alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate);

And here your issue: 这是您的问题:

Schema Creation Failed: ORA-02293: cannot validate (USER_4_E4450.CHECK_DATES) - check constraint violated 架构创建失败:ORA-02293:无法验证(USER_4_E4450.CHECK_DATES)-违反检查约束

Try this alter table appeal add constraint Check_Dates check (OpenDate < CloseDate) ENABLE NOVALIDATE; 尝试使用此变更表吸引力添加约束Check_Dates检查(OpenDate <CloseDate)ENABLE NOVALIDATE;

You will have check the previous data for errors but any new Data will fall under the CHECK 您将检查以前的数据是否有错误,但是任何新数据都将属于“检查”

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

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