Unique constraint for permutations across multiple columns

前端 未结 4 1464
抹茶落季
抹茶落季 2021-01-17 18:33

Given the following three columns in a Postgres database: first, second, third; how can I create a constraint such that permutations are unique?

E.g. If (\'foo

4条回答
  •  [愿得一人]
    2021-01-17 19:07

    Suggestion from co-worker, variation of @julien's idea:

    Sort the terms alphabetically and place a delimiter on either side of each term. Concatenate them and place them in a separate field that becomes the primary key.

    Why the delimiter? So that, "a", "aa", "aaa" and "aa", "aa", "aa" can both be inserted.

提交回复
热议问题