I have the sample table definition below. Is it possible to create a check constraint wherein the column affected can be nullable?

CREATE TABLE task (
    # other definition
    registered_date timestamp without time zone NOT NULL,
    completed_date timestamp without time zone
    # constraints
);

I was planning to imply a constraint check on completed_date so that the input value won't be lesser than registered_date. Is this possible?

有帮助吗?

解决方案

Yes. Check constraints accept NULL values automatically. Per documentation:

It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value. Since most expressions will evaluate to the null value if any operand is null, they will not prevent null values in the constrained columns. To ensure that a column does not contain null values, the not-null constraint described in the next section can be used.

Bold emphasis mine.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top