oracle数据库无符号整数

67up9zun  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(449)

我也想知道,如果oracle数据库支持unsigned int(number),我该如何使用它,如果不支持,还有什么替代方法呢?我不需要为sql语法设置条件,因为我所有的数据都是正数,无符号int对于性能和存储非常重要。

ibrsph3r

ibrsph3r1#

我不认为oracle为无符号整数提供了特定的数据类型。它提供了一个数据类型来存储固定的数值,称为 NUMBER ,其精度和刻度可根据需要进行调整。
在甲骨文中,所谓的 INT 数据类型是为ansi兼容性提供的一种语法糖,它在内部Map到 NUMBER .
我想推荐一个带有 0 scale(这是一个整数)和一个check约束来确保它是正的:

create table mytable (
    id number(20, 0) check (id >= 0)
);
bgibtngc

bgibtngc2#

在oracle中没有无符号整数作为本机数据类型。这就是 NUMBER 数据类型。但是,您可以使用 INT ,例如。

SQL> create table test (id int);

Table created.

SQL> insert into test (id) values (-1);

1 row created.

SQL> insert into test (id) values (25.335);

1 row created.

SQL> select * From test;

        ID
----------
        -1
        25

SQL>

如您所见,它同时接受正值和负值(小数被截断)。
要使其为正,请添加一个约束:

SQL> truncate table test;

Table truncated.

SQL> alter table test add constraint ch_id_pos check (id >= 0);

Table altered.

SQL> insert into test (id) values (-1);
insert into test (id) values (-1)

* 

ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_ID_POS) violated

SQL>

相关问题