Howard Zoopaloopa
Howard Zoopaloopa

Reputation: 3822

Database Cross Table Association Logic

I've got two tables in my db. One called forms and one called form_fields. form_fields will contain a list of fields that any form may have... first_name, last_name, email, address, hieght, weight, etc... forms will be a list of unique forms that may or may not contain any number of those fields in form_fields.

So, my question is, what is the standard practice for using the data in form_fields to relate to forms?

Should I

a) Create a column in forms called field_array and create an array of indexes that relate to the index numbers (primary key) of each field in form_fields?

or

b) Create a relation table and insert rows of two columns for every form field chosen. One column being the form unique index and the other being the chosen form field unique index?

I feel like having a field in forms filled up with comma delimited numbers is a little weird. I've been at this a few years now, independently, so I'm not always positive if there is a tried and true method for these types of cross table associations.

Thanks in advance.

Upvotes: 0

Views: 244

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753930

You should go with the association or relation or junction table approach.

Your new table might be:

CREATE TABLE Forms_Using_Fields
(
    FormID    INTEGER NOT NULL REFERENCES Forms(Id),
    FieldID   INTEGER NOT NULL REFERENCES Form_Fields(Id),
    PRIMARY KEY (FormId, FieldId),
    Seqno     INTEGER NOT NULL,  -- Sequence number of field on form
    ...
);

Upvotes: 2

Related Questions