Find numeric subtype (smallint, int, etc) in oracle

不羁岁月 提交于 2019-12-11 07:29:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!