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

mysql5.6常见数据类型

2015-11-19 23:00 751 查看
以下所述mysql版本为5.6.27,不同版本直接可能存在差异
1.整数类型

DataTypeStorageRequired
TINYINT
1byte
SMALLINT
2bytes
MEDIUMINT
3bytes
INT
,
INTEGER
4bytes
BIGINT
8bytes
FLOAT(p
)
4bytesif0<=
p
<=
24,8bytesif25<=
p
<=
53
FLOAT
4bytes
DOUBLE[PRECISION]
,
REAL
8bytes
DECIMAL(M
,
D
),
NUMERIC(M
,
D
)
Varies;seefollowingdiscussion
BIT(M
)
approximately(
M
+7)/8
bytes
DECIMAL对DECIMAL(M,D),M表示float的总共长度,D表示小数点后面的长度,例如decimal(7,4)表示-999.9999,如果不写,默认是decimal(10,0)表示
mysql>createtablet4(idint(5),xint(2));

mysql中的int(5)指定长度并不能现在int的使用长度,例如:
mysql>insertintot4values(111111,111);

bit型表示位型,当插入数字的时候,会将数字转换为二进制,如果转换为的二进制数大于bit所指定的大小,那么查询的时候将显示空,默认bit为bin(1),例如下面的例子:

mysql>createtablet5(idbit(10));
mysql>insertintot5values(10);
mysql>selectbin(id),hex(id)fromt5;
+---------+---------+
|bin(id)|hex(id)|
+---------+---------+
|1010|A|.
+---------+---------+


2.时间类型
如果用来表示年月日,一般用date
如果用来表示年月日时分秒,一般用datetime
如果只用来表示分秒,通常用time
如果需要经常插入当前的系统时间,一般使用timestamp

DataTypeStorageRequiredBeforeMySQL5.6.4StorageRequiredasofMySQL5.6.4
YEAR
1byte1byte
DATE
3bytes3bytes
TIME
3bytes3bytes+fractionalsecondsstorage
DATETIME
8bytes5bytes+fractionalsecondsstorage
TIMESTAMP
4bytes4bytes+fractionalsecondsstorage
下例子:

mysql>createtablettime(adate,bdatetime,ctimestamp,dtime,eyear);
mysql>insertintottimevalues(current_timestamp,current_timestamp,current_timestamp,current_timestamp,current_timestamp);
QueryOK,1rowaffected,1warning(0.01sec)
mysql>select*fromttime;
mysql>select*fromttime;
+------------+---------------------+---------------------+----------+------+
|a|b|c|d|e|
+------------+---------------------+---------------------+----------+------+
|2015-11-19|2015-11-1919:30:30|2015-11-1919:30:30|19:30:30|2015|
+------------+---------------------+---------------------+----------+------+


如果对timestamp不指定输入或者输入空值,timestamp会取当前操作系统时间,因此timestamp适合用在经常去操作系统时间的场景,注意只有第一个timestamp会出现这种情况,如果还有第二个timestamp类型,在不指定默认值为current_timestamp的情况下,其默认值为0。

timestamp一个重要特点是和时区相关,如果不同的时区查看的值是不同的,而且timestamp只支持到2038年的某一天

可能在以前的版本中只能有一个timestamp的默认值指定为current_timestamp,我测试的版本为5.6.27,可以有多个timestamp类型的默认值设置为current_timestamp

mysql>insertintottime(a)values(current_timestamp);
QueryOK,1rowaffected,1warning(0.00sec)
mysql>select*fromttime;
+------------+---------------------+---------------------+----------+------+
|a|b|c|d|e|
+------------+---------------------+---------------------+----------+------+
|2015-11-19|2015-11-1919:30:30|2015-11-1919:30:30|19:30:30|2015|
|2015-11-19|NULL|2015-11-1919:51:01|NULL|NULL|
+------------+---------------------+---------------------+----------+------+
2rowsinset(0.00sec)


mysql>selectversion();
+------------+
|version()|
+------------+
|5.6.27-log|
+------------+
1rowinset(0.00sec)
mysql>showcreatetabletstamp\G
***************************1.row***************************
Table:tstamp
CreateTable:CREATETABLE`tstamp`(
`a`timestampNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,
`b`timestampNOTNULLDEFAULTCURRENT_TIMESTAMP,
`c`timestampNOTNULLDEFAULTCURRENT_TIMESTAMP,
`d`timestampNOTNULLDEFAULT'0000-00-0000:00:00'
)ENGINE=InnoDBDEFAULTCHARSET=utf8
1rowinset(0.00sec)
mysql>select*fromtstamp;
+---------------------+---------------------+---------------------+---------------------+
|a|b|c|d|
+---------------------+---------------------+---------------------+---------------------+
|2015-11-1920:06:49|2015-11-1920:06:49|2015-11-1920:07:56|0000-00-0000:00:00|
|2015-11-1920:08:27|2015-11-1920:08:27|2015-11-1920:08:27|0000-00-0000:00:00|
+---------------------+---------------------+---------------------+---------------------+


3.字符类型
支持的字符类型:

DataTypeStorageRequired
CHAR(M
)
M
×
w
bytes,
0
<=M
<=255,
where
w
is
thenumberofbytesrequiredforthemaximum-lengthcharacterinthecharacterset.SeeSection
14.2.6.7,“PhysicalRowStructure”forinformationabout
CHAR
data
typestoragerequirementsfor
InnoDB
tables.
BINARY(M
)
M
bytes,
0
<=M
<=255
VARCHAR(M
),
VARBINARY(M
)
L
+
1bytesifcolumnvaluesrequire0−255bytes,
L
+
2bytesifvaluesmayrequiremorethan255bytes
TINYBLOB
,
TINYTEXT
L
+
1bytes,where
L
<
28
BLOB
,
TEXT
L
+
2bytes,where
L
<
216
MEDIUMBLOB
,
MEDIUMTEXT
L
+
3bytes,where
L
<
224
LONGBLOB
,
LONGTEXT
L
+
4bytes,where
L
<
232
ENUM('value1
','
value2
',...)
1or2bytes,dependingonthenumberofenumerationvalues(65,535valuesmaximum)
SET('value1
','
value2
',...)
1,2,3,4,or8bytes,dependingonthenumberofsetmembers(64membersmaximum)

来源:<http://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html>
由上表可以看出char是0-255字符,不是字节
varchar支持0-65535个字节
char和varchar的区别是一个是定长,一个是变长
char会删除字符串末尾的空格,varchar则不会
对于相同长度的char的效率会比varchar高,但是varchar的存储空间会比char小。

BLOB和TEXT的区别是BLOB是二进制字符串,一般保存图片什么的,text类似于oracle中的clob。

binary和varbinary类似于char和varchar。但是他们保存的二进制类型.

ENUM是枚举行,创建表的时候需要通过枚举的方式指定允许的值,1-255个成员需要一个字节存储,对于255-65535个成员需要2个字节存储
枚举是不区分大小写的,如果对表中插入一条不在枚举范围内的数据会报错ERROR1265(01000)
也可以通过插入1,2,3...表示插入的为枚举中的第1,2,3个值:

mysql>createtablet8(aenum('a','b','c','d','e'));
QueryOK,0rowsaffected(0.01sec)
mysql>insertintot8values(2);
QueryOK,1rowaffected(0.00sec)
mysql>insertintot8values(5);
QueryOK,1rowaffected(0.00sec)
mysql>insertintot8values('c');
QueryOK,1rowaffected(0.00sec)
mysql>select*fromt8;
+------+
|a|
+------+
|b|
|e|
|c|
+------+
3rowsinset(0.00sec)
mysql>insertintot8values('g');
ERROR1265(01000):Datatruncatedforcolumn'a'atrow1


set类型和enum类型很像,但是set类型可以一次选取多个成员,而enum只能是一个:

mysql>createtablet9(aset('a','b','c','d','e'));
QueryOK,0rowsaffected(0.00sec)
mysql>insertintot9values('a','b');
ERROR1136(21S01):Columncountdoesn'tmatchvaluecountatrow1
mysql>insertintot9values('ab');
ERROR1265(01000):Datatruncatedforcolumn'a'atrow1
mysql>insertintot9values('a,b');
QueryOK,1rowaffected(0.00sec)
mysql>insertintot9values('c,d,e');
QueryOK,1rowaffected(0.00sec)
mysql>select*fromt9;
+-------+
|a|
+-------+
|a,b|
|c,d,e|
+-------+
2rowsinset(0.00sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: