First, the data type of MySQL

It mainly includes the following five categories:

Integer types: BIT, BOOL, TINY INT, SMALL INT, MEDIUM INT, INT, BIG INT

Floating point types: FLOAT, DOUBLE, DECIMAL

String types: CHAR, VARCHAR, TINY TEXT, TEXT, MEDIUM TEXT, LONGTEXT, TINY BLOB, BLOB, MEDIUM BLOB, LONG BLOB

Date type: Date, DateTime, TimeStamp, Time, Year

Other data types: BINARY, VARBINARY, ENUM, SET, Geometry, Point, MultiPoint, LineString, MultiLineString, Polygon, GeometryCollection, etc.

 

1. Integer

MySQL data type Meaning (signed)
tinyint(m) 1 byte range (-128 ~ 127)
smallint(m) 2 bytes range (-32768 ~ 32767)
mediumint(m) 3 bytes range (-8388608 ~ 8388607)
int(m) 4 bytes range (-2147483648 ~ 2147483647)
bigint(m) 8 bytes range (+ -9.22 * 10 to the 18th power)

If unsigned is added, the maximum value doubles. For example, the value range of tinyint unsigned is (0 ~ 256).

 The m in int (m) indicates the display width in the SELECT query result set, does not affect the actual value range, does not affect the display width, and I do not know what use this m has.

 

2. Floating point type (float and double)

MySQL data type meaning
float(m,d) Single-precision floating-point type 8-bit precision (4 bytes) m total number, d decimal places
double(m,d) Double-precision floating-point type 16-bit precision (8 bytes) m total number, d decimal places

Set a field to be defined as float (6,3). If you insert a number 123.45678, the actual database stores 123.457, but the total number is still subject to the actual, that is, 6 digits. The integer part has a maximum of 3 digits. If you insert the number 12.123456, it will store 12.1234. If you insert 12.12, it will store 12.1200.

 

3. Fixed point number

Floating-point types store approximate values ​​in the database, while fixed-point types store exact values ​​in the database. 

decimal (m, d) The parameter m <65 is the total number, d <30 and d <m is the decimal place.

 

4. String (char, varchar, _text)

MySQL data type meaning
char(n) Fixed length, up to 255 characters
varchar(n) Fixed length, up to 65535 characters
tinytext Variable length, up to 255 characters
text Variable length, up to 65535 characters
mediumtext Variable length, up to 2 to the 24th power-1 character
longtext Variable length, up to 2 to the 32nd power-1 character

char和varchar:

1.char (n) If the number of stored characters is less than n, it will be supplemented by spaces, and the spaces will be removed during the query. Therefore, there is no space at the end of the string stored in the char type, and varchar is not limited to this. 

2. char (n) fixed length, no matter how many characters are stored in char (4), it will occupy 4 bytes, varchar is the actual number of characters stored + 1 byte (n <= 255) or 2 Bytes (n> 255),

So varchar (4), storing 3 characters will occupy 4 bytes. 


3. The character string retrieval speed of the char type is faster than that of the varchar type.
varchar and text: 

1. varchar can specify n, text cannot be specified, internal storage varchar is the actual number of characters stored + 1 byte (n <= 255) or 2 bytes (n> 255), text is the actual number of characters + 2 Words

Section. 

2. The text type cannot have a default value. 

3. Varchar can directly create an index, and text creates an index to specify how many characters before. varchar query speed is faster than text, in the case of creating indexes, text index does not seem to work.

 

5. Binary data (_Blob)

1. _BLOB and _text are stored differently, _TEXT is stored in text mode, English storage is case sensitive, and _Blob is stored in binary mode, regardless of case.

2. The data stored in _BLOB can only be read out as a whole. 

3. _TEXT can specify the character set, _BLO does not need to specify the character set.

 

6. Date and time type

MySQL data type meaning
date Date '2008-12-2'
time Time '12: 25: 36 '
datetime Date and time '2008-12-2 22:06:44'
timestamp Automatic storage record modification time

If you define a field as timestamp, the time data in this field will be automatically refreshed when other fields are modified, so the field of this data type can store the last modified time of this record.

 

Data type attributes

 

MySQL keywords meaning
NULL Data columns can contain NULL values
NOT NULL Data columns are not allowed to contain NULL values
DEFAULT Defaults
PRIMARY KEY Primary key
AUTO_INCREMENT Automatic increment, suitable for integer type
UNSIGNED Unsigned
CHARACTER SET name Specify a character set

 

Second, the length and scope of the MYSQL data type

List of data types and byte length:

type of data Byte length Scope or usage
Bit 1 Unsigned [0,255], signed [-128,127], Tianyuan Blog Remarks: Both BIT and BOOL Boolean types occupy 1 byte
TinyInt 1 Integer [0,255]
SmallInt 2 Unsigned [0,65535], signed [-32768,32767]
MediumInt 3 Unsigned [0,2 ^ 24-1], signed [-2 ^ 23,2 ^ 23-1]]
Int 4 Unsigned [0,2 ^ 32-1], signed [-2 ^ 31,2 ^ 31-1]
BigInt 8 Unsigned [0,2 ^ 64-1], signed [-2 ^ 63, 2 ^ 63 -1]
Float(M,D) 4 Single precision floating point number. Tianyuan Blog reminds that D here is precision. If D <= 24, it is the default FLOAT. If D> 24, it will be automatically converted to DOUBLE type.
Double(M,D) 8  Double precision floating point.
Decimal(M,D) M + 1 or M + 2 未打包的浮点数,用法类似于FLOAT和DOUBLE,天缘博客提醒您如果在ASP中使用到Decimal数据类型,直接从数据库读出来的Decimal可能需要先转换成Float或Double类型后再进行运算。
Date 3 以YYYY-MM-DD的格式显示,比如:2009-07-19
Date Time 8 以YYYY-MM-DD HH:MM:SS的格式显示,比如:2009-07-19 11:22:30
TimeStamp 4 以YYYY-MM-DD的格式显示,比如:2009-07-19
Time 3 以HH:MM:SS的格式显示。比如:11:22:30
Year 1 以YYYY的格式显示。比如:2009
Char(M) M 定长字符串。
VarChar(M) M 变长字符串,要求M<=255
Binary(M) M 类似Char的二进制存储,特点是插入定长不足补0
VarBinary(M) M 类似VarChar的变长二进制存储,特点是定长不补0
Tiny Text Max:255 大小写不敏感
Text Max:64K 大小写不敏感
Medium Text Max:16M 大小写不敏感
Long Text Max:4G 大小写不敏感
TinyBlob Max:255 大小写敏感
Blob Max:64K 大小写敏感
MediumBlob Max:16M 大小写敏感
LongBlob Max:4G 大小写敏感
Enum 1或2 最大可达65535个不同的枚举值
Set 可达8 最大可达64个不同的值
Geometry    
Point    
LineString    
Polygon    
MultiPoint    
MultiLineString    
MultiPolygon    
GeometryCollection    

三、使用建议

1、在指定数据类型的时候一般是采用从小原则,比如能用TINY INT的最好就不用INT,能用FLOAT类型的就不用DOUBLE类型,这样会对MYSQL在运行效率上提高很大,尤其是大数据量测试条件下。

2、不需要把数据表设计的太过复杂,功能模块上区分或许对于后期的维护更为方便,慎重出现大杂烩数据表

3、数据表和字段的起名字也是一门学问

4、设计数据表结构之前请先想象一下是你的房间,或许结果会更加合理、高效

5、数据库的最后设计结果一定是效率和可扩展性的折中,偏向任何一方都是欠妥的

 

选择数据类型的基本原则

前提:使用适合存储引擎。

选择原则:根据选定的存储引擎,确定如何选择合适的数据类型。

下面的选择方法按存储引擎分类:

  • MyISAM 数据存储引擎和数据列:MyISAM数据表,最好使用固定长度(CHAR)的数据列代替可变长度(VARCHAR)的数据列。
  • MEMORY存储引擎和数据列:MEMORY数据表目前都使用固定长度的数据行存储,因此无论使用CHAR或VARCHAR列都没有关系。两者都是作为CHAR类型处理的。
  • InnoDB 存储引擎和数据列:建议使用 VARCHAR类型。


对于InnoDB数据表,内部的行存储格式没有区分固定长度和可变长度列(所有数据行都使用指向数据列值的头指针),因此在本质上,使用固定长度的CHAR列不一定比使用可变长度VARCHAR列简单。因而,主要的性能因素是数据行使用的存储总量。由于CHAR平均占用的空间多于VARCHAR,因 此使用VARCHAR来最小化需要处理的数据行的存储总量和磁盘I/O是比较好的。

下面说一下固定长度数据列与可变长度的数据列。

char与varchar

CHAR和VARCHAR类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。

下面的表显示了将各种字符串值保存到CHAR(4)和VARCHAR(4)列后的结果,说明了CHAR和VARCHAR之间的差别:

CHAR(4) 存储需求 VARCHAR(4) 存储需求
'' '    ' 4个字节 '' 1个字节
'ab' 'ab  ' 4个字节 'ab ' 3个字节
'abcd' 'abcd' 4个字节 'abcd' 5个字节
'abcdefgh' 'abcd' 4个字节 'abcd' 5个字节


请注意上表中最后一行的值只适用不使用严格模式时;如果MySQL运行在严格模式,超过列长度不的值保存,并且会出现错误。

从CHAR(4)和VARCHAR(4)列检索的值并不总是相同,因为检索时从CHAR列删除了尾部的空格。通过下面的例子说明该差别:
mysql> CREATE TABLE vc (v VARCHAR(4), c CHAR(4));
Query OK, 0 rows affected (0.02 sec)
 
mysql> INSERT INTO vc VALUES ('ab  ', 'ab  ');
Query OK, 1 row affected (0.00 sec)
 
mysql> SELECT CONCAT(v, '+'), CONCAT(c, '+') FROM vc;
+----------------+----------------+
| CONCAT(v, '+') | CONCAT(c, '+') |
+----------------+----------------+
| ab  +          | ab+            |
+----------------+----------------+
1 row in set (0.00 sec)

text和blob

 

在使用text和blob字段类型时要注意以下几点,以便更好的发挥数据库的性能。

①BLOB和TEXT值也会引起自己的一些问题,特别是执行了大量的删除或更新操作的时候。删除这种值会在数据表中留下很大的"空洞",以后填入这些"空洞"的记录可能长度不同,为了提高性能,建议定期使用 OPTIMIZE TABLE 功能对这类表进行碎片整理.

②使用合成的(synthetic)索引。合成的索引列在某些时候是有用的。一种办法是根据其它的列的内容建立一个散列值,并把这个值存储在单独的数据列中。接下来你就可以通过检索散列值找到数据行了。但是,我们要注意这种技术只能用于精确匹配的查询(散列值对于类似<或>=等范围搜索操作符 是没有用处的)。我们可以使用MD5()函数生成散列值,也可以使用SHA1()或CRC32(),或者使用自己的应用程序逻辑来计算散列值。请记住数值型散列值可以很高效率地存储。同样,如果散列算法生成的字符串带有尾部空格,就不要把它们存储在CHAR或VARCHAR列中,它们会受到尾部空格去除的影响。

合成的散列索引对于那些BLOB或TEXT数据列特别有用。用散列标识符值查找的速度比搜索BLOB列本身的速度快很多。

③在不必要的时候避免检索大型的BLOB或TEXT值。例如,SELECT *查询就不是很好的想法,除非你能够确定作为约束条件的WHERE子句只会找到所需要的数据行。否则,你可能毫无目的地在网络上传输大量的值。这也是 BLOB或TEXT标识符信息存储在合成的索引列中对我们有所帮助的例子。你可以搜索索引列,决定那些需要的数据行,然后从合格的数据行中检索BLOB或 TEXT值。

④把BLOB或TEXT列分离到单独的表中。在某些环境中,如果把这些数据列移动到第二张数据表中,可以让你把原数据表中 的数据列转换为固定长度的数据行格式,那么它就是有意义的。这会减少主表中的碎片,使你得到固定长度数据行的性能优势。它还使你在主数据表上运行 SELECT *查询的时候不会通过网络传输大量的BLOB或TEXT值。

浮点数与定点数

为了能够引起大家的重视,在介绍浮点数与定点数以前先让大家看一个例子:
mysql> CREATE TABLE test (c1 float(10,2),c2 decimal(10,2));
Query OK, 0 rows affected (0.29 sec)

mysql> insert into test values(131072.32,131072.32);
Query OK, 1 row affected (0.07 sec)

mysql> select * from test;
+-----------+-----------+
| c1        | c2        |
+-----------+-----------+
| 131072.31 | 131072.32 |
+-----------+-----------+
1 row in set (0.00 sec)

从上面的例子中我们看到c1列的值由131072.32变成了131072.31,这就是浮点数的不精确性造成的。

在mysql中float、double(或real)是浮点数,decimal(或numberic)是定点数。

浮点数相对于定点数的优点是在长度一定的情况下,浮点数能够表示更大的数据范围;它的缺点是会引起精度问题。在今后关于浮点数和定点数的应用中,大家要记住以下几点:

  1. 浮点数存在误差问题;
  2. 对货币等对精度敏感的数据,应该用定点数表示或存储;
  3. 编程中,如果用到浮点数,要特别注意误差问题,并尽量避免做浮点数比较;
  4. 要注意浮点数中一些特殊值的处理。