gpt4 book ai didi

sql - 将非整数插入整数列时如何强制违反约束?

转载 作者:行者123 更新时间:2023-12-02 01:23:06 26 4
gpt4 key购买 nike

正如您从下面的示例中看到的那样,我知道如何使用检查约束强制执行范围以及如何强制列仅包含整数。

让我感到困惑的是,当插入非整数时,Oracle 不会引发违反约束的错误,而是默默地将数字四舍五入为整数。

如何防止插入非整数数值?我想要违反约束而不是插入。

在 11g 上工作,但如果 12c(和 future 版本)的答案不同,也欢迎这些答案。

示例:

create table so59_t (
id number(38)
,a number(38)
,constraint a_ch check (a > 0)
);

--
-- the following values raise a constraint violation as expected
--
SQL> insert into so59_t values(1, -1);
insert into so59_t values(1, -1)
*
ERROR at line 1:
ORA-02290: check constraint (TEST.A_CH) violated

SQL> insert into so59_t values(2, 0);
insert into so59_t values(2, 0)
*
ERROR at line 1:
ORA-02290: check constraint (TEST.A_CH) violated

--
-- the following values are silently rounded and successfully inserted
-- however I'd like to have a constraint violation instead
--
SQL> insert into so59_t values(3, 1.4);

1 row created.

SQL> insert into so59_t values(4, 1.5);

1 row created.

SQL> select * from so59_t;

ID A
---------- ----------
3 1
4 2

SQL>

最佳答案

创建支票

constraint a_int check (a = ROUND(a))

编辑:在发布之前应该阅读语法图...

编辑:按照 sanjay radadiya 的建议将列创建为 number(38,1)

关于sql - 将非整数插入整数列时如何强制违反约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38781170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com