是否可以创建电子邮件约束为“@”的 table 检查是为了确保电子邮件具有 @ 符号

Is it possible to create a table with an email constraint of '@' The check is to make sure the email has an @ symbol

CREATE TABLE Member
(
    user_ID              NUMBER NOT NULL ,
    user_password        VARCHAR2(50) NULL  CONSTRAINT  Email_153906048 CHECK (user_password IN ('%@%')),
    user_email           VARCHAR2(50) NULL  CONSTRAINT  Email_1275780631 CHECK (user_email IN ('%@%')),
    user_firstName       VARCHAR2(20) NULL ,
    user_lastName        VARCHAR2(20) NULL ,
    user_type            VARCHAR2(8) NULL  CONSTRAINT  Type_1346517516 CHECK (user_type IN ('Artist', 'Follower')),
CONSTRAINT  XPKUser PRIMARY KEY (user_ID)
);

是的。您可以创建一个触发器,对 @ 符号进行验证检查。

像这样:

CREATE OR REPLACE TRIGGER members_bi
BEFORE INSERT
   ON members
   FOR EACH ROW
BEGIN
   IF INSTR(:new.user_email, '@') = 0 then
      raise_application_error(-20101, 'email must contain AT character');
   end if;
END;

/

这将检查它是否包含至少一个“@”:

CHECK (user_email LIKE '%@%')

确保'@'两边都有东西:

CHECK (user_email LIKE '%_@_%')

可以使用正则表达式进行更具体的检查。

为了补充 Tony 的正确答案,如果您决定要进行更多检查,则可以使用多个子句扩展检查约束,例如:

CHECK(
    user_email like '%@%.%'   -- Must contain at least one @ and one subsequent .
and user_email NOT like '%..%'        -- Cannot have two periods in a row
and user_email NOT like '%@%@%'       -- Cannot have two @ anywhere
and user_email NOT like '%.@%'AND user_email NOT like '%@.%' -- Cant have @ and . next to each other
and replace(translate(user_email,'[ &'',":;!=\/()<>]*%','                    '),' ') = user_email -- check for invalid characters
)

不过不要对规则太可爱,因为你不想不小心排除有效地址。