Create a new table to contain groups of values:
CREATE TABLE values ( id SERIAL, group INT NOT NULL, value TEXT NOT NULL, label TEXT NOT NULL, PRIMARY KEY (id), UNIQUE (group, value) );
For instance:
INSERT INTO values (group, value, label) VALUES (1, 'NY', 'New York'); INSERT INTO values (group, value, label) VALUES (1, 'CA', 'California'); INSERT INTO values (group, value, label) VALUES (1, 'FL', 'Florida');
So, group 1 contains three possible values โโfor the dropdown selector. Then your form table can refer to which group the particular column is using.
Note that you must add fields to the form through rows, not columns. Ie, your application should not adjust the scheme when adding new forms, it should only create new lines. So make each field its own line:
CREATE TABLE form ( id SERIAL, name TEXT NOT NULL, PRIMARY KEY (id) ); CREATE TABLE form_fields ( id SERIAL, form_id INT NOT NULL REFERENCES form(id), field_label TEXT NOT NULL, field_type INT NOT NULL, field_select INT REFERENCES values(id), PRIMARY KEY (id) ); INSERT INTO form (name) VALUES ('new form'); $id = last_insert_id() INSERT INTO form_fields (form_id, field_label, field_type) VALUES ($id, 'age', 'text'); INSERT INTO form_fields (form_id, field_label, field_type) VALUES ($id, 'profession', 'text'); INSERT INTO form_fields (form_id, field_label, field_type) VALUES ($id, 'salary', 'text'); INSERT INTO form_fields (form_id, field_label, field_type, field_select) VALUES ($id, 'state', 'select', 1);