I'm working on a application that needs to store a deck of hands in database. I'm not sure whats is the best way to represent this in a database.
The deck will be pre-dealt to 4 hands with each holding 13 cards by the application. After that I need to store the hands and additional data like suit distribution etc...
Is it best to create a seperate row for each hand and associate it with a deck? Or maybe it would be better to keep them in one row?
Also I'm not sure if I should keep them as text or number.
Here's an example:
Sorted by suit and separated by dot
(52 = Largest rank or largest suit or 02 = 2 of smallest suit)
I'd think the most natural thing would be to create a table for cards, that would have one record for each card in the deck, and then a record for hands, that would have one record for each card in the hand. If there's data about the hand as a whole -- the name of the player? where he's sitting at the table? how many chips he has? whatever -- then you'd need a separate table for the hand as opposed to the cards in the hand. I'd also create a lookup table for suits.
(We could debate use of synthetic keys versus natural keys here, but that's another subject.)
card (cardid, suitid, rank) suit (suitid, suitname) hand (handid, playername, whatever) handcard (handcardid, handid, cardid)
My first impulse would be to make "rank" a number from 1 to 13, and translate 1 to "Ace" and 11 to 13 to "Jack", "Queen", and "King" at output time. You could store them as text, but then it would be difficult to compare ranks, if there is some need to do that. i.e. deciding if 13>10 is easy for the database; King>10, not so much.
I definitely would NOT try to cram identification of multiplpe cards into a single field, whether separated by periods or fixed length ids or whatever. Doing that would require your program to have code to take this field apart to get the data you want, instead of just letting the database retrieve the data you want, which is what databases are for.
I don't know what you want to do with these card hands, but most things you would be likely to want to do are pretty easy with a structure like I'm describing, fairly tough if you cram a list of cards into a single field. Like, "Who has the King of Spades?" Easy:
select playername from hand join handcard using (handid) join card using (cardid) join suit using (suitid) where suit.name='Spades' and card.rank=13
Or, "How many face cards does each player have?"
select playername, count(*) from hand join handcard using (handid) join card using (cardid) where card.rank between 11 and 13
Of course "What cards does player #3 have?" is also easy:
select rank, suitname from handcard join card using (cardid) join suit using (suitid) where handid=3
Doing those queries with a packed format would require tricky string extraction calls. And string manipulation is always a pain. Like you always run into problems like, if I'm searching for 2's in text, how do I exclude the 2's that are part of '12's? etc.