当前位置:网站首页>MySQL blob and text types

MySQL blob and text types

2022-06-12 15:44:00 AlbenXie

One 、 Basic concepts  

1.Text type ( Large text series )===>>VARCHAR The enhancement of
          TINYTEXT: 255 Bytes                                                 TEXT:    65535 byte    

          MEDIUMTEXT:  16M                                                    LONGTEXT: 4G

   2.Blob type ( Large binary series )===>>BINARY The enhancement of

          TINYBLOB:   255 Bytes                                     BLOB:   65535 byte

           MEDIUMBLOB:  16M                                         LONGBLOB:     4G

   3. Respectively corresponding java Types and their applications

            ① Store a novel or text Type of document , Consider using TEXT series .   TEXT The series corresponds to Java Medium String type .   Just put VARCHAR Change to TEXT system   Column that will do ,Java The code doesn't need to be changed .

           ② Binary type , Mainly used to store images , Audio , Video and other secondary data . General , During development, we will not save binary data to the database , Instead, save the binary file in the database , Then find the file through the path .
 

Two 、 The main difference

1、 The main difference

TEXT And BLOB The main difference is BLOB Save binary data ,TEXT Save character data . At present, almost all the images in the blog content are not stored in the database in binary , Instead, upload the image to the server and use it in the text <img> Label reference , Such a blog can be used TEXT type . and BLOB You can convert the image into binary and save it in the database .

2、 Type difference

BLOB Yes 4 Types :TINYBLOB、BLOB、MEDIUMBLOB and LONGBLOB. They just have different maximum length of accommodation values .

TEXT Also have 4 Types :TINYTEXT、TEXT、MEDIUMTEXT and LONGTEXT. These types are the same BLOB Same type , Same maximum length and storage requirements .

3、 Character set

BLOB Column has no character set , And sort and compare numeric values based on column value bytes .TEXT Column has a character set , And sort and compare the values according to the proofreading rules of the character set

4、 Case write

stay TEXT or BLOB Column storage or retrieval process , There is no case conversion , Are all the same !

5、 Strict mode

Running in non strict mode , If you are BLOB or TEXT Column assigns a value that exceeds the maximum length of the column type , The value is truncated to ensure that it is suitable for . If the truncated character is not a space , There will be a warning . Use strictly SQL Pattern , There will be mistakes , And the value will be rejected instead of intercepted and given a warning .

6、 Other

When saving or retrieving BLOB and TEXT The value of the column does not delete trailing spaces .

about BLOB and TEXT Column index , You must specify the length of the index prefix .

BLOB and TEXT Column cannot have default value .

Only the first... Of the column is used when sorting max_sort_length Bytes .max_sort_length Of The default value is 1024.

When you want to make more than max_sort_length It's meaningful , For those with long values BLOB or TEXT Column usage GROUP BY or ORDER BY Another way to do this is to convert column values to fixed length objects . The standard method is to use SUBSTRING function .

BLOB or TEXT The maximum size of an object is determined by its type , But the maximum value that can actually be passed between the client and the server is determined by the amount of memory available and the size of the communication cache . You can change max_allowed_packet The value of the variable changes the size of the message cache , But you have to modify both the server and the client .

3、 ... and 、 Use with caution

BLOB And TEXT It's a data type designed to store huge strings , Binary and string storage .mysql These two types can be described as painstaking ,mysql These two types of values will be treated as a separate object , The storage engine usually does special processing during storage ,

When BLOB And TEXT When the value of is too large ,InnoDB Will use special “ external ” Storage area to store , In this case, each value will be taken as 1~4 A self storing pointer , Store the actual value in the external storage area .

Mysql Yes BLOB And TEXT Type is different from other types in sorting , Only for the first max_sort_length Sort by yourself , If you only need to sort the first few bytes , Then you can set max_sort_length Parameters or substring(value,length) To intercept part of the string .

These two types should be used with caution in practice , Especially when temporary tables are created , Because if the temporary table size exceeds max_heap_table_size perhaps tmp_table_size, The temporary table will be stored on disk , This leads to a decrease in the overall speed !

原网站

版权声明
本文为[AlbenXie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121528102987.html