I need to store Java's java.math.BigInteger
numbers in SQL. I can safely assume that the numbers will be positive and less than 2^k
for some k
that will be known at table creation time. Lets say k = 256
.
How do I store it in an RDBMS such that it allows me comparisons (<, >, =
etc).
I am currently converting to byte array and storing them as VARBINARY(k+1)
.
I can also store them as VARCHAR
if that helps. I need a DBMS independent way to do it.
EDIT1: following up to the comment by JBNizet below. I need to do the following types of SQL queries:
SELECT * FROM myTable WHERE bigInteger > 100
UPDATE myTable SET bigInteger = bigInteger + 10 WHERE bigInteger = 123
EDIT2: It is possible to compare VARCHAR
with lexicographic ordering with some effort. However, the increment gives weird behavior given below (tested on SQLite):
CREATE TABLE T(Id INTEGER PRIMARY KEY, Name VARCHAR, test VARCHAR);
INSERT INTO T VALUES(1,'Tom', '000030000000000000000000000000');
SELECT * FROM T;
1|Tom|000030000000000000000000000000
UPDATE T SET test = test+1;
SELECT * FROM T;
1|Tom|3.0e+25