You shouldn't store arrays, or any kind of object, in a relational database. A relational database, to be in first normal form, should consist only of fields with basic datatypes. That is INT
, CHAR
, VARCHAR
, DECIMAL
etc. Additionally, it should contain no repeating groups, and the columns should have no intrinsic ordering. Arrays, by their very definition, violates this. If you feel the need to store other values, you have misunderstood the concept of a relational database.
If you want to store objects, such as arrays etc, then you should try some database system that is created for such use, i.e. Object Databases
The best way to store the array is to make an additional table for the array, and put each value of the array in its own row, then store some kind of array identifier in the parent table.
Example:
CREATE TABLE People (
ssn INT,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(20) NOT NULL,
PRIMARY KEY (ssn)
);
CREATE TABLE Emails (
belongs_to INT,
email VARCHAR(128),
PRIMARY KEY (belongs_to, email),
UNIQUE(email),
CONSTRAINT fk_belongs_to_ssn FOREIGN KEY (belongs_to) REFERENCES People (ssn)
);
In this schema, every person can have multiple emails. Each email belongs to a person in the People table. A set of emails can be found for a person by joining People with Emails on ssn = belongs_to. A single email can easily be found by searching the email-column for a particular value and/or pattern.