title: Why does query which subtracts the value of an unsigned int column fails in certain cases?
body: <p>I have a table with 2 columns <code>id</code> and <code>myvalue</code>. The <code>id</code> is primary key and the <code>myvalue</code> is an <code>unsigned smallint</code>. The current <code>myvalue</code> for <code>id</code> 1 is 0. I have the below sql query:</p>
  
  <pre><code>UPDATE tablename SET myvalue = IF(myvalue - 1 &gt; 0, myvalue - 1, 0) WHERE id = 1;
  </code></pre>
  
  <p>I get the below error:</p>
  
  <pre><code>ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`dbname`.`tablename`.`myvalue` - 1)'
  </code></pre>
  
  <p>Any idea why this happens? I could not find any online resources regarding this.</p>
  
  <p>The same query works if I alter the table to remove unsigned from the <code>myvalue</code> column.</p>