如何确保 Column_A 只有在 Column_B 为 NULL 时才有值?反之亦然

How can I ensure that Column_A can only have a value if Column_B is NULL? And vice versa

我正在尝试创建一个包含三列的 table:

id

paid_at

failed_at

如何确保 paid_at 只有在 failed_atNULL 时才有值?

这是我当前的代码:

 CREATE TABLE charges(
      id        TEXT     NOT NULL     PRIMARY KEY,
      paid_at   TEXT,
      failed_at TEXT
    );

    ALTER TABLE charges
      ADD CONSTRAINT paid_at CHECK (failed_at IS NULL);

    ALTER TABLE charges
      ADD CONSTRAINT failed_at CHECK (paid_at IS NULL);

我还想确保 BOTH 不能为空。

我该怎么做?

谢谢!

您可以使用以下谓词:

alter table charges add constraint exclusive_rule check (
  paid_at is null and failed_at is not null or
  paid_at is not null and failed_at is null
);

您可以在检查约束中使用num_nonnulls()

alter table charges 
  add constraint only_one_not_null 
  check (num_nonnulls(paid_at, failed_at) = 1);

确保其中一列不为空,另一列为空。

如果您认为只有空格的字符串也是 "null",您可以将其扩展为:

alter table charges 
  add constraint only_one_not_null 
  check (num_nonnulls(nullif(trim(paid_at),''), nullif(trim(failed_at),'')) = 1);

我倾向于加法。要检查一组列中的一个不为空,请计算非空值的数量:

check ( (paid_at is not null)::int + (failed_at is not null)::int) > 0 )