0

我为加拿大邮政编码创建了一个约束,它工作正常,但是当我输入一个美国邮政编码时1234567,记录仍然被添加。我正在寻找一种方法来改变我的约束,所以它只接受5 数字

Postal_Code varchar2(7) Constraint NN_Investor_PostalCode Null,
Constraint CK_Investor_PostalCode check 
(REGEXP_LIKE (Postal_Code, '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]') 
    or REGEXP_LIKE (Postal_Code, '[1-9][0-9][0-9][0-9][0-9]')), 
4

1 回答 1

0

你可以试试这个

REGEXP_LIKE (Postal_Code, '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]') 
        or ( REGEXP_LIKE (Postal_Code, '^[1-9][0-9]{4}') and length(Postal_Code)=5

作为检查约束。

示例演示:

with t(Postal_Code) as
(
 select '12345'   from dual union all
 select '32045'   from dual union all 
 select '1234567' from dual union all
 select '123456'  from dual union all
 select  '01234'  from dual 
)
select * 
  from t
 where REGEXP_LIKE (Postal_Code, '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]') 
    or ( REGEXP_LIKE (Postal_Code, '^[1-9][0-9]{4}') and length(Postal_Code)=5 );

POSTAL_CODE
----------- 
  12345
  32045
于 2018-09-21T07:28:07.340 回答