Deferrable, case-insensitive unique constraint

前端 未结 1 540
遇见更好的自我
遇见更好的自我 2020-12-16 14:00

Is it possible in PostgreSQL to create a deferrable unique constraint on a character column, but case-insensitive?

Let\'s assume the following basic table:



        
相关标签:
1条回答
  • 2020-12-16 14:35

    You can circumvent the restriction by using the special type citext provided by the additional module of the same name. Quoting the manual:

    The citext module provides a case-insensitive character string type, citext. Essentially, it internally calls lower when comparing values. Otherwise, it behaves almost exactly like text.

    It addresses your case exactly. Run once per database:

    CREATE EXTENSION citext;
    

    Then you can:

    CREATE TABLE sample_table ( 
       my_column citext
      ,CONSTRAINT my_unique_constraint UNIQUE(my_column)
       DEFERRABLE INITIALLY IMMEDIATE
    );
    
    0 讨论(0)
提交回复
热议问题