Postgresql – Adding constraint on data

constraintpostgresqlpsql

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!

Best Answer

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.