11.1.1 数字类型概述
A summary of the numeric data types follows. For additional information about properties and storage requirements of the numeric types, see Section 11.2, “Numeric Types”, and Section 11.8, “Data Type Storage Requirements”.
M
indicates the maximum display width for integer types. The maximum display width is 255. Display width is unrelated to the range of values a type can contain, as described in Section 11.2, “Numeric Types”. For floating-point and fixed-point types, M
is the total number of digits that can be stored.
If you specify ZEROFILL
for a numeric column, MySQL automatically adds the UNSIGNED
attribute to the column.
Numeric data types that permit the UNSIGNED
attribute also permit SIGNED
. However, these data types are signed by default, so the SIGNED
attribute has no effect.
SERIAL
is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
.
SERIAL DEFAULT VALUE
in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE
.
When you use subtraction between integer values where one is of type UNSIGNED
, the result is unsigned unless the NO_UNSIGNED_SUBTRACTION
SQL mode is enabled. See Section 12.10, “Cast Functions and Operators”.
BIT[(
A bit-value type.M
)]M
indicates the number of bits per value, from 1 to 64. The default is 1 ifM
is omitted.TINYINT[(
A very small integer. The signed range isM
)] [UNSIGNED] [ZEROFILL]-128
to127
. The unsigned range is0
to255
.BOOL
,BOOLEAN
These types are synonyms forTINYINT(1)
. A value of zero is considered false. Nonzero values are considered true:mysql> SELECT IF(0, 'true', 'false'); +------------------------+ | IF(0, 'true', 'false') | +------------------------+ | false | +------------------------+ mysql> SELECT IF(1, 'true', 'false'); +------------------------+ | IF(1, 'true', 'false') | +------------------------+ | true | +------------------------+ mysql> SELECT IF(2, 'true', 'false'); +------------------------+ | IF(2, 'true', 'false') | +------------------------+ | true | +------------------------+
However, the values
TRUE
andFALSE
are merely aliases for1
and0
, respectively, as shown here:mysql> SELECT IF(0 = FALSE, 'true', 'false'); +--------------------------------+ | IF(0 = FALSE, 'true', 'false') | +--------------------------------+ | true | +--------------------------------+ mysql> SELECT IF(1 = TRUE, 'true', 'false'); +-------------------------------+ | IF(1 = TRUE, 'true', 'false') | +-------------------------------+ | true | +-------------------------------+ mysql> SELECT IF(2 = TRUE, 'true', 'false'); +-------------------------------+ | IF(2 = TRUE, 'true', 'false') | +-------------------------------+ | false | +-------------------------------+ mysql> SELECT IF(2 = FALSE, 'true', 'false'); +--------------------------------+ | IF(2 = FALSE, 'true', 'false') | +--------------------------------+ | false | +--------------------------------+
The last two statements display the results shown because
2
is equal to neither1
nor0
.SMALLINT[(
A small integer. The signed range isM
)] [UNSIGNED] [ZEROFILL]-32768
to32767
. The unsigned range is0
to65535
.MEDIUMINT[(
A medium-sized integer. The signed range isM
)] [UNSIGNED] [ZEROFILL]-8388608
to8388607
. The unsigned range is0
to16777215
.INT[(
A normal-size integer. The signed range isM
)] [UNSIGNED] [ZEROFILL]-2147483648
to2147483647
. The unsigned range is0
to4294967295
.INTEGER[(
This type is a synonym forM
)] [UNSIGNED] [ZEROFILL]INT
.BIGINT[(
A large integer. The signed range isM
)] [UNSIGNED] [ZEROFILL]-9223372036854775808
to9223372036854775807
. The unsigned range is0
to18446744073709551615
.SERIAL
is an alias forBIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
.Some things you should be aware of with respect toBIGINT
columns:- All arithmetic is done using signed
BIGINT
orDOUBLE
values, so you should not use unsigned big integers larger than9223372036854775807
(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 aBIGINT
value to aDOUBLE
.MySQL can handleBIGINT
in the following cases:- When using integers to store large unsigned values in a
BIGINT
column. - In
MIN(
orcol_name
)MAX(
, wherecol_name
)col_name
refers to aBIGINT
column. - When using operators (
+
,-
,*
, and so on) where both operands are integers.
- When using integers to store large unsigned values in a
- You can always store an exact integer value in a
BIGINT
column by storing it using a string. In this case, MySQL performs a string-to-number conversion that involves no intermediate double-precision representation. - The
-
,+
, and*
operators useBIGINT
arithmetic when both operands 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 than9223372036854775807
.
- All arithmetic is done using signed
DECIMAL[(
A packed “exact” fixed-point number.M
[,D
])] [UNSIGNED] [ZEROFILL]M
is the total number of digits (the precision) andD
is the number of digits after the decimal point (the scale). The decimal point and (for negative numbers) the-
sign are not counted inM
. IfD
is 0, values have no decimal point or fractional part. The maximum number of digits (M
) forDECIMAL
is 65. The maximum number of supported decimals (D
) is 30. IfD
is omitted, the default is 0. IfM
is omitted, the default is 10.UNSIGNED
, if specified, disallows negative values.All basic calculations (+, -, *, /
) withDECIMAL
columns are done with a precision of 65 digits.DEC[(
,M
[,D
])] [UNSIGNED] [ZEROFILL]NUMERIC[(
,M
[,D
])] [UNSIGNED] [ZEROFILL]FIXED[(
These types are synonyms forM
[,D
])] [UNSIGNED] [ZEROFILL]DECIMAL
. TheFIXED
synonym is available for compatibility with other database systems.FLOAT[(
A small (single-precision) floating-point number. Permissible values areM
,D
)] [UNSIGNED] [ZEROFILL]-3.402823466E+38
to-1.175494351E-38
,0
, and1.175494351E-38
to3.402823466E+38
. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system.M
is the total number of digits andD
is the number of digits following the decimal point. IfM
andD
are omitted, values are stored to the limits permitted by the hardware. A single-precision floating-point number is accurate to approximately 7 decimal places.UNSIGNED
, if specified, disallows negative values.UsingFLOAT
might give you some unexpected problems because all calculations in MySQL are done with double precision. See Section B.5.4.7, “Solving Problems with No Matching Rows”.DOUBLE[(
A normal-size (double-precision) floating-point number. Permissible values areM
,D
)] [UNSIGNED] [ZEROFILL]-1.7976931348623157E+308
to-2.2250738585072014E-308
,0
, and2.2250738585072014E-308
to1.7976931348623157E+308
. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system.M
is the total number of digits andD
is the number of digits following the decimal point. IfM
andD
are omitted, values are stored to the limits permitted by the hardware. A double-precision floating-point number is accurate to approximately 15 decimal places.UNSIGNED
, if specified, disallows negative values.DOUBLE PRECISION[(
,M
,D
)] [UNSIGNED] [ZEROFILL]REAL[(
These types are synonyms forM
,D
)] [UNSIGNED] [ZEROFILL]DOUBLE
. Exception: If theREAL_AS_FLOAT
SQL mode is enabled,REAL
is a synonym forFLOAT
rather thanDOUBLE
.FLOAT(
A floating-point number.p
) [UNSIGNED] [ZEROFILL]p
represents the precision in bits, but MySQL uses this value only to determine whether to useFLOAT
orDOUBLE
for the resulting data type. Ifp
is from 0 to 24, the data type becomesFLOAT
with noM
orD
values. Ifp
is from 25 to 53, the data type becomesDOUBLE
with noM
orD
values. The range of the resulting column is the same as for the single-precisionFLOAT
or double-precisionDOUBLE
data types described earlier in this section.FLOAT(
syntax is provided for ODBC compatibility.p
)
11.1.2 日期和时间类型概述
A summary of the temporal data types follows. For additional information about properties and storage requirements of the temporal types, see Section 11.3, “Date and Time Types”, and Section 11.8, “Data Type Storage Requirements”. For descriptions of functions that operate on temporal values, see Section 12.7, “Date and Time Functions”.
For the DATE
and DATETIME
range descriptions, “supported” means that although earlier values might work, there is no guarantee.
MySQL permits fractional seconds for TIME
, DATETIME
, and TIMESTAMP
values, with up to microseconds (6 digits) precision. To define a column that includes a fractional seconds part, use the syntax
, where type_name
(fsp
)type_name
is TIME
, DATETIME
, or TIMESTAMP
, and fsp
is the fractional seconds precision. For example:
CREATE TABLE t1 (t TIME(3), dt DATETIME(6));
The fsp
value, if given, must be in the range 0 to 6. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. (This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)
Any TIMESTAMP
or DATETIME
column in a table can have automatic initialization and updating properties.
DATE
A date. The supported range is'1000-01-01'
to'9999-12-31'
. MySQL displaysDATE
values in'YYYY-MM-DD'
format, but permits assignment of values toDATE
columns using either strings or numbers.DATETIME[(
A date and time combination. The supported range isfsp
)]'1000-01-01 00:00:00.000000'
to'9999-12-31 23:59:59.999999'
. MySQL displaysDATETIME
values in'YYYY-MM-DD HH:MM:SS[.fraction]'
format, but permits assignment of values toDATETIME
columns using either strings or numbers.An optionalfsp
value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0.Automatic initialization and updating to the current date and time forDATETIME
columns can be specified usingDEFAULT
andON UPDATE
column definition clauses, as described in Section 11.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.TIMESTAMP[(
A timestamp. The range isfsp
)]'1970-01-01 00:00:01.000000'
UTC to'2038-01-19 03:14:07.999999'
UTC.TIMESTAMP
values are stored as the number of seconds since the epoch ('1970-01-01 00:00:00'
UTC). ATIMESTAMP
cannot represent the value'1970-01-01 00:00:00'
because that is equivalent to 0 seconds from the epoch and the value 0 is reserved for representing'0000-00-00 00:00:00'
, the “zero”TIMESTAMP
value.An optionalfsp
value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0.The way the server handlesTIMESTAMP
definitions depends on the value of theexplicit_defaults_for_timestamp
system variable (see Section 5.1.5, “Server System Variables”).Ifexplicit_defaults_for_timestamp
is enabled, there is no automatic assignment of theDEFAULT CURRENT_TIMESTAMP
orON UPDATE CURRENT_TIMESTAMP
attributes to anyTIMESTAMP
column. They must be included explicitly in the column definition. Also, anyTIMESTAMP
not explicitly declared asNOT NULL
permitsNULL
values.If
explicit_defaults_for_timestamp
is disabled, the server handlesTIMESTAMP
as follows:Unless specified otherwise, the first
TIMESTAMP
column in a table is defined to be automatically set to the date and time of the most recent modification if not explicitly assigned a value. This makesTIMESTAMP
useful for recording the timestamp of anINSERT
orUPDATE
operation. You can also set anyTIMESTAMP
column to the current date and time by assigning it aNULL
value, unless it has been defined with theNULL
attribute to permitNULL
values.Automatic initialization and updating to the current date and time can be specified using
DEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
column definition clauses. By default, the firstTIMESTAMP
column has these properties, as previously noted. However, anyTIMESTAMP
column in a table can be defined to have these properties.TIME[(
A time. The range isfsp
)]'-838:59:59.000000'
to'838:59:59.000000'
. MySQL displaysTIME
values in'HH:MM:SS[.fraction]'
format, but permits assignment of values toTIME
columns using either strings or numbers.An optionalfsp
value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0.YEAR[(4)]
A year in four-digit format. MySQL displaysYEAR
values inYYYY
format, but permits assignment of values toYEAR
columns using either strings or numbers. Values display as1901
to2155
, and0000
.NoteThe
YEAR(2)
data type is deprecated and support for it is removed in MySQL 5.7.5. To convertYEAR(2)
columns toYEAR(4)
, see Section 11.3.4, “YEAR(2) Limitations and Migrating to YEAR(4)”.For additional information about
YEAR
display format and interpretation of input values, see Section 11.3.3, “The YEAR Type”.
The SUM()
and AVG()
aggregate functions do not work with temporal values. (They convert the values to numbers, losing everything after the first nonnumeric character.) To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. Examples:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name;
SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;
The MySQL server can be run with the MAXDB
SQL mode enabled. In this case, TIMESTAMP
is identical with DATETIME
. If this mode is enabled at the time that a table is created, TIMESTAMP
columns are created as DATETIME
columns. As a result, such columns use DATETIME
display format, have the same range of values, and there is no automatic initialization or updating to the current date and time. See Section 5.1.8, “Server SQL Modes”.
11.1.3 字符类型概述
A summary of the string data types follows. For additional information about properties and storage requirements of the string types, see Section 11.4, “String Types”, andSection 11.8, “Data Type Storage Requirements”.
In some cases, MySQL may change a string column to a type different from that given in a CREATE TABLE
or ALTER TABLE
statement. See Section 13.1.18.7, “Silent Column Specification Changes”.
MySQL interprets length specifications in character column definitions in character units. This applies to CHAR
, VARCHAR
, and the TEXT
types.
Column definitions for many string data types can include attributes that specify the character set or collation of the column. These attributes apply to the CHAR
, VARCHAR
, the TEXT
types, ENUM
, and SET
data types:
- The
CHARACTER SET
attribute specifies the character set, and theCOLLATE
attribute specifies a collation for the character set. For example:CREATE TABLE t ( c1 VARCHAR(20) CHARACTER SET utf8, c2 TEXT CHARACTER SET latin1 COLLATE latin1_general_cs );
This table definition creates a column named
c1
that has a character set ofutf8
with the default collation for that character set, and a column namedc2
that has a character set oflatin1
and a case-sensitive collation.The rules for assigning the character set and collation when either or both of the
CHARACTER SET
andCOLLATE
attributes are missing are described in Section 10.1.3.5, “Column Character Set and Collation”.CHARSET
is a synonym forCHARACTER SET
. - Specifying the
CHARACTER SET binary
attribute for a character string data type causes the column to be created as the corresponding binary string data type:CHAR
becomesBINARY
,VARCHAR
becomesVARBINARY
, andTEXT
becomesBLOB
. For theENUM
andSET
data types, this does not occur; they are created as declared. Suppose that you specify a table using this definition:CREATE TABLE t ( c1 VARCHAR(10) CHARACTER SET binary, c2 TEXT CHARACTER SET binary, c3 ENUM('a','b','c') CHARACTER SET binary );
The resulting table has this definition:
CREATE TABLE t ( c1 VARBINARY(10), c2 BLOB, c3 ENUM('a','b','c') CHARACTER SET binary );
- The
BINARY
attribute is shorthand for specifying the table default character set and the binary (_bin
) collation of that character set. In this case, comparison and sorting are based on numeric character code values. - The
ASCII
attribute is shorthand forCHARACTER SET latin1
. - The
UNICODE
attribute is shorthand forCHARACTER SET ucs2
.
Character column comparison and sorting are based on the collation assigned to the column. For the CHAR
, VARCHAR
, TEXT
, ENUM
, and SET
data types, you can declare a column with a binary (_bin
) collation or the BINARY
attribute to cause comparison and sorting to use the underlying character code values rather than a lexical ordering.
For additional information about use of character sets in MySQL, see Section 10.1, “Character Set Support”.
[NATIONAL] CHAR[(
A fixed-length string that is always right-padded with spaces to the specified length when stored.M
)] [CHARACTER SETcharset_name
] [COLLATEcollation_name
]M
represents the column length in characters. The range ofM
is 0 to 255. IfM
is omitted, the length is 1.NoteTrailing spaces are removed when
CHAR
values are retrieved unless thePAD_CHAR_TO_FULL_LENGTH
SQL mode is enabled.CHAR
is shorthand forCHARACTER
.NATIONAL CHAR
(or its equivalent short form,NCHAR
) is the standard SQL way to define that aCHAR
column should use some predefined character set. MySQL usesutf8
as this predefined character set. Section 10.1.3.7, “The National Character Set”.The
CHAR BYTE
data type is an alias for theBINARY
data type. This is a compatibility feature.MySQL permits you to create a column of type
CHAR(0)
. This is useful primarily when you have to be compliant with old applications that depend on the existence of a column but that do not actually use its value.CHAR(0)
is also quite nice when you need a column that can take only two values: A column that is defined asCHAR(0) NULL
occupies only one bit and can take only the valuesNULL
and''
(the empty string).[NATIONAL] VARCHAR(
A variable-length string.M
) [CHARACTER SETcharset_name
] [COLLATEcollation_name
]M
represents the maximum column length in characters. The range ofM
is 0 to 65,535. The effective maximum length of aVARCHAR
is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. For example,utf8
characters can require up to three bytes per character, so aVARCHAR
column that uses theutf8
character set can be declared to be a maximum of 21,844 characters. See Section C.10.4, “Limits on Table Column Count and Row Size”.MySQL storesVARCHAR
values as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. AVARCHAR
column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.NoteMySQL follows the standard SQL specification, and does not remove trailing spaces from
VARCHAR
values.VARCHAR
is shorthand forCHARACTER VARYING
.NATIONAL VARCHAR
is the standard SQL way to define that aVARCHAR
column should use some predefined character set. MySQL usesutf8
as this predefined character set. Section 10.1.3.7, “The National Character Set”.NVARCHAR
is shorthand forNATIONAL VARCHAR
.BINARY(
TheM
)BINARY
type is similar to theCHAR
type, but stores binary byte strings rather than nonbinary character strings.M
represents the column length in bytes.VARBINARY(
TheM
)VARBINARY
type is similar to theVARCHAR
type, but stores binary byte strings rather than nonbinary character strings.M
represents the maximum column length in bytes.TINYBLOB
ABLOB
column with a maximum length of 255 (28 − 1) bytes. EachTINYBLOB
value is stored using a 1-byte length prefix that indicates the number of bytes in the value.TINYTEXT [CHARACTER SET
Acharset_name
] [COLLATEcollation_name
]TEXT
column with a maximum length of 255 (28 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTINYTEXT
value is stored using a 1-byte length prefix that indicates the number of bytes in the value.BLOB[(
AM
)]BLOB
column with a maximum length of 65,535 (216 − 1) bytes. EachBLOB
value is stored using a 2-byte length prefix that indicates the number of bytes in the value.An optional lengthM
can be given for this type. If this is done, MySQL creates the column as the smallestBLOB
type large enough to hold valuesM
bytes long.TEXT[(
AM
)] [CHARACTER SETcharset_name
] [COLLATEcollation_name
]TEXT
column with a maximum length of 65,535 (216 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachTEXT
value is stored using a 2-byte length prefix that indicates the number of bytes in the value.An optional lengthM
can be given for this type. If this is done, MySQL creates the column as the smallestTEXT
type large enough to hold valuesM
characters long.MEDIUMBLOB
ABLOB
column with a maximum length of 16,777,215 (224 − 1) bytes. EachMEDIUMBLOB
value is stored using a 3-byte length prefix that indicates the number of bytes in the value.MEDIUMTEXT [CHARACTER SET
Acharset_name
] [COLLATEcollation_name
]TEXT
column with a maximum length of 16,777,215 (224 − 1) characters. The effective maximum length is less if the value contains multibyte characters. EachMEDIUMTEXT
value is stored using a 3-byte length prefix that indicates the number of bytes in the value.LONGBLOB
ABLOB
column with a maximum length of 4,294,967,295 or 4GB (232 − 1) bytes. The effective maximum length ofLONGBLOB
columns depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGBLOB
value is stored using a 4-byte length prefix that indicates the number of bytes in the value.LONGTEXT [CHARACTER SET
Acharset_name
] [COLLATEcollation_name
]TEXT
column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length ofLONGTEXT
columns also depends on the configured maximum packet size in the client/server protocol and available memory. EachLONGTEXT
value is stored using a 4-byte length prefix that indicates the number of bytes in the value.ENUM('
An enumeration. A string object that can have only one value, chosen from the list of valuesvalue1
','value2
',...) [CHARACTER SETcharset_name
] [COLLATEcollation_name
]'
,value1
''
,value2
'...
,NULL
or the special''
error value.ENUM
values are represented internally as integers.AnENUM
column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) A table can have no more than 255 unique element list definitions among itsENUM
andSET
columns considered as a group. For more information on these limits, see Section C.10.5, “Limits Imposed by .frm File Structure”.SET('
A set. A string object that can have zero or more values, each of which must be chosen from the list of valuesvalue1
','value2
',...) [CHARACTER SETcharset_name
] [COLLATEcollation_name
]'
,value1
''
,value2
'...
SET
values are represented internally as integers.ASET
column can have a maximum of 64 distinct members. A table can have no more than 255 unique element list definitions among itsENUM
andSET
columns considered as a group. For more information on this limit, see Section C.10.5, “Limits Imposed by .frm File Structure”.