约束检查范围浮动

use*_*209 2 sql oracle constraints

我的sql代码有问题(使用oracle).

我正在努力限制身高,身高应在1,0到2,4之间.数据类型是浮点数.小数位用逗号分隔.这是代码:

alter table tableName
add constraint check_height
check (columnName between 1,0 and 2,4);
Run Code Online (Sandbox Code Playgroud)

我试图用逗号和点来划分代码中的小数位(我不能改变数据列表中的小数位数),我也尝试改变范围并用<>显示范围.到目前为止没有任何工作.有谁知道我错过了什么?

错误消息是:'检查约束违反'.

干杯

Ale*_*ole 5

如果表中的数据已经包含违反约束的值,则无法使用default validate子句创建它:

create table tableName(columnName number);

insert into tablename (columnName) values(2.5);

alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4);

SQL Error: ORA-02293: cannot validate (STACKOVERFLOW.CHECK_HEIGHT) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause:    an alter table operation tried to validate a check constraint to
           populated table that had nocomplying values.
*Action:   Obvious
Run Code Online (Sandbox Code Playgroud)

(我的领土有nls_numeric_characters='.,',所以我用过.而不是,).

您可以更正或删除无效值,也可以通过指定novalidate子句来仅在验证新数据时保留错误值:

alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4)
novalidate;

table TABLENAME altered.
Run Code Online (Sandbox Code Playgroud)

仍将验证新的插入或更新,这仅影响现有数据.