Oracle中如何设置字段类型必须为正整数(无符号)
在用mysql的时候,我可以设置字段的类型为int(unsigned),用来保证些字段的值不能小于0
但是oracle里面,只有number类型,没有unsigned这个选项,那么我们要如何设置呢?
设置方法如下:
SQL> create table foo (id number primary key,
constraint foo_uint_id check (id between 0 and 4294967295));
Table created.
SQL> insert into foo (id) values (-1);
insert into foo (id) values (-1)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.FOO_UINT) violated
SQL> insert into foo (id) values (0);
1 row created.
SQL> insert into foo (id) values (4294967295);
1 row created.
SQL> insert into foo (id) values (4294967296);
insert into foo (id) values (4294967296)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.FOO_UINT_ID) violated
SQL> select * from foo;
ID
----------
0
4294967295