我已创建CHECK约束但收到错误消息

DKC*_*oat 2 sql oracle

create sequence student_studentid_seq
increment by 10
start with 100
nocycle;

create table student 
(studentid number(10),
name varchar2(30) not null,
ss# number(9) unique,
gpa number(2,3) not null,
  constraint student_studentid_pk PRIMARY KEY (studentid),
  constraint student_gpa_ck CHECK (GPA >= 0) );

insert into student (studentid, name, ss#, gpa)
              values(student_studentid_seq.NEXTVAL,'Draze Katan', 323456789,1);

receiving error message:
Error starting at line 29 in command:
insert into student (studentid, name, ss#, gpa)
              values(student_studentid_seq.NEXTVAL,'Draze Katan', 323456789,1)
Error report:
SQL Error: ORA-01438: value larger than specified precision allowed for this column
01438. 00000 -  "value larger than specified precision allowed for this column"
*Cause:    When inserting or updating records, a numeric value was entered
           that exceeded the precision defined for the column.
*Action:   Enter a value that complies with the numeric column's precision,
           or use the MODIFY option with the ALTER TABLE command to expand
           the precision.
Run Code Online (Sandbox Code Playgroud)

因此,出现错误消息是针对下一个约束:约束student_gpa_ck CHECK(GPA> = 0)); 在插入语句中,如果我为GPA输入'0'将插入raw,但是我将收到更多错误消息.

这是我的一个练习题,我无法弄清楚.我只需要提示错误不是全分辨率.如果你能帮帮我的话.

Ale*_*sej 5

问题在于您创建表的方式,特别是在列中GPA.

您正在使用number(2, 3),看起来像"构建一个包含2位总数和3位小数"的数字.

oracle文档中,您可以找到有关NUMBER数据类型,其属性以及类似number(2,3)含义的更好解释:

使用以下格式指定定点数:

NUMBER(p,s)其中:

p是精确值或有效十进制数的最大数,其中最高有效位是最左边的非零数字,最低有效数字是最右边的已知数字.Oracle保证数字的可移植性,精度最高可达20个基数-100位,相当于39或40个十进制数字,具体取决于小数点的位置.

s是从小数点到最低有效位的小数位数或位数.比例范围可以从-84到127.

正比例是小数点右边的有效数字,包括最低有效数字.

负标度是小数点左边的有效位数,但不包括最低有效位.对于负比例,最小有效数字位于小数点的左侧,因为实际数据四舍五入到小数点左侧的指定位数.例如,(10,-2)的规范意味着舍入到数百.

比例可以大于精度,最常见的是使用e表示法.当scale大于precision时,precision指定小数点右侧的最大有效位数.例如,定义为NUMBER(4,5)的列要求小数点后第一个数字为零,并将所有值舍入小数点后的第五个数字.

例如:

SQL> create table tabError( a number (2, 3));

Table created.

SQL> insert into tabError values (1);
insert into tabError values (1)
                             *
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column


SQL> insert into tabError values (0.1);
insert into tabError values (0.1)
                             *
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column


SQL> insert into tabError values (0.01);

1 row created.
Run Code Online (Sandbox Code Playgroud)

如果整数部分需要2位数而小数位需要3位数,那么number(5, 3)根据Mathguy的注释,你需要或者,如果你需要一个整数位和2位小数的数字,你需要number(3,2).