使用转义字符的邮政编码的SQL检查声明

HBe*_*net 1 sql oracle escaping check-constraints

我试过看其他方面,但我似乎无法弄清楚我在做什么有什么问题.我试图在列上创建一个检查约束,该列应该包含格式为"00000-0000"的9位邮政编码.这是一个赋值,所以我唯一能做的就是创建一个检查约束.

我已经成功地将check语句用于其他列,但由于某种原因,我在Stack Overflow上找到的语句不起作用.唯一允许的字符是数字和连字符(' - ').

alter table Student
    add constraint student_zip_ck
    check (Zip not like '%[0-9\-]%' escape '\');
Run Code Online (Sandbox Code Playgroud)

由于此检查约束是在Stack Overflow上的另一个(正评级)问题之后建模的,所以我不知道可能出现什么问题.这是我收到的错误.

Error starting at line 751 in command:
alter table Student
  add constraint student_zip_ck
  check (Zip not like '%[0-9\-]%' escape '\')
Error report:
SQL Error: ORA-00604: error occurred at recursive SQL level 1
ORA-01424: missing or illegal character following the escape character
00604. 00000 -  "error occurred at recursive SQL level %s"
*Cause:    An error occurred while processing a recursive SQL statement
           (a statement applying to internal dictionary tables).
*Action:   If the situation described in the next error on the stack
           can be corrected, do so; otherwise contact Oracle Support.
Run Code Online (Sandbox Code Playgroud)

有没有人对我有任何建议,问题或评论?

Ben*_*Ben 6

你错过了与正则表达式相关的任何东西,这可能就是为什么它不起作用.你的语法意味着你打算使用它们,我会同意这种情况.自Oracle 10g以来,正则表达式已经可用,因此您必须确保使用此版本或更高版本.

语法将变为:

alter table Student
    add constraint student_zip_ck
    check (regexp_like(Zip,'^[[:digit:]]{5}-[[:digit:]]{4}$'));
Run Code Online (Sandbox Code Playgroud)

这意味着:

  • ^ - 固定到字符串的开头
  • [[:digit:]] - 仅接受数字值.这是符合POSIX标准的变体,相当于\d.
  • {5} - 完全5次
  • - - 匹配连字符
  • $ - 固定到字符串的末尾

要使连字符和第二个数字可选,您需要使用创建组().这使得括号内的所有内容都是单个单元,无论是字符串还是其他正则表达式,然后您可以将其他运算符应用于其中.A ?表示匹配0或1次,需要应用于该组.总而言之,你得到:

regexp_like(Zip,'^[[:digit:]]{5}(-[[:digit:]]{4})?$')
Run Code Online (Sandbox Code Playgroud)

进一步阅读

读这个(第一次)有一个相当类似的问题,例如3-1,它使用Perl类型的正则表达式语法\d而不是POSIX,可能是有意义的.