Note that there are some explanatory texts on larger screens.

plurals
  1. PORelational database design - I want an extra field for some row types but not all
    primarykey
    data
    text
    <p>This a very basic database design/normalisation question. </p> <p>Suppose I have a <code>Books</code> table with the following columns:</p> <pre><code>isbn|title|author|status </code></pre> <p>and <code>status</code> can be one of <code>checked out</code>, <code>available</code>, <code>overdue</code>, <code>lost</code> (stored as integers).</p> <p>When adding rows I decide "actually, when the status is <code>checked out</code>, I want to store another field <code>due_date</code>". <em>I only want to store this field for books with status checked out</em>, as it has no meaning otherwise. </p> <p><strong>What is the standard, correct, canonical way to do this?</strong></p> <p>One approach is to add the column and set it to <code>NULL</code> if the status is not <code>checked out</code>, but this sounds like a bad idea to me (for integrity among other things, e.g. what if the status is <code>available</code> and we also have a <code>due_date</code>?)</p> <p>The other obvious answer is to create a <code>DueDates</code> table and store <code>isbn|due_date</code> pairs in it. This is the approach I normally take but it's easy to end up with tables and <code>JOIN</code>s all over the place. </p> <p>I am not looking for how to store books specifically, that's just an example of the problem and I want to know the standard solution.</p> <p><strong>Edit</strong>: Does the answer change if I decide that I want to add <em>lots</em> of fields for <code>checked out</code> status only (<code>due_date</code>, <code>borrowed_by</code>, <code>checked_out_from</code>, ...) - and have all these as <code>NULL</code> if the status is not <code>checked out</code>?</p>
    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.
 

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