简体   繁体   中英

Check constraint for Emails in an Oracle Database

I've searched everywhere for a decent and logical CHECK constraint to validate that an email is in the right format. So far I've found really long and unnecessary expressions like:

create table t (
email varchar2(320) check (
regexp_like(email, '[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]')
 )
);

and

create table stk_t (
email varchar2(320) check (
email LIKE '%@%.%' AND email NOT LIKE '@%' AND email NOT LIKE '%@%@%'
 )
);

Surely there is a simpler way? I'm using Oracle 11g database and SQL Developer IDE. This is what I have:

constraint Emails_Check check (Emails LIKE '%_@%_._%')

Can someone please let me know if this is the most efficient way of validating emails?

You can try this

email varchar2(255) check (
email LIKE '%@%.%' AND email NOT LIKE '@%' AND email NOT LIKE '%@%@%'   )
CREATE TABLE MYTABLE(
  EMAIL VARCHAR2(30) CHECK(REGEXP_LIKE (EMAIL,'^[A-Za-z]+[A-Za-z0-9.]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$'))
)

Explanation of Regular Expression
^           #start of the line
  [_A-Za-z0-9-]+    #  must start with string in the bracket [ ], must contains one or more (+)
  (         #  start of group #1
    \\.[_A-Za-z0-9-]+   #     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )*            #  end of group #1, this group is optional (*)
    @           #     must contains a "@" symbol
     [A-Za-z0-9]+       #        follow by string in the bracket [ ], must contains one or more (+)
      (         #      start of group #2 - first level TLD checking
       \\.[A-Za-z0-9]+  #        follow by a dot "." and string in the bracket [ ], must contains one or more (+)
      )*        #      end of group #2, this group is optional (*)
      (         #      start of group #3 - second level TLD checking
       \\.[A-Za-z]{2,}  #        follow by a dot "." and string in the bracket [ ], with minimum length of 2
      )         #      end of group #3
$           #end of the line

Stumbled upon this answer while hunting for a simple solution on the internet:

ALTER TABLE YourTableName
ADD CONSTRAINT YourConstraintName CHECK(YourColumnName LIKE '%___@___%.__%')

All points to @bhanu_nz here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM