How can I create a constraint to check if an email is valid in postgres?

后端 未结 4 578
有刺的猬
有刺的猬 2020-12-25 12:02

How can I create a constraint to use a regular expression in postgres?

4条回答
  •  感动是毒
    2020-12-25 12:55

    CREATE TABLE emails (
        email varchar
        CONSTRAINT proper_email CHECK (email ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+[.][A-Za-z]+$')
    );
    

    (regex may be incomplete, you can search for regexp for email matching all over the web and pick the one you like best).

提交回复
热议问题