Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL unsigned integer

Tags:

sql

oracle11g

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.

like image 895
Velizar Hristov Avatar asked May 02 '13 12:05

Velizar Hristov


People also ask

Does SQL have unsigned int?

What is UNSIGNED INT Data Type? When we set the MySQL INT UNSIGNED column, it means that negative values cannot be inserted in this column. The maximum range value with the UNSIGNED data type is 4294967295, while the minimum one is 0.

What does int unsigned mean in SQL?

The “unsigned” in MySQL is a data type. Whenever we write an unsigned to any column that means you cannot insert negative numbers. Suppose, for a very large number you can use unsigned type. The maximum range with unsigned int is 4294967295. Note: If you insert negative value you will get a MySQL error.

What does <> mean in Oracle?

It means 'not equal to'.

Can integer be null in Oracle?

You can insert null or blank values in string and integer fields in expressions. By default, when you insert a string or integer object into an expression, the value is null .


2 Answers

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
like image 65
Alex Poole Avatar answered Oct 14 '22 08:10

Alex Poole


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).

like image 27
Colin 't Hart Avatar answered Oct 14 '22 07:10

Colin 't Hart