微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Oracle 中创建自定义 CHECK

如何解决如何在 Oracle 中创建自定义 CHECK

我必须为我的项目创建一个数据库,我目前正在努力创建自定义检查。 我想对学生个人身份号码 (pesel) 创建自定义检查,我希望它有 11 个字符长,并且只有 0-9 之间的数字,我还想对邮政编码创建检查以具有以下格式:“11- 111 英寸。 我在 Oracle sql 开发人员工作。

这些是我尝试添加的检查,但我在它们中都有相同的错误

ALTER TABLE Address ADD CHECK (Zip_Code ~ '^[0-9]{2}-[0-9]{3}');

ALTER TABLE Students ADD CHECK (pesel ~ '^[0-9]*$');

这是我收到的错误

Error starting at line : 1 in command -

ALTER TABLE Adress ADD CHECK (Zip_Code ~ '^[0-9]{2}-[0-9]{3}')

Error report -

00911. 00000 -  "invalid character"

*Cause:  
  The identifier name started with an ASCII character other than a
           letter or a number. After the first character of the identifier
           name,ASCII characters are allowed including "$","#" and "_".
           Identifiers enclosed in double quotation marks may contain any
           character other than a double quotation. Alternate quotation
           marks (q'#...#') cannot use spaces,tabs,or carriage returns as
           delimiters. For all other contexts,consult the sql Language
           Reference Manual.

*Action:  
 Check the Oracle identifier naming convention. If you are
           attempting to provide a password in the IDENTIFIED BY clause of
           a CREATE USER or ALTER USER statement,then it is recommended to
           always enclose the password in double quotation marks because
           characters other than the double quotation are then allowed.

解决方法

如果模式正确,您将使用正则表达式,例如

ALTER TABLE Address ADD CHECK (regexp_like(address,'^[0-9]{2}-[0-9]{3}'));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。