Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top