Dear Monks,
Apologies for being off-topic but I come here seeking advice as I know the depth of experience in the monastery (and everything else in the project is built with Perl).

I am creating a database for a heritage organisation who try to keep alive 100s of varieties of heirloom apple trees - the ones we will need when the climate goes really crazy!

The tree table has a number of fields like:
usage origin flowering ripe picking status rootstock is_triploid
Some of these fields can have multiple values, so to store them in an index I intend to create extra tables. I have come up with two options but am unsure which to choose.

Option 1
Create a table for each field:

CREATE TABLE tree_usage_index ( id int, name varchar(100), PRIMARY KEY (name,id) ); CREATE TABLE tree_flowering_index ( id int, name varchar(100), PRIMARY KEY (name,id) );
The problem I have with this set up is that to do a search for
trees with usage of 'cider' or usage of 'juicer' and with flowering of 'spring' and with ripe of 'autumn'
I will have to query multiple tables, and the code I have written for this seems a bit complex.
SELECT id from tree_usage_index WHERE name='cider' OR name='juicer' # if there are results SELECT id from tree_flowering_index WHERE id IN (results) and name='sp +ring' # if there are still results SELECT id from tree_ripe_index WHERE id IN (results) and name='autumn'
I know you can nest SQL selects but as there are 12 fields that could be involved in a query the SQL could get a bit hairy. Also, I will probably need to add an index to the 'id' field of each table to speed up the WHERE id IN (results)

Option 2
Create one table for all fields:

CREATE TABLE tree_field_index ( field varchar(100), id int, name varchar(100), PRIMARY KEY (field,name,id), );
The advantage I see with this is that I will only need to query the one table to perform the same search:
SELECT id from tree_field_index WHERE ( (field='usage' AND name='cider +') OR (field='usage' AND name='juicer') ) AND (field='flowering' AND +name='spring') AND (field='ripe' AND name='autumn')
Any advice on the right path to take, or some other solution, greatly appreciated.

In reply to OT - SQL choosing a schema for index tables by bangor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.