Search
Tweets
Blogroll
Powered by Squarespace
« Amazon announces EC2 | Main | Beatmaster Bush »
Wednesday
Aug232006

SQL repeating sets

I have the need to store sets in an RDBMS, but SQL doesn’t lend itself well to repeating elements.

For example, picture having to store a bunch of name/value pairs on a person. Initial try may conclude a Non-First Normal Form of type

PersonSkills
Alicec=ok;java=good,verifiedBy=Sun,verifiedDate=[timestamp]
Bobc=good;c++=excellent
Malloryc=good;hacking=excellent,verifiedby=cloodyHatSociety

This of course doesn’t scale at all. And parsing weird name/value pair lists inside a string? You might as well store it as an XML blob.

If you’re stuck with a relational database, you may want to break it up at least till 3NF. I have recently done a lot of Ruby on Rails and have come to like the auto increment primary key counter Rails uses everywhere.

IdPerson
1Alice
2Bob
3Mallory

Users, primary key Id

IdStatementVerifierVerification date
1S1nullnull
1S2V1timestamp
2S3nullnull
2S4nullnull
3S3nullnull
3S5V2null

Verifications, primary key (Id, Statement)

IdName
1V1Sun
2V2cloudyHatSociety

Verifiers, primary key Id

IdNameValue
1S1cok
2S2javagood
3S3cgood
4S4c++excellent
5S5hackingexcellent

Statements, primary key Id

This seems a very simple and usable solution.

Reader Comments (1)

One afternoon, I was in the backyard hanging the laundry when an old, tired-looking dog wandered into the yard. I could tell from his collar and well-fed belly that he had a home. But when I walked into the house, he followed me, sauntered down the hall and fell asleep in a corner. An hour later, he went to the door, and I let him out. The next day he was back. He resumed his position in the hallway and slept for an hour.
This continued for several weeks. Curious, I pinned a note to his collar: "Every afternoon your dog comes to my house for a nap. "
The next day he arrived with a different note pinned to his collar: "He lives in a home with ten children - he's trying to catch up on his sleep."

I cried from laughter
Sorry, if not left a message on Rules.
May 2, 2008 | Unregistered CommenterMelissik

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.