I have a table that needs each row to store an ordered collection of rows of the same type.
e.g., I am imagining something similar to:
class blah
{
float yetMoreData;
};
class foo
{
int data;
String moreData;
blah[] anOrderedCollectionOfBlahClasses;
};
What would be the best way to achieve this?
You want
BlahandFooto be separate tables, whereBlahhas a many-to-one relationship withFoo. The records in theBlahtable should have a foreign keyfoo_id, identifying their parent Foo record.Blahalso has anindex_in_arrayattribute that allows the results to be sorted.If you want to get all the
Blahs of a givenFoo(say, theFoowith the ID of 5), use:If you’re ordering records, usually it shouldn’t be by something arbitrary (unless that arbitrary order was chosen specifically by the user, e.g., sorting the items on a to-do list). Normally, you sort on some “naturally-occurring” property of the data, like the date it was added or alphabetically by name, etc.
N.B.: This answer is meant to be pseudocode SQL — make sure you adapt it to your specific database syntax before you toss it into a script. (And don’t forget to add some keys and indexes!)