Table Column Types for Bit Strings

This section describes 2 types of table columns to store bit strings: BIT and BIT VARYING.

There are 2 types of table columns to store bit strings: BIT and BIT VARYING.

"BIT(length)" - Bit string with fixed length.

"BIT VARYING(length)" - Bit string with varying length.

Examples of bit string column types, BitStringColumns.sql:

-- BitStringColumns.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
CREATE TABLE BitString (
   Line VARCHAR(2),
   A BIT(32),
   B CHAR(4)
--   C BIT VARYING(8) -- not supported by MySQL
   );
INSERT INTO BitString VALUES (
   '1',
   'AAA',
   'AAA'
   );
INSERT INTO BitString VALUES (
   '2',
   X'424242',
   X'424242'
   );
INSERT INTO BitString VALUES (
   '3',
   BINARY(X'434343'),
   BINARY(X'434343')
   );
SELECT * FROM BitString;
DROP TABLE BitString;

Run BitStringColumns.sql on MySQL 5.7 server, you will get:

C:\herong>set "MySQL=\Program Files\MySQL\MySQL Server 5.7"
C:\herong>"%MySQL%\bin\mysql" --user=root --password=TopSecret test 
   < BitStringColumns.sql

Line    A       B
1       \0AAA   AAA
2       \0BBB   BBB
3       \0CCC   CCC
5.6 Line A B 1 \0AAA AAA 2 \0BBB BBB 3 \0CCC CCC

Cool. My bit string examples are working.

Run BitStringColumns.sql on MySQL 5.6 and 5.0 servers, you will get the same result:

But run BitStringColumns.sql on MySQL 4.0 server, you will get:

Line    A       B
1       0       AAA
2       127     BBB
3       0       CCC

My bit string examples are not working on MySQL 4.0 server.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

Table Column Types for Different Types of Values

 Table Column Types for Character Strings

Table Column Types for Bit Strings

 Table Column Types for Exact Numbers

 Table Column Types for Approximate Numbers

 Table Column Types for Date and Time Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version