Email Column Constraint to Contain ‘@’ character

constraintoracle-12c

How do you create an inline constraint for a Email Address Field that requires there be an "@" character? Pretty much I want there to be a restriction to the field so that the data entered must have the "@" within it. I'm using Oracle SQL Developer 12c.

Best Answer

Try this:

CREATE TABLE myTable ( email VARCHAR2(254) );
ALTER TABLE myTable ADD CONSTRAINT chk_email_format CHECK ( REGEXP_LIKE(EMAIL, '.{1,64}@.{1,255}'));

INSERT INTO myTable (email) VALUES ('abc abc.com')

Error report - SQL Error: ORA-02290: check constraint (FLASHCARD.CHK_EMAIL_FORMAT) violated

254 total characters is from Errata ID: 1690 with 64 before the @ and up to 255 after it.

This simple regexp, email address must have a @, will alert you to attempts to insert wildly wrong data (perhaps helping catch a bug or two). However, building a complete RFC 5322 validator is a difficult task. For details take a look here:

https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address