Introduction to MYSQL data types

da2b688183e74b8cbb01fcab863cae6d.gif1. MySQL data types

 

It mainly includes the following five categories:

 

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

 

Floating point type: FLOAT, DOUBLE, DECIMAL

 

String type: 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 byte range (-32768~32767)

mediumint(m) 3 bytes range (-8388608~8388607)

int(m) range of 4 bytes (-2147483648~2147483647)

bigint(m) 8 byte range (+-9.22*10 to the 18th power)

If unsigned is added to the value range, the maximum value will be doubled. For example, the value range of tinyint unsigned is (0~256).

 

 The m in int(m) indicates the display width of the SELECT query result set, and does not affect the actual value range, nor does it affect the display width. I don’t know what the use of this m is.

 

 

 

2. Floating point type (float and double)

 

MySQL data type meaning

float(m,d) Single-precision floating-point 8-bit precision (4 bytes) total number of m, d decimal places

double(m,d) Double-precision floating-point 16-bit precision (8 bytes) total number of m, d decimal places

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

 

 

 

3, fixed number

 

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

 

decimal(m,d) 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 power of 32 - 1 character

char and varchar:

 

1.char(n) If the number of stored characters is less than n, fill it with a space, and remove the space when querying. Therefore, the string stored in the char type cannot have spaces at the end, and varchar is not limited to this. 

 

2. char(n) has a fixed length, char(4) will occupy 4 bytes no matter how many characters are stored, 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 string retrieval speed of char type is faster than that of 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

 

Festival. 

 

2. The text type cannot have a default value. 

 

3. varchar can directly create an index, and how many characters must be specified before creating an index for text. The varchar query is faster than the text, and the text index does not seem to work when all indexes are created.

 

 

 

5. Binary data (_Blob)

 

1. The storage methods of _BLOB and _text are different. _TEXT is stored in text mode, and English storage is case-sensitive, while _Blob is stored in binary mode, which is not case-sensitive.

 

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

 

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

 

 

 

6. Date time type

 

MySQL data type meaning

date date '2008-12-2'

time time '12:25:36'

datetime datetime '2008-12-2 22:06:44'

timestamp automatically stores 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 modification time of this record.

 

 

 

properties of the data type

 

 

 

MySQL keyword meaning

NULL data column can contain NULL value

NOT NULL data column does not allow NULL values

DEFAULT default value

PRIMARY KEY primary key

AUTO_INCREMENT auto-increment, suitable for integer types

UNSIGNED unsigned

CHARACTER SET name specifies a character set

 

 

Second, the length and range of the MYSQL data type

List of data types and byte lengths:

 

data type byte length range or usage

Bit 1 Unsigned [0,255], signed [-128,127], Tianyuan Blog Remarks: Both BIT and BOOL 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 will be 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 Unpacked floating-point numbers, the usage is similar to FLOAT and DOUBLE, Tianyuan blog reminds you that if the Decimal data type is used in ASP, the Decimal read directly from the database may need Convert to Float or Double type before performing operations.

Date 3 is displayed in the format of YYYY-MM-DD, for example: 2009-07-19

Date Time 8 is displayed in the format of YYYY-MM-DD HH:MM:SS, for example: 2009-07-19 11:22:30

TimeStamp 4 is displayed in the format of YYYY-MM-DD, for example: 2009-07-19

Time 3 is displayed in the format HH:MM:SS. For example: 11:22:30

Year 1 is displayed in YYYY format. For example: 2009

Char(M) M 

Fixed-length string.

VarChar(M) M variable length character string, M<=255 required

Binary(M) M is similar to the binary storage of Char, which is characterized by inserting 0 if the fixed length is insufficient

VarBinary(M) M is a variable-length binary storage similar to VarChar, which is characterized by fixed-length and not supplemented with 0

Tiny Text Max:255 case insensitive

Text Max: 64K case insensitive

Medium Text Max:16M case insensitive

Long Text Max: 4G case insensitive

TinyBlob Max: 255 case sensitive

Blob Max: 64K case sensitive

MediumBlob Max: 16M case sensitive

LongBlob Max: 4G case sensitive

Enum 1 or 2 up to 65535 different enumeration values

Set up to 8 up to 64 different values

Geometry    

Point    

LineString    

Polygon    

MultiPoint    

MultiLineString    

MultiPolygon    

GeometryCollection    

3. Suggestions for use

1. When specifying the data type, the small principle is generally adopted. For example, if you can use TINY INT, it is better not to use INT, and if you can use FLOAT type, you don’t need to use DOUBLE type. This will greatly improve the operating efficiency of MYSQL, especially Under the test conditions of large amount of data.

 

2. It is not necessary to design the data table too complicated. It may be more convenient to distinguish the functional modules for later maintenance. Be careful when there is a hodgepodge data table

 

3. The naming of data tables and fields is also a science

 

4. Before designing the data table structure, please imagine that it is your room, maybe the result will be more reasonable and efficient

 

5. The final design result of the database must be a compromise between efficiency and scalability, and it is inappropriate to favor either side

 

 

 

Basic principles for choosing data types

Prerequisite: Use a suitable storage engine.

 

Selection principle: According to the selected storage engine, determine how to select the appropriate data type.

 

The following selection methods are categorized by storage engine:

 

MyISAM data storage engine and data columns: For MyISAM data tables, it is best to use fixed-length (CHAR) data columns instead of variable-length (VARCHAR) data columns.

MEMORY storage engine and data columns: MEMORY data tables currently use fixed-length data row storage, so it doesn't matter whether you use CHAR or VARCHAR columns. Both are handled as CHAR type.

InnoDB storage engine and data columns: VARCHAR type is recommended.

 

For InnoDB data tables, the internal row storage format does not distinguish between fixed-length and variable-length columns (all data rows use header pointers to data column values), so in essence, using fixed-length CHAR columns is not necessarily better than using variable-length columns. Variable length VARCHAR columns are simple. Thus, the primary performance factor is the total amount of storage used by the data rows. Since CHAR takes up more space on average than VARCHAR, it is better to use VARCHAR to minimize the total amount of storage and disk I/O for the rows that need to be processed.

 

Let's talk about fixed-length data columns and variable-length data columns.

 

char and varchar

CHAR and VARCHAR types are similar, but they are stored and retrieved differently. They also differ in terms of maximum length and whether or not trailing spaces are preserved. No case conversion is done during storage or retrieval.

 

The following table shows the results of storing various string values ​​into CHAR(4) and VARCHAR(4) columns, illustrating the difference between CHAR and VARCHAR:

 

value CHAR(4) storage requirement VARCHAR(4) storage requirement

'' ' ' 4 bytes'' 1 byte

'ab' 'ab' 4 bytes 'ab' 3 bytes

'abcd' 'abcd' 4 bytes 'abcd' 5 bytes

'abcdefgh' 'abcd' 4 bytes 'abcd' 5 bytes

 

Please note that the values ​​in the last row in the above table are only applicable when strict mode is not used; if MySQL is running in strict mode, values ​​that exceed the length of the column will not be saved, and an error will occur.

 

Values ​​retrieved from CHAR(4) and VARCHAR(4) columns are not always the same because trailing spaces are removed from CHAR columns when retrieving. The difference is illustrated by the following example:

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 and blob

 

 

Pay attention to the following points when using text and blob field types, so as to better utilize the performance of the database.

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/131406621