Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do not serialize your arrays to strings. This is one of the common mistakes people make when storing lists in databases (although to be fair it's better than making a fixed number of columns for the first n items).</p> <p>I would make a separate <code>options</code> table where each row has two columns: <code>(question_id, option)</code> where <code>question_id</code> is a foreign key into your table of questions. The query would look something like,</p> <pre><code>SELECT option FROM options WHERE question_id = &lt;&lt;$id&gt;&gt;; </code></pre> <p>(&lt;> isn't actual SQL, just a placeholder for whatever the PHP parameterized query syntax is.)</p> <p>If the options need to be ordered, add a third column <code>rank</code>:</p> <pre><code>SELECT option FROM options WHERE question_id = &lt;&lt;$id&gt;&gt; SORT BY rank ASCENDING; </code></pre> <p><strong>Edit: Why not serialize arrays into strings?</strong></p> <p>This isn't something I've studied in depth, but off the top of my head I can think of performance, convenience, and theory:</p> <ol> <li><p>For performance reasons, databases are optimized to store fields of fixed width. If you start putting lists into fields, eventually you'll encounter a list with too many items, and need to resize the field. Meanwhile, databases are highly optimized to store tables of arbitrarily many rows, and you never need to resize the table to accommodate more rows.</p></li> <li><p>As a matter of convenience, it's really annoying to run queries against lists that have been serialized into a single field. Most databases have very poor support for querying into lists stored as fields. From the database's perspective, that's just a string or maybe even an opaque binary blob.</p></li> <li><p>From a theoretical perspective, it violates the fiction that relational databases are reifications of the relational algebra. But this turns out to have practical consequences, because the query optimizer uses equivalence theorems from the relational algebra to rewrite your query into a more performant form. Violating the assumptions of the relational algebra tends to generate queries that optimize poorly (although never incorrectly).</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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