Mysql2: Data Types

Mysql supports multiple data types, the main types of numeric, date / time, strings.

1) Value Data Type: integer types TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT

            Decimal floating-point data types FLOAT, DOUBLE

         Fixed-point decimal type DECIMAL

2) Date / Time Type: YEAR, TIME, DATE, DATETIME, TIMESTAMP

3) string type: CHAR, VARCHAR, BINARY, VARBINARY, TEXT, ENUM, SET and the like. String type is divided into text strings and binary strings.

 

Integer type  

  

 

  Different integer number of bytes required to store different types, according to the number of bytes occupied by each data type can be determined in the range of, e.g. TINYINT requiring 1 byte (8bits) storage, then its maximum unsigned 2 ** 255 i.e. 8-1; signed maximum of 127 = 2 ** 7-1.

  The following table is a range of different data types

        

 

  Data type int (11) when the added data table, 11 denotes a display width of the specified data type, the predetermined number of digital values ​​can be displayed.

 

  Display width independent of data type and value range. The number of broadband digital display may show only the largest known Mysql, populated by a space less than the specified value when the number of bits width; if the value is greater than the display width is inserted, as long as the value does not exceed the range of the type, the value is still can be inserted, it can be displayed.

  When defining the table structure, if the display width is not specified, the system defaults for each type of specified width value.

  

 

   Examples can be seen that the system for different data types specified display different widths. In Case type tinyint, tinyint signed and unsigned types are -128 to 127 and 0 to 255, since the minus sign accounts for a digit, so the default display width of 4 bits.

 

Floating-point and fixed-point

  Mysql using decimal floating-point and fixed-point represented.

  Floating-point and fixed-point type can be used (M, N) is represented, where M represents the accuracy, represented by the total number of bits; N represents a scale, shows a number of decimal places.

  

 

   

 

 

 

Date and Time Types

  

 

 YEAR:

  YEAR type is used to represent a type of single byte, during storage, only one byte can specify a value for use in a variety of formats YEAR.

  1) to 4 YEAR string or numeric format 4, the range of 1901 ~ 2155. The input format is "YYYY" or YYYY.

   

 

  2) YEAR represented in string format 2, the range of '00' ~ '99 '. '00' ~ '69' is converted to 2000 to 2069; '70' ~ '99' is converted into a range of years from 1970 to 1999. Insert more than the range of values ​​it will be converted to 2000.

   

  3) 2 to a digital representation. 99.1 ~ 69 1 ~ 2001 ~ 2069 is converted to; 70 to 99 to be converted is converted from 1970 to 1999.0 0000, 2000 instead.

  

 

TIME

  TIME type requires only a value of the time information required when storing three bytes. Format is "HH: MM: SS"

  The range is -838 TIME: 59: 59 to 838: 59: 59. Such a large part of the hour is not only because the TIME type represents a time of day can also be a time interval between the last time an event or two events. TIME may use various formats specified value.

  1) 'D HH: MM: SS' string format. D represents a day, can take a value between 0 to 34, when inserted into the database, is converted into D hours save format D * 24 + HH

  2) or a value HHMMSS format 'HHMMSS' format. Is understood to 101,211 e.g. 10:12:11. However, if the value is not valid, for example, '109 712' at this time has no meaning minutes, were understood to 00:00:00

  NOTE: If no colon, the rightmost two digits for seconds, such as 1112, mysql interpreted as 00:11:12

    If you use a colon was seen as the time of day. For example '11: 12 'is interpreted as 11:12:00

  3) System Date: CURRENT_TIME or NOW ()

   

 

 

DATA type

  DATA type is used requires only a date value, the time portion do not need to store three bytes.

  Date format is: 'YYYY-MM-DD'

  1) to the date string format 'YYYY-MM-DD' or 'YYYYMMDD', the range is '1000-01-01' ~ ~ '9999-12-31'

  2) in a date format string 'YY-MM-DD' or 'YYMMDD'. '00' ~ '69' is converted to 2000 to 2069; '70' ~ '99' is converted into a range of years from 1970 to 1999

  3) or YY-MM-DD format YYMMDD.

  4) Use the system date: CURRENT_DATE () or NOW ()

 

DATATIME type

  DATATIME type value used in the need to include the date and time information, storage requires 8 bytes.

  1) 'YYYY-MM-DD HH: MM: SS' or 'YYYYMMDDHHMMSS' value in string format.

  2) 'YY-MM-DD HH: MM: SS' date or 'YYMMDDHHMMSS' of the string format.

  3) the date and time YYYYMMDDHHMMSS YYMMDDHHMMSS or digital format.

 

TIMESTAMP

  TIMESTAMP with the same display format DATATIME, the display width is fixed YYYY-MM-DDD HH 19 characters Date format: MM: SS, requiring 4 bytes during storage.

  TIMESTAMP column is smaller than the range in the range of DATATIME, is' 1970-01-01 00:00:01 'UTC ~~' 2038-01-01 03: 14: 07'UTC. UTC is the time in which the world standard, so when you insert data, to ensure that within the legal range of values.

  DATATIME when storing date, according to store the actual input format, input what what is stored, regardless of the time zone.

  TIMESTAMP value stored is saved in UTC, current time zone conversion storage, and converted back to the current time zone upon retrieval. That is, when a query, depending on the current time zone, the time value displayed is different.

 

Text string type

 

  Used to store character string data type string, the string may be stored in addition, you can also store other data, such as binary data of pictures and sound.

  Mysql supports two types of character data: text strings and binary strings.

  

 

 

1) CHAR type VARCHAR and

  CHAR (M) is a fixed-length string, the string length of the column specified in the definition. When stored in the right padded with spaces to the specified length. M represents a column length, M ranges from 0 to 255 characters.

  VARCHAR (M) is a variable length string, M is the maximum length of the column. M ranges from 0 to 65,635.

  VARCHAR maximum practical length determined by the size of the longest line and the character set used, and the actual space occupied by the actual length of the string plus 1. For example VARCHAR (50) defines a maximum length of a string 50, If the inserted character string as long as 10 characters, the actual memory is 10 characters and the end character string. VARCHAR trailing spaces remain in the value of saving and retrieval.

 

2) TEXT type

  TEXT columns hold non-binary strings, such as articles, reviews, etc., when you save or query the value of TEXT columns, trailing spaces are not deleted. TEXT types are divided into 4 different types of storage space and data length.

  TINYTEXT maximum length of 255 (2 ** 8--1) characters TEXT column

  TEXT maximum length of 65535 (2 ** 16-1) of the character row TEXT

  MEDIUMTEXT maximum length of 16777215 (2 ** 24--1) characters TEXT column

  LONGTEXT maximum length of 4,294,967,295 (2 ** 32--1) characters TEXT column

 

3) ENUM type

  ENUM is a string object, an enumerated value in a predetermined column when a value table is created.

   grammar:

 

   Example:

  

 

 

4) SET type

  SET type is a string object, zero or more values, SET column can have up to 64 members.

  grammar:        

 

  ENUM same type, the SET in the internal integer representation, each value in the list has an index number. When you create a table, trailing spaces SET members will be automatically deleted.

  But ENUM type is different, ENUM type of field can be inserted from a selected column values ​​defined, and may be selected from a plurality of characters SET type joint.

   If you insert a SET column values ​​in field plots, the mysql automatically delete duplicate values, insert SET field value of the order is not important, Mysql data will be stored at the time, the order of definition display, if you insert an incorrect value, default case, mysql will ignore these values ​​and give a warning.

 

Binary string type

  

 

1)BIT类型

  BIT类型是位字段类型。M表示每个值得位数,范围是1~64.如果M被省略,默认为1.如果为BIT(M)列分配得值得长度小于M位,在值的左边用0填充。BIT数据类型可以用来保存字段值

  b+0表示将二进制的结果转换为对应的数字的值,bin()函数将数字转换为二进制。

  

 

 

2)BINARY和VARBINARY类型

  BINARY和VARBINARY类型类似与CHAR和VARCHAR,不同的是它们包含二进制字节字符串。

  BINARY类型的长度是固定的,指定长度之后,不足最大长度的,将在右边填充‘\0'补齐达到指定长度。

  VARBINARY类型的长度是可变的,指定长度之后,其长度可以在0到最大值之间。例如:指定列数据类型为    VARBINARY(20),如果插入的值的长度只有10,则实际存储空间为10加1,即实际占用空间为字符串的实际长度加1.

 

3)BLOB类型

  

 

 如何选择数据类型

1.整数和浮点数

  如果不需要小数部分,则使用整数来保存数据;如果需要表示小数部分,则使用浮点数类型。对于浮点数据列,存入的数值会对该列定义的小数位进行四舍五入。例如,如果列的值的范围为1~9999,若使用整数,则 MEDIUMINT UNSIGNED是最好的类型:若需要存储小数,则使用 FLOAT类型。浮点类型包括 FLOAT和 DOUBLE类型。 DOUBLE类型精度比 FLOAT类型高,因此,如要求存储精度较高时,应选择 DOUBLE类型。

2.浮点数和定点数

  浮点数 FLOAT、 DOUBLE相对于定点数 DECIMAL的优势是:在长度一定的情况下,浮点数能表示更大的数据范围,但是由于浮点数容易产生误差,因此对精确度要求比较高时,建议使用 DECIMAL来存储。

   DECIMAL在 MySQL中是以字符串存储的,用于定义货币等对精确度要求较高的数据。在数据迁移中, float(M,D)是非标准SL定义,数据库迁移可能会出现问题,最好不要这样使用。另外两个浮点数进行减法和比较运算时也容易出问题,因此在进行计算的时候,一定要小心。如果进行数值比较,最好使用 DECIMAL类型。

3.日期与时间类型

  MySQL对于不同种类的日期和时间有很多的数据类型,比如YEAR和TME。如果只需 要记录年份,则使用YEAR类型即可;如果只记录时间,只需使用TME类型。如果同时需要记录日期和时间,则可以使用 TIMESTAMP或者 DATETIME类型。

  由于TIMESTAMP列的取值范围小于 DATETIME的取值范围,因此存储范围较大的日期最好使用DATETIME。TIMESTAMP也有一个 DATETIME不具备的属性。默认的情况下,当插入一条记录但并没有指定 TIMESTAMP这个列值时,,MySQL会把 TIMESTAMP列设为当前的时间。因此当需要插入记录同时插入当前时间时,使用 TIMESTAMP是方便的。另外 TIMESTAMP在空间上比 DATETIME更有效。

4.CHAR与VARCHAR

  CHAR是固定长度,所以它的处理速度比VARCHAR的速度块, 但缺点是浪费存储空间。所以对于存储不大,但在速度上有要求的可以使用CHAR类型。

 5.ENUM和SET

  ENUM只能取单值,它的数据列表是一个枚举集合。它的合法取值列表最多允许有65535 个成员。因此,在需要从多个值中选取一个时,可以使用ENUM。比如:性别字段适合定义为ENUM类型,每次只能从“男’或‘女’中取一个值。SET可取多值。它的合法取值列表最多允许有64个成员。空字符串也是一个合法的SET值。在需要取多个值的时候,适合使用SET类型,比如:要存储一个人兴趣爱好,最好使用SET类型。ENUM和SET的值是以字符串形式出现的,但在内部,,MySQL以数值的形式存储它们。

6.BLOB和TEXT

  BLOB是二进制字符串,TEXT是非二进制字符串,两者均可存放大容量的信息。BLOB 主要存储图片、音频信息等,而TEXT只能存储纯文本文件

 

Guess you like

Origin www.cnblogs.com/juankai/p/11609580.html