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!

  • 2
    You may be interested in the answers on this question that deals with the best way to store email addresses in PostgreSQL – Max Vernon Aug 14 '17 at 13:59
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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