Toggle a TINYINT Field in MySQL

31st December 2007

MySQL uses the datatype TINYINT to store boolean values. MySQL stores the value as TINYINT(1) which is the same as a bit so the value is either 0 (false) or 1 (true). Using boolean fields can be very useful, but it can be costly in processing as to change the value you have to query the database, find out the value of the field and then act accordingly.

Here is a simple MySQL query that can be used to toggle the value already present in the TINYINT field without having to do any pre-querying.

UPDATE TABLE SET FIELD = 1 - FIELD

Comments

Permalink
works with bool even thanks

Anonymous (Fri, 08/27/2010 - 14:26)

Add new comment

The content of this field is kept private and will not be shown publicly.