Page 382 - Beginning PHP 5.3
P. 382
Part III: Using PHP in Practice
MySQL Data Types
When you create a database table — which you do later in the chapter — the type and size of each field
must be defined. A field is similar to a PHP variable except that you can store only the specified type and
size of data in a given field. For example, you can ’ t insert characters into an integer field. MySQL
supports three main groups of data types — numeric, date/time, and string — which are outlined in the
following sections.
The descriptions here are fine for everyday use, but they ’ re not complete. For full details see the MySQL
manual at http://dev.mysql.com/doc/ .
This book assumes that you ’ re using MySQL version 5 (the current version at the time of writing).
Numeric Data Types
You can store numbers in MySQL in many ways, as shown by the following table. Generally speaking, you
should pick the data type most suited for the type of numbers you need to store.
Numeric Data Type Description Allowed Range of Values
TINYINT Very small integer – 128 to 127, or 0 to 255 if UNSIGNED
SMALLINT Small integer – 32768 to 32767, or 0 to 65535 if
UNSIGNED
MEDIUMINT Medium - sized integer – 8388608 to 8388607, or 0 to 16777215
if UNSIGNED
INT Normal - sized integer – 2147483648 to 2147483647, or 0 to
4294967295 if UNSIGNED
BIGINT Large integer – 9223372036854775808 to
9223372036854775807, or 0 to
18446744073709551615 if UNSIGNED
– 38
FLOAT Single - precision floating - Smallest non - zero value: ± 1.176 × 10 ;
38
point number largest value: ± 3.403 × 10
DOUBLE Double - precision floating - Smallest non - zero value: ± 2.225 × 10 ;
– 308
308
point number largest value: ± 1.798 × 10
DECIMAL( precision , Fixed - point number Same as DOUBLE , but fixed - point
scale rather than floating - point. precision
)
specifies the total number of allowed
digits, whereas scale specifies how
many digits sit to the right of the
decimal point.
BIT 0 or 1 0 or 1
344
9/21/09 9:11:08 AM
c12.indd 344 9/21/09 9:11:08 AM
c12.indd 344