1

Here's the table schema:

create table EMPLOYEE
(fname /* Employee's first name */ varchar(15) not null,
init /* Employee's middle initial */ char(1),
lname /* Employee's last name */ varchar(15) not null,
IRD /* Employee's IRD number */ varchar(10) not null primary key, sex /* Employee's sex */ char(1)
constraint check_sex check (sex in ('f','m','F','M')),
bdate /* Employee's birthdate */ date,
office /* Employee's office */ varchar(5),
reg_org /* The number of the registration office the employee works for */
varchar(10),
sdate /* Starting date in the organization */ date);

Then I think it make sense to add the constraint

alter table employee
add constraint ck_date check (sdate >= bate);

but it gives me an error

add constraint ck_date check (sdate >= bdate)
               *
ERROR at line 2:
ORA-02293: cannot validate (SYSTEM.CK_DATE) - check constraint violated

Anyone know where I go wrong?


update the data of the two columns

SQL> select bdate, sdate from employee;

BDATE     SDATE
--------- ---------
21-JAN-58 22-FEB-10
21-MAY-70 17-MAR-09
09-NOV-47 12-MAY-08
10-OCT-53 15-JUN-09
01-OCT-56 01-OCT-05

SQL> select to_char(bdate, 'yyyy-mm-dd') as bdate, to_char(sdate,'yyyy-mm-dd') from employee;

BDATE      TO_CHAR(SD
---------- ----------
1958-01-21 2010-02-22
1970-05-21 2009-03-17
2047-11-09 2008-05-12
1953-10-10 2009-06-15
1956-10-01 2005-10-01


4

1 回答 1

1

问题是您的表中可能已经有一些数据,当您尝试创建该约束时,数据库正在尝试验证已存储在表中的值。似乎有一些sdate比。<bdate

如果您不希望在创建过程中验证约束,请使用:

ALTER TABLE employee ADD CONSTRAINT ck_date CHECK (sdate >= bdate) NOVALIDATE;

编辑

正如我在您的数据中可以看到的那样,您有,例如,21-JAN-5822-FEB-10- 我想BDATE应该是21-JAN-1958,和sdate- 22-FEB-2010,对吗?如果您有任何机会使用 RR 格式来指定年份,那么如果年份小于 50,您会得到年复一年2000,如果值更高,那么您会得到介于1950和之间的年份1999,请查看:

SELECT TO_CHAR(TO_DATE('01-01-49', 'DD-MM-RR'), 'DD-MM-YYYY') AS year_in_2000,
       TO_CHAR(TO_DATE('01-01-50', 'DD-MM-RR'), 'DD-MM-YYYY') AS year_before_2000 FROM dual;

输出:

YEAR_IN_2000 YEAR_BEFORE_2000
------------ ----------------
01-01-2049 01-01-1950  
于 2013-11-05T08:52:27.417 回答