检查Oracle数据库中电子邮件的约束

时间:2017-05-11 04:36:16

标签: sql regex oracle constraints email-validation

我到处寻找一个体面的,合乎逻辑的CHECK约束来验证电子邮件的格式是否正确。到目前为止,我发现了很长很多不必要的表达方式,如:

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

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

当然有一种更简单的方法吗? 我正在使用Oracle 11g数据库和SQL Developer IDE。 这就是我所拥有的:

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

如果这是验证电子邮件的最有效方式,有人可以告诉我吗?

3 个答案:

答案 0 :(得分:1)

你可以试试这个

email varchar2(255) check (
email LIKE '%@%.%' AND email NOT LIKE '@%' AND email NOT LIKE '%@%@%'   )

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在互联网上寻找一个简单的解决方案时偶然发现了这个答案:

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

所有指向@bhanu_nz here