【问题标题】:oracle sql check constraint for number to_char数字 to_char 的 oracle sql 检查约束
【发布时间】:2020-05-09 07:23:12
【问题描述】:

这是我在oracle live sql网站上输入的作业问题:

Create table student (regno number (6), mark number (3) constraint b check (mark >=0 and
mark <=100));
Alter table student add constraint b2 check (length(regno<=4));

它不断在Alter 的第二行抛出“缺少右括号”错误。我在其他地方读到,这是语法的一般错误,但对于我的一生,即使我将源材料中的代码复制并粘贴到 SQL 工作表中,或者现在重新输入大约 20 次,我仍然会收到错误。

我还尝试转换为 char,因为 regno 是一个数字。

Alter table student add constraint b2 check (length(to_char(regno)<=4));

但我得到了同样的错误。

【问题讨论】:

  • 嗯,这只是家庭作业。否则,对数值的字符限制将毫无意义。照原样,length(to_char(regno)) &lt;= 4 表示regno &gt;= -999 and regno &lt;= 9999,如果我没记错的话,这是一个相当奇怪的限制:-)
  • 这个实验表的规格列出了 Windows NT 和 Windows 7,所以我只是向讲师抱怨了这一点以及问题中给定代码中的勘误表。

标签: oracle constraints ddl ora-00907


【解决方案1】:

你的比较运算符(&lt;=)应该在length函数之外:

SQL> CREATE TABLE STUDENT (
  2      REGNO   NUMBER(6),
  3      MARK    NUMBER(3)
  4          CONSTRAINT B CHECK ( MARK >= 0
  5                               AND MARK <= 100 )
  6  );

Table created.

SQL> -- Solution of the question
SQL> ALTER TABLE STUDENT
  2      ADD CONSTRAINT B2 CHECK ( LENGTH(REGNO) <= 4 );

Table altered.

SQL>

一个建议,如果您想将REGNO 限制为仅4 位数字,请将REGNO 的数据类型转换为NUMBER(4)

干杯!!

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多