本当に素数を数値として保存したい場合、質問の1つとして「素数は因数に分解できない」ということです。別の方法があります。シーケンス順に並べられた任意の数のモジュラスのリストに保存します。
小さな例:
2831781 == 2*100^3 + 83*100^2 + 17*100^1 + 81*100^0
リストは次のとおりです。
81, 17, 83, 2
実際のアプリケーションでは、2^32 (32 ビット整数) のモジュラスで分割すると便利です。特に、処理アプリケーションで素数がバイト配列として格納されている場合に便利です。
DB への保存:
create table PRIMES
(
PRIME_ID NUMBER not null,
PART_ORDER NUMBER(20) not null,
PRIME_PART_VALUE NUMBER not null
);
alter table PRIMES
add constraint PRIMES_PK primary key (PRIME_ID, PART_ORDER) using index;
上記の例を挿入します(1647は例にすぎません):
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 0, 81);
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 1, 17);
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 2, 83);
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 3, 82);
prime_id 値は Oracle シーケンスから割り当てることができます ...
create sequence seq_primes start with 1 increment by 1;
挿入する次の素数の ID を取得します。
select seq_primes.nextval from dual;
指定された ID を持つ素数コンテンツを選択します。
select PART_ORDER, PRIME_PART_VALUE
from primes where prime_id = 1647
order by part_order