In Oracle, what is the equivalent to MySQL's UNSIGNED?
I have the following query, only it doesn't work:
CREATE TABLE foo
(
id INT UNSIGNED NOT NULL PRIMARY KEY
);
Edit: My purpose is to save space, because for some fields I won't be using negative values. Alternatively, it would answer my question of someone confirms that what I'm asking for is impossible in Oracle 11g, or if it's possible, but not straightforward to do (more than 3 lines of code per unsigned int).
Also, It's not necessarily about int. I also use smallint and tinyint.
If you want to match the restrictions shown here, you can use a check constraint:
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
Here http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm#i54330 is the definitive list of Oracle's built in datatypes.
If you want the semantics of integers, then number(n) is basically your only option. Personally I use integer which is a slightly shorter way of saying number(38).
来源:https://stackoverflow.com/questions/16338044/oracle-sql-unsigned-integer