In Postgres, how do you restrict possible values for a particular column?

后端 未结 3 760
刺人心
刺人心 2021-01-30 08:09

I want to create a column element_type in a table (called discussion) that allows the text values \"lesson\" or \"quiz\" but will generate an error if

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 08:53

    A shorcut syntax is :

    ALTER TABLE distributors  
       ADD CONSTRAINT check_types 
       CHECK (element_type IN ('lesson', 'quiz') );
    

    This translates automaticolly to :

    CONSTRAINT check_types CHECK (element_type::text = ANY (ARRAY['lesson'::character varying, 'quiz'::character varying) )
    

    Enjoy ;-)

提交回复
热议问题