Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>(We could debate use of synthetic keys versus natural keys here, but that's another subject.)</p> <pre><code>card (cardid, suitid, rank) suit (suitid, suitname) hand (handid, playername, whatever) handcard (handcardid, handid, cardid) </code></pre> <p>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.</p> <p>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. </p> <p>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: </p> <pre><code>select playername from hand join handcard using (handid) join card using (cardid) join suit using (suitid) where suit.name='Spades' and card.rank=13 </code></pre> <p>Or, "How many face cards does each player have?"</p> <pre><code>select playername, count(*) from hand join handcard using (handid) join card using (cardid) where card.rank between 11 and 13 </code></pre> <p>Of course "What cards does player #3 have?" is also easy:</p> <pre><code>select rank, suitname from handcard join card using (cardid) join suit using (suitid) where handid=3 </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload