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
The possibilities are:
VARCHAR2
(example 1080::8:800:200c:417a
)NUMBER
data typeINTEGER
data typeRAW
value
RAW(4)
or RAW(16)
for IPv4 or IPv6 respectivelyRAW(1)
or 8 x RAW(2)
for IPv4 or IPv6 respectivelyI 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.