2

I created a table called customer, which consists of one attribute called email.

Then I would like to add a constraint to email attribute. Following is my coding:

Create table customer ( email varchar(100));

Here is my constraint: "@" must be included in the email

However, I don't know how to add this constraint. I am total new to psql. Thank you very much!

1
  • 2
    You may be interested in the answers on this question that deals with the best way to store email addresses in PostgreSQL
    – Hannah Vernon
    Commented Aug 14, 2017 at 13:59

1 Answer 1

3

Something like:

ALTER TABLE customer 
    ADD CONSTRAINT email_chk CHECK ( email like '%@%' );

should work. If email is mandatory you should make it NOT NULL as well. For a more thorough handling of the email-domain, check out the link:

What is the best way to store an email address in PostgreSQL?

that @MaxVernon posted in his comment.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.