sql - PostgreSQL 检查约束是否相关列可为空

标签 sql postgresql database-design check-constraints

我有下面的示例表定义。是否可以创建一个检查约束,其中受影响的列可以为空?

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

我计划暗示对 completed_date 进行约束检查,以便输入值不会小于registered_date。这可能吗?

最佳答案

是的。检查约束自动接受 NULL 值。 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.

我的粗体强调。

关于sql - PostgreSQL 检查约束是否相关列可为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342536/

相关文章:

sql - 如何使用带有过滤条件where子句的oracle外连接

java - 使用 JPA 创建对象并将其保存到表中,但将表名称作为参数

子查询的 SQL IN 运算符值

postgresql - `RETURN TABLE(columns )` be equivalent to using one or more ` OUT` parameters, and marking the function as returning `SETOF` record?

sql - 使用递增引用更新 postgresql 表列

node.js - Sequelize JS 构建关联

sql - 为 SELECT 和 UPDATE 锁定表中的行

php - 需要自动递增,但多个条目必须使用 MySQL 分配相同的 int

Mysql外键

sql - ADO Connection.Execute 不断返回关闭的记录集