当前位置:网站首页>MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string

MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string

2022-06-13 03:23:00 koping_ wu

1、Mysql Data types in

 Insert picture description here
The properties of common data types are as follows :
 Insert picture description here

2、 Integer types

2.1 Type introduction

Integer types have a total of 5 Kind of , Include TINYINT、SMALLINT、MEDIUMINT、INT(INTEGER) and BIGINT.
 Insert picture description here

3、 Floating point type

3.1 Type introduction

   Floating point number and fixed-point number types are characterized by Deal with decimals , You can think of integers as a special case of decimals . therefore , Usage scenarios of floating-point numbers and fixed-point numbers , Much larger than integers . MySQL Supported floating point number types , Namely FLOAT、DOUBLE、REAL.

  • FLOAT Represents a single precision floating-point number ;
  • DOUBLE Represents a double precision floating-point number ;
     Insert picture description here

   problem 1:FLOAT and DOUBLE What is the difference between these two data types ?
  FLOAT Less bytes , The value range is small ;DOUBLE Take up too many bytes , The value range is also large .

   problem 2: Why is the value range of unsigned number of floating-point number type , It is only half of the value range of signed numbers , That is, it is only equivalent to the part of the signed number whose value range is greater than or equal to zero ?
  MySQL The format for storing floating-point numbers is : Symbol (S) 、 mantissa (M) and Order code (E) . therefore , With or without symbols ,MySQL All floating-point numbers will store the part representing the symbol . therefore , The so-called unsigned number value range , In fact, it is the part of the signed number whose value range is greater than or equal to zero .

3.2 Accuracy error description

  MySQL use 4 Byte store FLOAT Type data , use 8 Bytes to store DOUBLE Type data . No matter which , They are stored in binary mode . such as 9.625, Express in binary , Namely 1001.101, Or expressed as 1.001101×2^3.
   therefore , If the mantissa is not 0 or 5( such as 9.624), You can't use a binary number to express . , in turn, , It has to be rounded within the allowable range .
   In programming , If floating-point numbers are used , Pay special attention to the error , Because floating point numbers are inaccurate , So we should avoid using “=” To determine whether two numbers are equal . meanwhile , In some projects that require high accuracy , Never use floating point numbers , Otherwise, the result will be wrong , Even cause irreparable losses . that ,MySQL Is there a precise data type ? Of course. , This is the fixed-point number type : DECIMAL .

4、 Fixed point number type

  MySQL The fixed-point number type in is only DECIMAL One type .
 Insert picture description here
   Use DECIMAL(M,D) High precision decimals are represented in the form of . among ,M It is called precision ,D It's called scaling .0<=M<=65,
0<=D<=30,D<M
. for example , Definition DECIMAL(5,2) The type of , Indicates that the value range of this column is -999.99~999.99.

  • DECIMAL(M,D) The maximum value range of is the same as DOUBLE Same type , But the valid data range is determined by M and D Decisive .
    DECIMAL The storage space is not fixed , By the precision value M decision , The total storage space occupied is M+2 Bytes
    . in other words , In some scenes with low accuracy requirements , Compared with the fixed-point number occupying the same byte length , The range of values expressed by floating-point numbers can be larger .
  • The fixed point number is in MySQL The interior is made up of character string Storage in the form of , This determines that it must be accurate .
  • When DECIMAL When the type does not specify precision and scale , His tacit view is that DECIMAL(10,0). When the precision of the data exceeds the precision range of the fixed-point number type , be MySQL Rounding is also performed .
  • Floating point numbers vs Fixed-point number : The advantage of floating-point numbers over fixed-point numbers is that when the length is fixed , Floating point type has a wide range of values , But it's not accurate , It is suitable for large range of values , It can also tolerate small errors in scientific computing scenarios ( Like computational chemistry 、 Molecular modeling 、 Fluid dynamics, etc ). The value range of fixed-point number type is relatively small , But accuracy , There is no error , It is suitable for scenes with high precision requirements ( For example, scenarios involving amount calculation ).

5、 A type of

BIT Type stores binary values , similar 010110.
 Insert picture description here
   Date and time are important information , In our system , Almost all data sheets can be used . The reason is that the customer needs to know the time stamp of the data , So as to query the data 、 Statistics and processing .
  MySQL There are many data types that represent dates and times , Different versions may be different ,MySQL8.0 The date and time types supported by the version mainly include :YEAR type 、TIME type 、DATE type 、DATETIME The type and TIMESTAMP type .

  • YEAR Type is usually used to represent year .
  • DATE Type is usually used to represent year 、 month 、 Japan .
  • TIME Types are often used to indicate when 、 branch 、 second .
  • DATETIME Type is usually used to represent year 、 month 、 Japan 、 when 、 branch 、 second .
  • TIMESTAMP Type is usually used to represent a year with a time zone 、 month 、 Japan 、 when 、 branch 、 second .

 Insert picture description here
   among TIMESTAMP and DATETIME The difference between :

  • TIMESTAMP The storage space is small , The date and time range represented is also relatively small
  • The underlying storage methods are different ,TIMESTAMP The underlying storage is the millisecond value , distance 1970-1-1 0:0:0 0 The millisecond value of milliseconds .
  • When two dates are compared in size or date calculation ,TIMESTAMP More convenient 、 faster .
  • TIMESTAMP It's about time zone .TIMESTAMP It will vary according to the user's time zone , Show different results . and DATETIME It can only reflect the local time zone at the time of insertion , People in other time zones are bound to have errors when viewing data .

   The most used date time type , Namely DATETIME . although MySQL Also support YEAR( year )、 TIME( Time )、DATE( date ), as well as TIMESTAMP type , however In the actual project , As far as possible with DATETIME type . Because this data type includes complete date and time information , The range of values is also the largest , Easy to use . After all , If the date and time information is scattered in several fields , It's not easy to remember , And when you query ,SQL Statements will also be more complex .

6、 Text string type

   In the actual project , We also often encounter a kind of data , String data .
  MySQL in , Text strings are generally divided into CHAR 、 VARCHAR 、 TINYTEXT 、 TEXT 、 MEDIUMTEXT 、LONGTEXT 、 ENUM 、 SET Other types .
   Insert picture description here

6.1 CHAR And VARCHAR type

CHAR and VARCHAR Types can store shorter strings .
 Insert picture description here

6.2 TEXT type

stay MySQL in ,TEXT A string used to hold text types , A total of 4 Types , Respectively TINYTEXT、TEXT、MEDIUMTEXT and LONGTEXT type .

In the TEXT Type of field when saving and querying data , The system automatically stores the data according to the actual length , There's no need to predefine the length . This and VARCHAR The same type .

Each of these TEXT The data length saved by the type is different from the storage space occupied , as follows :
 Insert picture description here

7、 Binary string type

MySQL The binary string type in mainly stores some binary data , For example, you can store pictures 、 Binary numbers such as audio and video
According to the .

MySQL Binary string types supported in mainly include BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB and LONGBLOB type .

BINARY and VARBINARY Be similar to CHAR and VARCHAR, They just store binary strings .
 Insert picture description here

BLOB It's a Binary big object , Can hold a variable amount of data .

MySQL Medium BLOB Types include TINYBLOB、BLOB、MEDIUMBLOB and LONGBLOB 4 Types , The maximum length they can accommodate values is different . Can store a large binary object , such as picture 、 Audio and video etc. .

TEXT and BLOB Precautions for use of :
In the use of text and blob Pay attention to the following points when field type , In order to better play the database performance .

  1. BLOB and TEXT Value also causes some problems of its own , Especially when there are a lot of delete or update operations . Deleting this value leaves a lot of... In the data table " empty ", Fill in these later " empty " The length of the records may be different . To improve performance , It is recommended to use... Regularly OPTIMIZE TABLE This function can be used to modify this kind of table Defragmentation .
  2. If you need to fuzzy query large text fields ,MySQL Provides Prefix index . But it's still important to avoid retrieving large BLOB or TEXT value . for example ,SELECT * Query is not a good idea , Unless you can be sure of WHERE Clause will only find the required data row . otherwise , You may be able to transfer a large number of values on the network without any destination .
  3. hold BLOB or TEXT Column Separate into separate tables in . In some circumstances , If you move these data columns to the second data table , It allows you to convert data columns in the original data table into fixed length data row format , So it makes sense . at present Reduce fragmentation in the main table , Give you the performance advantage of fixed length data rows . It also allows you to run... On the main data table SELECT * When querying, it will not transmit a large number of BLOB or TEXT value .

   therefore , In practice , Often not in MySQL Use in the database BLOB Type stores large object data , Usually the picture 、 Audio and video files are stored in On the server's disk , And put the picture 、 The access paths of audio and video are stored in MySQL in .

8、 Summary

   When defining data types , If it is Integers , Just use INT ; If it is decimal , Be sure to use the fixed-point number type DECIMAL(M,D) ; If it's a date and time , Just use DATETIME .
   The advantage of this is , First, make sure your system doesn't make mistakes because of data type definition . however , Everything has two sides , Good reliability , It doesn't mean efficient . such as ,TEXT Although easy to use , But not as efficient as CHAR(M) and VARCHAR(M).
   About the choice of string , It is recommended to refer to Alibaba's 《Java Development Manual 》 standard :

  • If any field is non negative , Must be UNSIGNED
  • mandatory 】 Decimal type is DECIMAL, No use FLOAT and DOUBLE. explain : At the time of storage ,FLOAT and DOUBLE There is a problem of precision loss , Most likely when comparing values , Get the wrong result . If the range of data stored exceeds DECIMAL The scope of the , It is recommended to divide the data into integers and decimals and store them separately .
  • mandatory 】 If the stored string length is almost equal , Use CHAR Fixed length string type .
  • mandatory 】VARCHAR Is a variable length string , Do not pre allocate storage space , Do not exceed the length 5000. If the storage length is greater than this value , Define field type as TEXT, Come up with a table , Match with primary key , Avoid affecting the index efficiency of other fields .
原网站

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