[Solved] Storing searchable Arrays in a SQL-database field


With Oracle, you can store arrays in a column using NESTED TABLEs (or VARRAYs):

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TYPE String_Table IS TABLE OF VARCHAR2(100)
/

CREATE TABLE test (
  id     NUMBER(10,0),
  col1   VARCHAR2(10),
  array1 String_Table
) NESTED TABLE array1 STORE AS test__array1
/

INSERT INTO test ( id, col1, array1 )
  SELECT 1, 'Row1', String_Table( 'A', 'B', 'C' ) FROM DUAL UNION ALL
  SELECT 2, 'Row2', String_Table( 'C', 'D', 'E' ) FROM DUAL
/

Query 1: Then you can use collection operations such as: MEMBER OF to find items in a collection; and MULTISET operators like SUBMULTISET OF to find collections containing all items of another collection.

SELECT *
FROM   test
WHERE  'B' MEMBER OF array1
OR     String_Table( 'E', 'C' ) SUBMULTISET OF array1

Results:

| ID | COL1 | ARRAY1 |
|----|------|--------|
|  1 | Row1 |  A,B,C |
|  2 | Row2 |  C,D,E |

If you are using Java then you can pass Java arrays as bind parameters of a PreparedStatement or CallableStatement. Some examples of this are here and here.

solved Storing searchable Arrays in a SQL-database field