Home > database >  Oracle to write time judgment conditions
Oracle to write time judgment conditions

Time:09-29

If now I have a createtime column, I want to let time data can be imported in the 1990-1-1-1990-1-1, in this time outside the scope of the data cannot be imported, can realize? (can be a very newbie question )

CodePudding user response:

Consider using the check?

CodePudding user response:

 
SQL>
SQL> Create table test (id int, createtime date);
The Table created
SQL> The alter table test add constraint ck_cdate
2 check (createtime between the date '1990-01-01' and the date '2015-12-31');
Table altered
SQL> - success
SQL> Insert into test values (1, the date '1995-02-05');
1 row inserted
SQL> Insert into test values (2, the date '2014-02-05');
1 row inserted
SQL> - the following two failure
SQL> Insert into test values (3, the date '2025-02-05');
Insert into test values (3, the date '2025-02-05')
ORA - 02290: a violation of check constraint conditions (ORACLE. CK_CDATE)
SQL> Insert into test values (4, the date '1900-02-05');
Insert into test values (4, the date '1900-02-05')
ORA - 02290: a violation of check constraint conditions (ORACLE. CK_CDATE)
SQL> Select * from the test;
ID CREATETIME
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1 1995/2/5
2 2014/2/5
SQL> Drop table test purge;
Table dropped

SQL>

CodePudding user response:

The alter table test add constraint ck_cdate
Check (createtime between the date '1990-01-01' and the date '2015-12-31');
  • Related