Oracle数据库无符号整数

问题描述 投票:0回答:2

我也想知道,如果Oracle数据库支持unsigned int(number),我可以如何使用它,如果不支持,有什么办法可以替代呢?

oracle oracle11g oracle10g oracle-sqldeveloper
2个回答
1
投票

我不认为Oracle为无符号整数提供了一个特定的数据类型。它提供了一个单一的数据类型来存储固定的数值,称为 NUMBER,其精度和规模可以根据需要进行调整。

在Oracle中,所谓的 INT datatype是一个为ANSI兼容性而提供的语法糖,它内部映射为 NUMBER.

我会推荐一个带有 0 scale(那是一个整数),以及一个检查约束来确保它是正数。

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

2
投票

There's no 无符号整数 作为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>
© www.soinside.com 2019 - 2024. All rights reserved.