Section 4: Persistent Data Storage
Chapter 39: MySQL Data Types
The next few pages will likely be a little dry. Apologies now. However, whenever we create a table structure in MySQL we must identify the data type we intend to store in any given column, and depending on the type of data and other features we may want, this is just the beginning. Since one of our goals in the relational approach to database design is reducing overall size, it is also important to consider the best fit data type for what we want to store. Familiarizing yourself with the types available in MySQL will lend to your ability to design efficient table structures. The tables below are adopted from http://www.w3resource.com/mysql/mysql-data-types.php. They have been trimmed down in an attempt to not introduce an overwhelming amount of detail. You are encouraged to review the original version for more depth.
Table 14 MySQL Data Types
Integer Types
Type |
Length in Bytes |
Minimum Value(Signed/Unsigned) |
Maximum Value(Signed/Unsigned) |
TINYINT |
1 |
-128 to 0 |
127 to 255 |
SMALLINT |
2 |
-32768 to 0 |
32767 to 65535 |
MEDIUMINT |
3 |
-8388608 to 0 |
8388607 to 16777215 |
INT |
4 |
-2147483648 to 0 |
2147483647 to 4294967295 |
BIGINT |
8 |
-9223372036854775808 to 0 |
9223372036854775807 to 18446744073709551615 |
Floating-Point Types
Types |
Description |
FLOAT |
A precision from 0 to 23 results in a four-byte single-precision FLOAT column. |
DOUBLE |
A precision from 24 to 53 results in an eight-byte double-precision DOUBLE column. |
Fixed-Point Types
Types |
Description |
DECIMAL |
In the format DECIMAL(precision,scale). Maximum number of digits allowed are 65 before MySQL 5.03 and 64 after 5.03. |
NUMERIC |
Same as DECIMAL. |
Bit Value Types
Types |
Description |
BIT |
In the format b BIT(N), where N is an integer. |
Numeric type Attributes
Types |
Description |
TYPE(N) |
Where N is an integer and display width of the type is up to N digits. |
ZEROFILL |
The default padding of spaces is replaced with zeroes. So, for a column INT(3) ZEROFILL, 7 is displayed as 007. |
DATETIME, DATE, and TIMESTAMP Types
Types |
Description |
Display Format |
Range |
DATETIME |
Use when you need values containing both date and time information. |
YYYY-MM-DD HH:MM:SS |
‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. |
DATE |
Use when you need only date information. |
YYYY-MM-DD |
‘1000-01-01’ to ‘9999-12-31’. |
TIMESTAMP |
Values are converted from the current time zone to UTC while storing, and converted back from UTC to the current time zone when retrieved. |
YYYY-MM-DD HH:MM:SS |
‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC. |
String Types
Types |
Description |
CHAR |
Contains non-binary strings. Length is fixed as you declare while creating a table. When stored, they are right-padded with spaces to the specified length. |
VARCHAR |
Contains non-binary strings. Columns are variable-length strings. |
BINARY and VARBINARY Types
Types |
Description |
Range in bytes |
BINARY |
Contains binary strings. |
0 to 25. |
VARBINARY |
Contains binary strings. |
A value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. |
BLOB and TEXT Types
Types |
Description |
Categories |
Range. |
BLOB |
Large binary object that containing a variable amount of data. Values are treated as binary strings. You do not need to specify length while creating a column. |
TINYBLOB |
Maximum length of 255 characters. |
MEDIUMBLOB |
Maximum length of 16777215 characters. |
||
LONGBLOB |
Maximum length of 4294967295 characters. |
||
TEXT |
Values are treated as character strings having a character set. |
TINYBLOB |
Maximum length of 255 characters. |
MEDIUMBLOB |
Maximum length of 16777215 characters. |
||
LONGBLOB |
Maximum length of 4294967295 characters. |
ENUM Types
A string object whose value is chosen from a list of values given at the time of table creation. For example:
- ENUM(‘small’, ‘medium’, ‘large’)
SET Types
A string object having zero or more comma separated values (maximum 64). Values are chosen from a list of values given at the time of table creation.