MySQL: The ignorance of Boolean

MySQL is not available natively BOOL data type, it (as BOOLEAN) is nothing but a synonym of another data type that actually exists: TINYINT (1); BOOL value to zero is considered false and values nonzero are considered “true”, false and true are only aliases (references) to real values “0″ and “1″.

To compensate for the lack of Boolean you can use a simple mechanism of emulation, so a statement like this:

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">ALTER TABLE tbl ADD COLUMN campo TINYINT(1) NOT NULL;</span> ALTER TABLE tbl ADD COLUMN field TINYINT (1) NOT NULL;</span>

It is nothing but an emulation of:

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">ALTER TABLE tbl ADD COLUMN campo BOOL NOT NULL;</span> ALTER TABLE tbl ADD COLUMN field BOOL NOT NULL;</span>

In this way, using TINYINT you can use the “0″ instead of false and “1″ instead of true, there are also inherent limitations in the process of emulation:

  • All other numbers are interpreted as other than false but not true;
  • A given type TINYINT occupies 1 byte while a BOOL should occupy 1 bit.

That creates some problems related to the optimization that can not be resolved before the introduction of this important type of data.

This entry was posted in Database. Bookmark the permalink.

Comments are closed.