oracle数据库无符号整数

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

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

ibrsph3r

ibrsph3r1#

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

  1. create table mytable (
  2. id number(20, 0) check (id >= 0)
  3. );
bgibtngc

bgibtngc2#

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

  1. SQL> create table test (id int);
  2. Table created.
  3. SQL> insert into test (id) values (-1);
  4. 1 row created.
  5. SQL> insert into test (id) values (25.335);
  6. 1 row created.
  7. SQL> select * From test;
  8. ID
  9. ----------
  10. -1
  11. 25
  12. SQL>

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

  1. SQL> truncate table test;
  2. Table truncated.
  3. SQL> alter table test add constraint ch_id_pos check (id >= 0);
  4. Table altered.
  5. SQL> insert into test (id) values (-1);
  6. insert into test (id) values (-1)
  7. *
  8. ERROR at line 1:
  9. ORA-02290: check constraint (SCOTT.CH_ID_POS) violated
  10. SQL>
展开查看全部

相关问题