问题
I added a table in oracle in oracle with fields -
- smallint
- int
- bigint
But it internally store them as decimal(22,0)
Is there any way to get individual subtype (e.g. smallint) instead of decimal.
My usecase:
I am using sqoop to copy data from oracle to hive. I need to check metadata of columns of oracle table and wrote own oracle to hive data type mapping.
But I am always getting decimal(22,0), so I need to map all these columns to decimal(22,0) in hive table even for smallint.
回答1:
The data type SMALLINT and INT are just ANSI/ISO standard types which, in Oracle, are aliases for NUMBER(38) and the BIGINT datatype does not exist.
If you want to store 2-byte, 4-byte and 8-byte values then you can use a combination of appropriately sized NUMBER columns and constraint checking to ensure the data is within the required bounds:
CREATE TABLE table_name (
small NUMBER( 5,0) CHECK ( small BETWEEN -POWER(2,15) AND +POWER(2,15)-1 ),
medium NUMBER(10,0) CHECK ( medium BETWEEN -POWER(2,31) AND +POWER(2,31)-1 ),
big NUMBER(19,0) CHECK ( big BETWEEN -POWER(2,63) AND +POWER(2,63)-1 )
);
INSERT INTO table_name VALUES ( -POWER(2,15), -POWER(2,31), -POWER(2,63) );
INSERT INTO table_name VALUES ( POWER(2,15)-1, POWER(2,31)-1, POWER(2,63)-1 );
Then:
SELECT * FROM table_name
Outputs:
SMALL MEDIUM BIG
------ ----------- --------------------
-32768 -2147483648 -9223372036854775808
32767 2147483647 9223372036854775807
来源:https://stackoverflow.com/questions/37276568/find-numeric-subtype-smallint-int-etc-in-oracle