您的位置:首页 > 数据库 > MySQL

mysql文档摘要续3

2015-08-18 13:16 716 查看
mysql 数据类型:

整数浮点类型

1、bool、boolean相当于tinyint(1).其中true等同于1,false等同于0.

2、tinyint(M)、smallint(M)等默认是signed有符号的,可以定义unsigned无符号,M表示最少的长度如果少于这个数则从左补起,大于的话就不做任何处理。

3、decimal(M,D)其中M表示总长度max 65,D表示小数点后面的位数max 30。

4、float 4字节,double 8字节。

时间类型:

1、date 范围是
'1000-01-01'
to
'9999-12-31',格式为'YYYY-MM-DD',datetime(fsp) 范围
'1000-01-01 00:00:00.000000'
to [code]'9999-12-31 23:59:59.999999',格式是
'YYYY-MM-DD HH:MM:SS[.fsp]',其中fsp值的范围是0-6.初始化或更新时可以设置default或on update 为CURRENT_TIMESTAMP。[/code]

2、timestamp(fsp) 范围是
'1970-01-01 00:00:01.000000'
UTC to
'2038-01-19 03:14:07.999999'
UTC。初始化或更新时可以设置default或on update 为CURRENT_TIMESTAMP。

3、time(fsp) 范围是
'-838:59:59.000000'
to
'838:59:59.000000'
.

字符类型

1、char(M)其中M默认是1范围是0-255。

2、varchar(M)其中M范围一般是0-65535,如果字符集是utf8(三个字节一个字符)则最大是21844.mysql一般采用1-2个字节来存储字符长度,如果1个字节的话那么最多长度是255字节。

3、binary(M)二进制类型,类似于char(M).

4、varbinary(M)二进制类型,类似于varchar(M).

5、blob(M)与varchar类似,最大是65535字节。还有mediumblob、longblob。是byte strings 没有字符集的概念与text刚好相反,与text一样没有默认值。

6、text(M) 同上。 还有longtext。

7、enum枚举最多可以有65535个元素。

8、set集合最多可以有64个不同元素。

mysql对每一行的数据长度有限制(跟存储引擎无关)65535bytes,除去blob、text字段(这两个自动一般分开存储)。mysiam对于null的存储会需要额外的bit,innodb一样。对于varchar类型需要考虑到一般还有1-2(一般小于255就1一个字节)个字节存储字节长度。

All MySQL collations are of type
PADSPACE
. This means that all
CHAR
,
VARCHAR
, and
TEXT
values in MySQL are compared without regard to any trailing spaces. “Comparison” in this context does not include the
LIKE
pattern-matching operator, for which trailing spaces are significant. For example:

mysql> [code]CREATE TABLE names (myname CHAR(10));

Query OK, 0 rows affected (0.03 sec)

mysql>
INSERT INTO names VALUES ('Monty');

Query OK, 1 row affected (0.00 sec)

mysql>
SELECT myname = 'Monty', myname = 'Monty  ' FROM names;

+------------------+--------------------+
| myname = 'Monty' | myname = 'Monty ' |
+------------------+--------------------+
| 1 | 1 |
+------------------+--------------------+
1 row in set (0.00 sec)

mysql>
SELECT myname LIKE 'Monty', myname LIKE 'Monty  ' FROM names;

+---------------------+-----------------------+
| myname LIKE 'Monty' | myname LIKE 'Monty ' |
+---------------------+-----------------------+
| 1 | 0 |
+---------------------+-----------------------+
1 row in set (0.00 sec)
[/code]
This is true for all MySQL versions, and is not affected by the server SQL mode.

上面一段的大概意思是mysql在校对的时候尾部的space一般不考虑,like语句除外。

mysql对于default values默认值处理。当没有指定默认值时发生如下情况:

1如果column可以为null,则默认为null。

2如果column不可以为null,则不指定默认值(主键除外)。所以当插入或replace时,如果在strict sql mode情况下则发生错误,否则一般根据column的类型来决定默认值。

下面是各种数据类型的存储大小:

Storage Requirements for Numeric Types

Data TypeStorage Required
TINYINT
1 byte
SMALLINT
2 bytes
MEDIUMINT
3 bytes
INT
,
INTEGER
4 bytes
BIGINT
8 bytes
FLOAT(
p
)
4 bytes if 0 <=
p
<= 24, 8 bytes if 25 <=
p
<= 53
FLOAT
4 bytes
DOUBLE [PRECISION]
,
REAL
8 bytes
DECIMAL(
M
,[code]D
)[/code],
NUMERIC(
M
,[code]D
)[/code]
Varies; see following discussion
BIT(
M
)
approximately (
M
+7)/8 bytes
Values for
DECIMAL
(and
NUMERIC
) columns are represented using a binary format that packs nine decimal (base 10) digits into four bytes. Storage for the integer and fractional parts of each value are determined separately. Each multiple of nine digits requires four bytes, and the “leftover” digits require some fraction of four bytes. The storage required for excess digits is given by the following table.

Leftover DigitsNumber of Bytes
00
11
21
32
42
53
63
74
84

Storage Requirements for Date and Time Types

For
TIME
,
DATETIME
, and
TIMESTAMP
columns, the storage required for tables created before MySQL 5.6.4 differs from tables created from 5.6.4 on. This is due to a change in 5.6.4 that permits these types to have a fractional part, which requires from 0 to 3 bytes.

Data TypeStorage Required Before MySQL 5.6.4Storage Required as of MySQL 5.6.4
YEAR
1 byte1 byte
DATE
3 bytes3 bytes
TIME
3 bytes3 bytes + fractional seconds storage
DATETIME
8 bytes5 bytes + fractional seconds storage
TIMESTAMP
4 bytes4 bytes + fractional seconds storage
As of MySQL 5.6.4, storage for
YEAR
and
DATE
remains unchanged. However,
TIME
,
DATETIME
, and
TIMESTAMP
are represented differently.
DATETIME
is packed more efficiently, requiring 5 rather than 8 bytes for the nonfractional part, and all three parts have a fractional part that requires from 0 to 3 bytes, depending on the fractional seconds precision of stored values.

Fractional Seconds PrecisionStorage Required
00 bytes
1, 21 byte
3, 42 bytes
5, 63 bytes
For example,
TIME(0)
,
TIME(2)
,
TIME(4)
, and
TIME(6)
use 3, 4, 5, and 6 bytes, respectively.
TIME
and
TIME(0)
are equivalent and require the same storage.

Storage Requirements for String Types

In the following table,
M
represents the declared column length in characters for nonbinary string types and bytes for binary string types.
L
represents the actual length in bytes of a given string value.

Data TypeStorage Required
CHAR(
M
)
M
×
w
bytes, 0
<= 
M
<=
255, where
w
is the number of bytes required for the maximum-length character in the character set. See Section 14.2.6.7, “Physical Row Structure” for information about
CHAR
data type storage requirements for
InnoDB
tables.
BINARY(
M
)
M
bytes, 0
<= 
M
<=
255
VARCHAR(
M
)
,
VARBINARY(
M
)
L
+ 1 bytes if column values require 0 − 255 bytes,
L
+ 2 bytes if values may require more than 255 bytes
TINYBLOB
,
TINYTEXT
L
+ 1 bytes, where
L
< 28
BLOB
,
TEXT
L
+ 2 bytes, where
L
< 216
MEDIUMBLOB
,
MEDIUMTEXT
L
+ 3 bytes, where
L
< 224
LONGBLOB
,
LONGTEXT
L
+ 4 bytes, where
L
< 232
ENUM('
value1
','[code]value2
',...)[/code]
1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)
SET('
value1
','[code]value2
',...)[/code]
1, 2, 3, 4, or 8 bytes, depending on the number of set members (64 members maximum)
Variable-length string types are stored using a length prefix plus data. The length prefix requires from one to four bytes depending on the data type, and the value of the prefix is
L
(the byte length of the string). For example, storage for a
MEDIUMTEXT
value requires
L
bytes to store the value plus three bytes to store the length of the value.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: