如何在 Oracle 中创建自定义 CHECK

How to create a custom CHECK in Oracle

我必须为我的项目创建一个数据库,我目前正在努力创建自定义检查。 我想创建学生个人身份号码 (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}'));