如何使行中的列唯一

How to make columns within a row unique

假设我在 postgresql 数据库中有一个 Room 实体,它包含

Room {
  id:uuid, //PK of Table
  participant1: uuid, //foreign key to a user entity
  participant2: uuid  //foreign key to a user entity
}

我想确保记录中的 participant1 和 participant2 值不应该相同。

使用检查约束:

create table room
(
  id integer primary key, 
  participant1 uuid, 
  participant2 uuid, 
  constraint unique_participant 
    check (participant1 <> participant2)
);

以上将允许任何列或两者中的 NULL 值。