Oracle 数据库无符号整数
Oracle Database unsigned integer
我也想知道,如果 Oracle 数据库支持 unsigned int(number) 我该如何使用它,如果不支持,还有什么替代方案?
我不需要为 SQL 语法设置条件的方法,因为我的所有数据都是正数,并且它对于性能和存储来说很重要。
我认为 Oracle 没有为无符号整数提供特定的数据类型。它提供单一数据类型来存储固定数值,称为NUMBER
,其精度和小数位数可以根据需要进行调整。
在 Oracle 中,所谓的 INT
数据类型是为 ANSI 兼容性提供的语法糖,内部映射到 NUMBER
.
我会推荐一个具有 0
比例的数字(这是一个整数),以及一个检查约束以确保它是正数:
create table mytable (
id number(20, 0) check (id >= 0)
);
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>
我也想知道,如果 Oracle 数据库支持 unsigned int(number) 我该如何使用它,如果不支持,还有什么替代方案? 我不需要为 SQL 语法设置条件的方法,因为我的所有数据都是正数,并且它对于性能和存储来说很重要。
我认为 Oracle 没有为无符号整数提供特定的数据类型。它提供单一数据类型来存储固定数值,称为NUMBER
,其精度和小数位数可以根据需要进行调整。
在 Oracle 中,所谓的 INT
数据类型是为 ANSI 兼容性提供的语法糖,内部映射到 NUMBER
.
我会推荐一个具有 0
比例的数字(这是一个整数),以及一个检查约束以确保它是正数:
create table mytable (
id number(20, 0) check (id >= 0)
);
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>