Note that there are some explanatory texts on larger screens.

plurals
  1. POSchema for storing "binary" values, such as Male/Female, in a database
    primarykey
    data
    text
    <h2>Intro</h2> <p>I am trying to decide how best to set up my database schema for a (Rails) model. I have a model related to money which indicates whether the value is an income (positive cash value) or an expense (negative cash value).</p> <p>I would like separate column(s) to indicate whether it is an income or an expense, rather than relying on whether the value stored is positive or negative.</p> <hr> <h2>Question:</h2> <p>How would you store these values, and why?</p> <ul> <li>Have a single column, say <code>Income</code>, and store <code>1</code> if it's an income, <code>0</code> if it's an expense, <code>null</code> if not known.</li> <li>Have two columns, <code>Income</code> and <code>Expense</code>, setting their values to <code>1</code> or <code>0</code> as appropriate. </li> <li>Something else?</li> </ul> <p>I figure the question is similar to storing a person's gender in a database (ignoring aliens/transgender/etc) hence my title.</p> <hr> <h2>My thoughts so far</h2> <ul> <li>Lookup might be easier with a single column, but there is a risk of mistaking <code>0</code> (<code>false</code>, expense) for <code>null</code> (unknown).</li> <li>Having seperate columns might be more difficult to maintain (what happens if we end up with a <code>1</code> in both columns?</li> </ul> <p>Maybe it's not that big a deal which way I go, but it would be great to have any concerns/thoughts raised before I get too far down the line and have to change my code-base because I missed something that should have been obvious!</p> <p>Thanks, Philip</p>
    singulars
    1. This table or related slice is empty.
    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