Representing IPv4/IPv6 addresses in Oracle

前端 未结 7 2158
清歌不尽
清歌不尽 2020-12-06 10:27

In Oracle, what is the appropriate data type or technique for representing network addresses, which addresses may be IPv4 or IPv6?

Background: I\'m converting a tab

相关标签:
7条回答
  • 2020-12-06 11:14

    The possibilities are:

    • Store as string, i.e. VARCHAR2 (example 1080::8:800:200c:417a)
    • Store as numeric value
      • NUMBER data type
      • INTEGER data type
    • Store as RAW value
      • One RAW value, i.e. RAW(4) or RAW(16) for IPv4 or IPv6 respectively
      • 4 x RAW(1) or 8 x RAW(2) for IPv4 or IPv6 respectively

    I would recommend to use RAW values because

    • If you use strings then you have to consider different formats of IPv6.

      1080::8:800:200C:417A
      1080::8:800:200c:417a
      1080::8:800:32.12.65.122
      1080:0:0:0:8:800:200C:417A
      1080:0:0:0:0008:0800:200C:417A
      1080:0000:0000:0000:0008:0800:200C:417A
      

      are all legal representations of the same IPv6 IP-Address. Your application needs to enforce a common format for proper usage, e.g. use in WHERE condition.

    • NUMBER/INTEGER values are senseless without conversion to human-readable format. You cannot use INTEGER data type in PL/SQL

      i INTEGER := 2**128-1; -- i.e. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
      
      -> ORA-06502: PL/SQL: numeric or value error: number precision too large. 
      
    • In case you have to work with subnetting you cannot use function BITAND - it also supports numbers only up to 2^127

    • You can use UTL_RAW functions UTL_RAW.BIT_AND, UTL_RAW.BIT_COMPLEMENT, UTL_RAW.BIT_OR for subnet operations.

    • In case you have to deal with really big amount of data (I am talking about billions of rows) it might be beneficial to split the IP-Address into several RAW values, i.e. 4 x RAW(1) or 8 x RAW(2). Such columns would be predestinated for Bitmap-Indexes and you would save a lot of disc space and gain performance.

    0 讨论(0)
提交回复
热议问题