oracle sql 检查数字 to_char 的约束
oracle sql check constraint for number to_char
这是我在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
的第二行抛出 "missing right parenthesis" 错误。我在别处读到这是语法的一般错误,但对于我来说,即使我将源 material 中的代码复制并粘贴到 SQL 工作表中或现在重新输入大约 20 次,我不断收到错误。
我还尝试转换为 char,因为 regno 是一个数字。
Alter table student add constraint b2 check (length(to_char(regno)<=4));
但是我得到了同样的错误。
你的比较运算符(<=
)应该在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)
干杯!!
这是我在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
的第二行抛出 "missing right parenthesis" 错误。我在别处读到这是语法的一般错误,但对于我来说,即使我将源 material 中的代码复制并粘贴到 SQL 工作表中或现在重新输入大约 20 次,我不断收到错误。
我还尝试转换为 char,因为 regno 是一个数字。
Alter table student add constraint b2 check (length(to_char(regno)<=4));
但是我得到了同样的错误。
你的比较运算符(<=
)应该在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)
干杯!!