SQL reference (MySQL and MariaDB dialects)

BIT type

BIT(M) columns of 1 to 64 bits, how they are written, shown and compared.

Syntax#

SQL
BIT [(M)]

M is the number of bits, 1 to 64; the default is 1.

Remarks#

  • A BIT value is kept as an unsigned whole number and sent to clients as the bytes of the number (column type 16), as MySQL does, so drivers read it as bits.
  • Write a value as b'101', as a number (5), or 0. DEFAULT b'0' is read. A bit literal is the number it spells (see Literals).
  • Compare and calculate with numbers: flag = 1, wide > 100, flag + 0. HEX(col) shows the value in hexadecimal.
  • A value with more bits than the column holds is error 1406 (Data too long for column), in strict and non-strict mode alike.
  • BIT(0) and BIT(65) and wider are error 1439 (The display width for column 'x' is out of range (maximum 64)).
  • A BIT column is read as unsigned. information_schema.COLUMNS shows column_type bit(12) and data_type bit with a numeric_precision equal to the width.

Examples#

SQL
CREATE TABLE b (id INT PRIMARY KEY, flag BIT(1) DEFAULT NULL, wide BIT(12) NOT NULL DEFAULT b'0', tiny bit(3));
INSERT INTO b VALUES (1, b'1', b'101010101010', b'111'), (2, b'0', 0, NULL), (3, NULL, 4095, b'10');
SELECT id, HEX(flag), HEX(wide), HEX(tiny), flag + 0, wide + 0, flag = 1, wide > 100 FROM b ORDER BY id;
INSERT INTO b (id, wide) VALUES (4, 5000);     -- error 1406: 5000 does not fit 12 bits
CREATE TABLE bad (x BIT(65));                  -- error 1439

Example (the output is identical to MySQL 9.7).

See also#