[英]How to create a constraint that will assure that the date entered is always >= today or a future date?
I'm created a table called Project with two columns ProjectID and StartDate. 我创建了一个名为Project的表,其中有两列ProjectID和StartDate。 I would like to create a constraint that will assure that the date entred is always equal to today or a future date.
我想创建一个约束,以确保所包含的日期始终等于今天或未来日期。
I have tried the code below but gets a missing expression error. 我已经尝试了下面的代码,但得到了一个缺少表达式错误。 The Constraint is only added after the creation of the table.
仅在创建表后添加约束。
CREATE TABLE PROJECT
(ProjectID NUMBER(7),
StartDate date NOT NULL,
CONSTRAINT PK_PROJECTID PRIMARY KEY (ProjectID)
);
ALTER TABLE PROJECT
ADD CONSTRAINT PROJECT_Check_StartDate CHECK(StartDate => Current_date);
Your proximal error is that the correct operator is >=
, not =>
. 您的近端错误是正确的运算符是
>=
,而不是=>
。
Fixing that problem will only generate a more existential error: 修复该问题只会产生更具存在性的错误:
ORA-02436: date or system variable wrongly specified in CHECK constraint
ORA-02436:CHECK约束中错误指定的日期或系统变量
That is, you cannot refer to a dynamic value in a check
constraint, and the current date/time is such a value. 也就是说,您不能在
check
约束中引用动态值,并且当前日期/时间就是这样的值。
The simplest option is to use a trigger to enforce the constraint. 最简单的选择是使用触发器来强制执行约束。 A "kind-of" crazy option is to store the create date of the record (which is a good idea) and then store the future portion as another column:
一种“种类”的疯狂选项是存储记录的创建日期(这是一个好主意),然后将未来部分存储为另一列:
create table . . . (
daysInTheFuture number, -- fractional days
CreatedAt date default sysdate,
constraint chk_t_number check (daysInTheFuture >= 0)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.