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!

filiprem
  • 6,747
  • 1
  • 19
  • 32

1 Answers1

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.

Lennart - Slava Ukraini
  • 23,842
  • 3
  • 34
  • 72