SELECT
and WHERE
Clauses
SELECT
, INSERT
, UPDATE
, DELETE
CREATE
, DROP
, ALTER
MySQL supports a number of column types, which may be grouped into three categories: numeric types, date and time types, and string (character) types. This section first gives an overview of the types available and summarises the storage requirements for each column type, then provides a more detailed description of the properties of the types in each category. The overview is intentionally brief. The more detailed descriptions should be consulted for additional information about particular column types, such as the allowable formats in which you can specify values.
The column types supported by MySQL are listed below. The following code letters are used in the descriptions:
M
D
M
-2.
Square brackets (`[' and `]') indicate parts of type specifiers that are optional.
Note that if you specify ZEROFILL
for a column, MySQL will
automatically add the UNSIGNED
attribute to the column.
Warning: you should be aware that when you use subtraction
between integer values where one is of type UNSIGNED
, the result
will be unsigned! See section 6.3.5 Cast Functions.
TINYINT[(M)] [UNSIGNED] [ZEROFILL]
-128
to 127
. The
unsigned range is 0
to 255
.
BIT
BOOL
BOOLEAN
TINYINT(1)
.
The BOOLEAN
synonym was added in version 4.1.0. A true boolean
type will be introduced in accordance with SQL-99.
SMALLINT[(M)] [UNSIGNED] [ZEROFILL]
-32768
to 32767
. The
unsigned range is 0
to 65535
.
MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL]
-8388608
to
8388607
. The unsigned range is 0
to 16777215
.
INT[(M)] [UNSIGNED] [ZEROFILL]
-2147483648
to
2147483647
. The unsigned range is 0
to 4294967295
.
INTEGER[(M)] [UNSIGNED] [ZEROFILL]
INT
.
BIGINT[(M)] [UNSIGNED] [ZEROFILL]
-9223372036854775808
to
9223372036854775807
. The unsigned range is 0
to
18446744073709551615
.
Some things you should be aware of with respect to BIGINT
columns:
BIGINT
or DOUBLE
values, so you shouldn't use unsigned big integers larger than
9223372036854775807
(63 bits) except with bit functions! If you
do that, some of the last digits in the result may be wrong because of
rounding errors when converting the BIGINT
to a DOUBLE
.
MySQL 4.0 can handle BIGINT
in the following cases:
BIGINT
column.
MIN(big_int_column)
and MAX(big_int_column)
.
+
, -
, *
, etc.) where
both operands are integers.
BIGINT
column by
storing it as a string. In this case, MySQL will perform a string-to-number
conversion that involves no intermediate double representation.
BIGINT
arithmetic when
both arguments are integer values! This means that if you
multiply two big integers (or results from functions that return
integers) you may get unexpected results when the result is larger than
9223372036854775807
.
FLOAT(precision) [UNSIGNED] [ZEROFILL]
precision
can be
<=24
for a single-precision floating-point number and between 25
and 53 for a double-precision floating-point number. These types are like
the FLOAT
and DOUBLE
types described immediately below.
FLOAT(X)
has the same range as the corresponding FLOAT
and
DOUBLE
types, but the display size and number of decimals are undefined.
In MySQL Version 3.23, this is a true floating-point value. In
earlier MySQL versions, FLOAT(precision)
always has 2 decimals.
Note that using FLOAT
may give you some unexpected problems as
all calculations in MySQL are done with double precision.
See section A.5.6 Solving Problems with No Matching Rows.
This syntax is provided for ODBC compatibility.
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
-3.402823466E+38
to -1.175494351E-38
, 0
,
and 1.175494351E-38
to 3.402823466E+38
. If
UNSIGNED
is specified, negative values are disallowed. The M
is the display width and D
is the number of decimals. FLOAT
without arguments or FLOAT(X)
where X
<= 24 stands for a
single-precision floating-point number.
DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
-1.7976931348623157E+308
to
-2.2250738585072014E-308
, 0
, and
2.2250738585072014E-308
to 1.7976931348623157E+308
. If
UNSIGNED
is specified, negative values are disallowed. The
M
is the display width and D
is the number of decimals.
DOUBLE
without arguments or FLOAT(X)
where 25 <= X
<= 53 stands for a double-precision floating-point number.
DOUBLE PRECISION[(M,D)] [UNSIGNED] [ZEROFILL]
REAL[(M,D)] [UNSIGNED] [ZEROFILL]
DOUBLE
.
DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
CHAR
column: ``unpacked'' means the number is stored as a string,
using one character for each digit of the value. The decimal point and,
for negative numbers, the `-' sign, are not counted in M
(but
space for these is reserved). If D
is 0, values will have no decimal
point or fractional part. The maximum range of DECIMAL
values is
the same as for DOUBLE
, but the actual range for a given
DECIMAL
column may be constrained by the choice of M
and
D
. If UNSIGNED
is specified, negative values are disallowed.
If D
is omitted, the default is 0. If M
is omitted, the
default is 10.
Prior to MySQL Version 3.23, the M
argument must include the space
needed for the sign and the decimal point.
DEC[(M[,D])] [UNSIGNED] [ZEROFILL]
NUMERIC[(M[,D])] [UNSIGNED] [ZEROFILL]
FIXED[(M[,D])] [UNSIGNED] [ZEROFILL]
DECIMAL
.
The FIXED
alias was added in version 4.1.0 for compatibility
with other servers.
DATE
'1000-01-01'
to '9999-12-31'
.
MySQL displays DATE
values in 'YYYY-MM-DD'
format, but
allows you to assign values to DATE
columns using either strings or
numbers. See section 6.2.2.2 The DATETIME
, DATE
, and TIMESTAMP
Types.
DATETIME
'1000-01-01
00:00:00'
to '9999-12-31 23:59:59'
. MySQL displays
DATETIME
values in 'YYYY-MM-DD HH:MM:SS'
format, but allows you
to assign values to DATETIME
columns using either strings or numbers.
See section 6.2.2.2 The DATETIME
, DATE
, and TIMESTAMP
Types.
TIMESTAMP[(M)]
'1970-01-01 00:00:00'
to sometime in the
year 2037
.
In MySQL 4.0 and earlier, TIMESTAMP
values are displayed in
YYYYMMDDHHMMSS
, YYMMDDHHMMSS
, YYYYMMDD
, or YYMMDD
format, depending on whether M
is 14
(or missing), 12
,
8
, or 6
, but allows you to assign values to TIMESTAMP
columns using either strings or numbers.
From MySQL 4.1, TIMESTAMP
is returned as a string with the format
'YYYY-MM-DD HH:MM:SS'
. If you want to have this as a number you
should add +0 to the timestamp column. Different timestamp lengths are
not supported. From version 4.0.12, the --new
option can be used
to make the server behave as in version 4.1.
A TIMESTAMP
column is useful
for recording the date and time of an INSERT
or UPDATE
operation because it is automatically set to the date and time of the most
recent operation if you don't give it a value yourself. You can also set it
to the current date and time by assigning it a NULL
value.
See section 6.2.2 Date and Time Types.
The M
argument affects only how a TIMESTAMP
column is displayed;
its values always are stored using 4 bytes each.
Note that TIMESTAMP(M)
columns where M
is 8 or 14 are reported to
be numbers while other TIMESTAMP(M)
columns are reported to be
strings. This is just to ensure that one can reliably dump and restore
the table with these types!
See section 6.2.2.2 The DATETIME
, DATE
, and TIMESTAMP
Types.
TIME
'-838:59:59'
to '838:59:59'
.
MySQL displays TIME
values in 'HH:MM:SS'
format, but
allows you to assign values to TIME
columns using either strings or
numbers. See section 6.2.2.3 The TIME
Type.
YEAR[(2|4)]
1901
to 2155
, 0000
in the 4-digit year format,
and 1970-2069 if you use the 2-digit format (70-69). MySQL displays
YEAR
values in YYYY
format, but allows you to assign values to
YEAR
columns using either strings or numbers. (The YEAR
type is
unavailable prior to MySQL Version 3.22.) See section 6.2.2.4 The YEAR
Type.
[NATIONAL] CHAR(M) [BINARY]
M
is 0 to 255 characters
(1 to 255 prior to MySQL Version 3.23).
Trailing spaces are removed when the value is retrieved. CHAR
values
are sorted and compared in case-insensitive fashion according to the
default character set unless the BINARY
keyword is given.
From version 4.1.0, if the M
value specified is greater than 255,
the column type is converted to TEXT
.
This is a compatibility feature.
NATIONAL CHAR
(or its equivalent short form, NCHAR
) is the
SQL-99 way to define that a CHAR
column should use the default
CHARACTER set. This is the default in MySQL.
CHAR
is a shorthand for CHARACTER
.
MySQL allows you to create a column of type
CHAR(0)
. This is mainly useful when you have to be compliant with
some old applications that depend on the existence of a column but that do not
actually use the value. This is also quite nice when you need a
column that only can take 2 values: A CHAR(0)
, that is not defined
as NOT NULL
, will occupy only one bit and can take only 2 values:
NULL
or ""
. See section 6.2.3.1 The CHAR
and VARCHAR
Types.
CHAR
CHAR(1)
.
[NATIONAL] VARCHAR(M) [BINARY]
M
is 0 to 255 characters (1 to 255 prior to MySQL Version 4.0.2).
VARCHAR
values are sorted and compared in case-insensitive fashion
unless the BINARY
keyword is given. See section 6.5.3.1 Silent Column Specification Changes.
From version 4.1.0, if the M
value specified is greater than 255,
the column type is converted to TEXT
.
This is a compatibility feature.
VARCHAR
is a shorthand for CHARACTER VARYING
.
See section 6.2.3.1 The CHAR
and VARCHAR
Types.
TINYBLOB
TINYTEXT
BLOB
or TEXT
column with a maximum length of 255 (2^8 - 1)
characters. See section 6.5.3.1 Silent Column Specification Changes. See section 6.2.3.2 The BLOB
and TEXT
Types.
BLOB
TEXT
BLOB
or TEXT
column with a maximum length of 65535 (2^16 - 1)
characters. See section 6.5.3.1 Silent Column Specification Changes. See section 6.2.3.2 The BLOB
and TEXT
Types.
MEDIUMBLOB
MEDIUMTEXT
BLOB
or TEXT
column with a maximum length of 16777215
(2^24 - 1) characters. See section 6.5.3.1 Silent Column Specification Changes. See section 6.2.3.2 The BLOB
and TEXT
Types.
LONGBLOB
LONGTEXT
BLOB
or TEXT
column with a maximum length of 4294967295
or 4G (2^32 - 1) characters. See section 6.5.3.1 Silent Column Specification Changes.
Upto MySQL version 3.23 the server/client protocol and MyISAM tables had
a limit of 16M per communication packet / table row, from version 4.x
the maximum allowed length of LONGTEXT
or LONGBLOB
columns
depends on the configured maximum packet size in the client/server
protocol and available memory. See section 6.2.3.2 The BLOB
and TEXT
Types.
ENUM('value1','value2',...)
'value1'
, 'value2'
, ...
,
NULL
or the special ""
error value. An ENUM
can
have a maximum of 65535 distinct values. See section 6.2.3.3 The ENUM
Type.
SET('value1','value2',...)
'value1'
, 'value2'
,
...
A SET
can have a maximum of 64 members. See section 6.2.3.4 The SET
Type.
DATETIME
, DATE
, and TIMESTAMP
TypesTIME
TypeYEAR
TypeCHAR
and VARCHAR
TypesBLOB
and TEXT
TypesENUM
TypeSET
TypePosted by alex_boyer on Friday November 15 2002, @1:25am | [Delete] [Edit] |
Notice:
The documentation says that negative values are
disallowed in FLOAT columns that are marked
UNSIGNED but this is not the case. Moreover, INT
UNSIGNED Columns are set to zero when a negative
value is provided.
Verify your data type before insertion/update.
Posted by [name withheld] on Monday May 12 2003, @10:17pm | [Delete] [Edit] |
The manual errs when it says:
BIT
BOOL
These are synonyms for TINYINT(1).
BOOL is a synonym for TINYINT(1) UNSIGNED.
BOOL UNSIGNED generates an error.
Posted by Darwin Baines on Thursday July 10 2003, @8:09am | [Delete] [Edit] |
> The manual errs when it says:
> BIT
> BOOL
> These are synonyms for TINYINT(1).
> BOOL is a synonym for TINYINT(1) UNSIGNED.
> BOOL UNSIGNED generates an error.
Although BOOL UNSIGNED generates an error, BOOL does in fact map to TINYINT(1).
Heres a little demo you can try:
mysql> CREATE TABLE test ( tester BOOL );
Query OK, 0 rows affected (0.03 sec)
mysql> SHOW COLUMNS FROM test;
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| tester | tinyint(1) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)