Overview of MySQL database data types

Overview of MySQL database data types

The official MySQL data type document  https://dev.mysql.com/doc/refman/8.0/en/data-types.html  , here is a brief summary and introduction of commonly used ones.

1. Integer type

type name

Storage requirements

 Value range (signed)

tinyint(m)

1 byte

 -128~127

smallint(m)

2 bytes

 -32768~32767

mediumint(m)

3 bytes

 -8388608~8388607

int(m)

4 bytes

 -2147483648~2147483647

bigint(m)

8 bytes

 -9223372036854775808~9223372036854775807

m represents the display width specified by the data type, and specifies the number of digits in the numeric value that can be displayed. For example, the definition: year int(4), declares a field that only displays 4 digits width to indicate the year.

The display width and the value range of the data type are irrelevant.

 

2. Floating-point data type and fixed-point number type

MySQL uses floating-point numbers and fixed-point numbers to represent decimals. They can all be represented by (m, n), where m is called the precision, which represents the total number of digits; n is called the scale, which is the number of decimal places.

type name

Storage requirements

Value range (signed)

float(m,d)

Single-precision floating-point type, 4 bytes

-3.402823466E + 38 ~ -1.175494351E-38

double(m,d)

Double-precision floating-point type, 8 bytes

-1.7976931348623157E + 308 ~ -2.2250738585072014E-308

decimal(m,d)

Fixed point. If M>D, it is M+2 otherwise D+2 depends on the value of M and D depends on the decimal value of M and D

The possible maximum value range is the same as double

Floating-point types store approximate values ​​in the database, while fixed-point types store exact values ​​in the database. Regardless of the fixed-point or floating-point type, if the precision specified by the user exceeds the precision range, it will be rounded up for processing.

 

3. Date and time type

When only the year information is recorded, only the YEAR type can be used instead of the DATE type. When specifying an illegal value, the system inserts a "zero" value into the database.

type name

Date format

Ranges

 Storage requirements

year

YYYY

1901~2155

1 byte

time

HH:MM:SS

-838:59:59~838:59:59

3 bytes

date

YYYY-MM-DD

1000-01-01~9999-12-31

3 bytes

datetime

YYYY-MM-DD HH:MM:SS 

1000-01-01 00:00:00~9999-12-31 23:59:59

8 bytes 

timestamp

YYYY-MM-DD HH:MM:SS

1970-01-01 00:00:01~2038-01-19 03:14:07

4 bytes

The reason why the value range of the time type is so large is that time can not only represent the time of a day, but also a large time period.

The timestamp value is stored in UTC (Universal Standard Time) format. The current time zone is converted during storage, and then converted to the current time zone during retrieval. That is, when querying, the displayed time value is different according to the current time zone.

 

4. String

type name

Description

 Storage requirements

char(m)

Fixed-length string

m bytes, 1<=m<=255

varchar(m) 

Variable length string

L+1 byte, where L<=m and 1<=m<=255

tinytext 

Very small string

L+1 byte, where L<2^8

text 

Small string

L+2 bytes, where L<2^16

mediumtext 

Medium-sized string

L+3 bytes, where L<2^24

longtext 

Large string 

L+4 bytes, where L<2^32

enum

Enumeration type, there can only be one enumeration string value

1 or 2 bytes, depending on the number of enumerated values ​​(maximum 65535)

set 

A set, the string object can have zero or more set members 

1, 2, 3, 4 or 8 bytes, depending on the number of set members (up to 64 members) 

The enum column always has a default value. If the enum column is declared as null, the null value is a valid value for the column, and the default value is null. If the enum column is declared as not null, its default value is the first element of the list of allowed values.

例:enum(“member1″, “member2″, … “member65535″)

The enum data type defines an enumeration that contains up to 65535 different members. When an enum column is defined, the value of the column is limited to the value declared in the column definition. If the column declaration contains a NULL attribute, then NULL will be considered a valid value and is the default value. If NOT NULL is specified, the first member of the list is the default value.

set(“member”, “member2″, … “member64″)

The set data type provides a way to specify zero or more values ​​in a set of predefined values, which can include up to 64 members. The selection of values ​​is limited to the values ​​declared in the column definition.

 

5. Binary Type

type name

Description

 Storage requirements

bit(m)

Bit field type

Approximately (m+7)/8 bytes

binary(m) 

Fixed-length binary string

m bytes

varbinary(m)

Variable-length binary string

m+1 bytes

tinyblob(m)

Very small blob

L+1 byte, where L<2^8

blob(m)

Little BLOB

L+2 bytes, where L<2^16

mediumblob(m)

Medium-sized BLOB

L+3 bytes, where L<2^24

longblob(m)

Very large blob

L+3 bytes, where L<2^32

The BLOB column stores binary large objects (byte strings); the TEXT column stores non-binary strings (character strings). BLOB columns do not have a character set, and sorting and comparison are based on the value of the value byte; TEXT columns have a character set, and values ​​are sorted and compared according to the character set.

 

Description of the auto_increment attribute

auto_increment can use this attribute to generate a unique identifier for a new row

auto_increment can assign a unique integer identifier to the newly inserted row. Assigning this attribute to the column will assign each newly inserted row the value of the last inserted ID+1.

MySQL requires the auto_increment attribute to be used for the column as the primary key. In addition, only one auto_increment column is allowed per table. E.g:

id smallint not null auto_increment primary key

You can use the LAST_INSERT_ID() SQL function to query the latest AUTO_INCREMENT value.

 

如何选择合适的MySQL数据类型
数据类型的合理选择,有利于我们在检索时,更快的获取数据,或者更精确的进行计算。这个问题不简单,牵扯很大,如:使用适合存储引擎、具体存储的特点、实现的意图等,需要综合考虑和实践经验,在此就不多介绍了,可参见: https://www.jianshu.com/p/93d91f5192a0

 

Guess you like

Origin blog.csdn.net/cnds123/article/details/114648568