in reply to Storing Complex Structures in a database

Ovid and AgentM are definitely on the right track.

However, if all you want to do is take a perl datastructure, and then save it into a DB for later ressurection you should look at Storable, FreezeThaw and Data::Dumper. The process is called serilization and is very DB independant. Just serilize your data structure and shove it in a VARCHAR field. You can retrieve and re-inflate it from that field later. However, this is only an apropriate solution if you do not need to access the contents of the structure until you can "inflate" it back into perl.

  • Comment on Re: Storing Complex Structures in a database

Replies are listed 'Best First'.
RE: Re: Storing Complex Structures in a database
by jreades (Friar) on Oct 02, 2000 at 20:05 UTC

    VARCHAR would probably not be a good idea here as the data is likely to exceed 255 characters (MySQL restriction) pretty quickly.

    If the post-er wants to serialize, I'd imagine that blob or text would be a more flexible solution and without the 255 character restriction.

    Personally, I'd go with a single table approach (assuming that each node has only a single parent) using a parent node id field...

    nodes
    node_id (UNIQUE INT)
    parent_node_id (INT)
    data (whatever is relevant)

    That's just an off-the-cuff response, but as your nodes and subnodes theoretically contain the same information I wouldn't go sticking them in different tables. This way works by simply adding a parent_node_id column that would allow you look either up or down the node tree any arbitrary amount. It's not necessarily the fastest way (because of the multiple selects to move up or down the tree, so as the nodes reach 'n' levels they take, what is it, O(n) time) but it represents a relatively consistent interface that won't throw you into problems with your database.

    A question of tradeoffs I suppose.

      I should have been clearer. When I said VARCHAR what I meant was "some sort of character storage that is of variable length" not necessarily the VARCHAR type. The exact name of the apropriate type will vary based on your database and your storage needs.