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: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.usage origin flowering ripe picking status rootstock is_triploid
Option 1
Create a table for each field:
The problem I have with this set up is that to do a search forCREATE 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) );
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.
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)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'
Option 2
Create one table for all fields:
The advantage I see with this is that I will only need to query the one table to perform the same search:CREATE TABLE tree_field_index ( field varchar(100), id int, name varchar(100), PRIMARY KEY (field,name,id), );
Any advice on the right path to take, or some other solution, greatly appreciated.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')
In reply to OT - SQL choosing a schema for index tables by bangor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |