当前位置:网站首页>Introduction to data types in MySQL

Introduction to data types in MySQL

2022-07-06 03:40:00 51CTO

1、MySQL data type

MySQL Defining the types of data fields in is very important for optimizing your database .

MySQL Support for multiple types , It can be roughly divided into three categories : The number 、 date / Time and string ( character ) type .

2、 value type (12)

    A picture can explain it clearly :

    

    INTEGER Same as INT.

    DECIMAL and NUMERIC Type in the MySQL The same type as seen in . They are used to hold values that must be of exact precision .

    Use as follows :

  1. salary DECIMAL(5,2)

    The following introduction will be based on the above example .

    We see that there are two parameters , namely DECIMAL(M,D), among M Represents the total number of decimal digits ,D The number of digits after the decimal point , The value range in the above example is -999.99~999.99.

    If in storage , The integer part is out of range ( As in the example above , Add a value of 1000.01),MySql You're going to report a mistake , It is not allowed to store such value .

    If in storage , If the decimal part is out of range , In the following cases :

  • If after rounding , The integer part is not out of range , Just a warning , But it can operate successfully and delete the extra decimal places and save it . Such as 999.994 Actually saved as 999.99.
  • If after rounding , The integer part is out of range , be MySql Report errors , And refuse to deal with . Such as 999.995 and -999.995 All will report wrong. .

    M The default value of is 10,D The default value is 0. If you create a table , A field is defined as decimal Type without any parameters , Equate to decimal(10,0). With a parameter ,D Take the default value .

  • M The value range of is 1~65, take 0 Will be set to the default value , Out of range error will be reported .
  • D The value range of is 0~30, And must <=M, Out of range error will be reported .

    therefore , Obviously , When M=65,D=0 when , Maximum and minimum values can be obtained .

    It has been explained in great detail , If not clear , Please reply .

     Floating point numbers are a way to represent real numbers , It USES M( mantissa ) * B( base ) Of E( Index ) Power to express real numbers , Relative to the fixed number of points , In the case of a certain length , It has the characteristics of representing a large range of data . But there are also errors .

    If you want to ensure that the value is accurate , Fixed point data type is recommended .

    MySql Floating point types in are float,double and real. They define it as :FLOAT(M,D) 、 REAL(M,D) 、 DOUBLE PRECISION(M,D).

    REAL Namely DOUBLE , If SQL Server mode includes REAL_AS_FLOAT Options ,REAL yes FLOAT A synonym for, not DOUBLE A synonym for .

    “(M,D)” Indicates that the value shows M An integer , among D Place after decimal point . for example , Defined as FLOAT(7,4) A column of can be displayed as -999.9999.MySQL Rounding when saving values , So if the FLOAT(7,4) Insert... In the column 999.00009, The approximate result is 999.0001.

    FLOAT and DOUBLE Medium M and D The default values of are 0, That is, except for the maximum and minimum value , No limit on the number of digits . The allowable value is theoretically -1.7976931348623157E+308~-2.2250738585072014E-308、0 and 2.2250738585072014E-308~1.7976931348623157E+308.M、D The scope is as follows (MySql5.7 Actually measured , And IEEE The standard calculation is actually different , Let's introduce ):

  • M The value range is 0~255.FLOAT Ensure that only 6 The accuracy of the significant digits , therefore FLOAT(M,D) in ,M<=6 when , The numbers are usually accurate . If M and D It's all clearly defined , The treatment after exceeding the scope is the same as decimal.
  • D The value range is 0~30, At the same time, we must <=M.double Ensure that only 16 The accuracy of the significant digits , therefore DOUBLE(M,D) in ,M<=16 when , The numbers are usually accurate . If M and D It's all clearly defined , The treatment after exceeding the scope is the same as decimal.

    FLOAT and DOUBLE in , if M The definitions of go beyond 7 and 17, Then the extra significant digits , The value is variable , Usually, numerical errors occur . Because floating point numbers are inaccurate , So we should avoid using “=” To determine whether two numbers are equal .

    MySql Floating point numbers in follow IEEE 754 standard .

    In the memory ,FLOAT Occupy 4-byte(1 Bit sign bit 8 Bitwise index 23 Bits represent mantissa ),DOUBLE Occupy 8-byte(1 Bit sign bit 11 Bitwise index 52 Bits represent mantissa ).IEEE754 The standard also regulates the format of mantissa :d.dddddd..., There is only 1 Bit and cannot be zero , Inside the computer is binary , therefore , The left part of the mantissa decimal point is always 1. obviously , This 1 It can be omitted. , To improve the accuracy of mantissa . It can be seen from the above that , The mantissa of a single precision floating-point number is 24bit It means , The mantissa of a double precision floating-point number is 53bit It means . Therefore, we can calculate the value range and the accurate significant digits , but MySql In fact, it is slightly different .

    BIT Data types can be used to hold bit field values .BIT(M) Type allows storage M A value .M The scope is 1~64, The default is 1.

    BIT It's actually a binary value , similar 010110.

    If you deposit a BIT Type value , The number of digits is less than M value , Then left complement 0.

    If you deposit a BIT Type value , There are more digits than M value ,MySQL The operation depends on the effective SQL Pattern :

  • If the mode is not set ,MySQL Crop the value to the corresponding end of the range , And save the cut value .
  • If the mode is set to traditional(“ Strict mode ”), Out of range values will be rejected with an error , And according to SQL Standard insertion will fail .

    Here's an official example :

  1. mysql>(b BIT(8));
  2. mysql>='11111111';
  3. mysql>='1010';
  4. mysql>='0101';
  5. mysql>+0,(b+0),(b+0),(b+0);
  6. +------+----------+----------+----------+
  7. |+0|(b+0)|(b+0)|(b+0)|
  8. +------+----------+----------+----------+
  9. |255|11111111|377||
  10. |10|1010|12||
  11. |5|101|5|5|
  12. +------+----------+----------+----------+

 

3、 String type (14)

     String type means CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM and SET.

    CHAR and VARCHAR The length of the type declaration represents the maximum number of characters you want to save . for example ,CHAR(30) It can occupy 30 Characters . The default length is 255.

    CHAR The length of the column is fixed to the length declared when the table is created . The length can be from 0 To 255 Any value . When saving CHAR When the value of , Fill in the spaces to the right of them to achieve the specified length . When the retrieved CHAR When the value of , The trailing space is removed , therefore , We can't have spaces to the right of the string when we store it , Even if there is , It will also be deleted after the query . No case conversion during storage or retrieval .

     So when char When a field of type is a unique value , Whether the added value already exists to exclude trailing spaces ( There may be more than one space ) The value of , When comparing, it will be compared with the existing value after filling the blank space at the end .

    VARCHAR The value in the column is a variable length string . Length can be specified as 0 To 65,535 Between the value of the ( The actual maximum length that can be specified is related to encoding and other fields , such as , I am MySql Use utf-8 Coding format , The size is standard format size 2 times , There is only one varchar The measured maximum value in the field is only 21844, If you add a char(3), Then the maximum value is reduced 3. The overall maximum length is 65,532 byte ).

    Same as CHAR contrast ,VARCHAR When the value is saved, only the number of characters needed is saved , Add another byte to record the length ( If the length of the column declaration exceeds 255, Then use two bytes ).

    VARCHAR Values are saved without padding . When the value is saved and retrieved, the trailing space remains , Meet the standard SQL.

    If assigned to CHAR or VARCHAR The value of the column exceeds the maximum length of the column , Then crop the value to fit . If the cut character is not a space , A warning will be generated . If you crop non whitespace characters , It will lead to mistakes ( Not a warning ) And by using strictly SQL Mode disable value insertion .

    The following shows how to save various string values to CHAR(4) and VARCHAR(4) The result after the column :

    

     The value of the last row in the table only applies when strict mode is not used ; If MySQL Run in strict mode , Values exceeding the column length are not saved , And there will be mistakes .

    Because of the space , The same value is stored in a length sufficient varvhar and char in , Taking out may be different , such as "a" and "a  ".

    BINARY and VARBINARY The type is similar to CHAR and VARCHAR type , But here's the difference , They don't store character strings , It's a binary string . So they have no character set , And sort and compare numeric values based on column value bytes .

     When saving BINARY When the value of , Fill them to the right 0x00( Zero byte ) Value to reach the specified length . The last byte is not deleted when the value is taken . All bytes are important when comparing ( Because of the spaces and 0x00 Is different ,0x00< Space ), Include ORDER BY and DISTINCT operation . For example, insert 'a ' Will become 'a \0'.

     about VARBINARY, Insert without filling characters , Do not crop bytes when selecting . All bytes are important when comparing .

    When the type is BINARY When the field of is the primary key , The storage method described above should be considered .

    BLOB Is a binary large object , Can hold a variable amount of data . Yes 4 Kind of BLOB type :TINYBLOB、BLOB、MEDIUMBLOB and LONGBLOB. They just have different maximum length of accommodation values .

     Yes 4 Kind of TEXT type :TINYTEXT、TEXT、MEDIUMTEXT and LONGTEXT. These correspondences 4 Kind of BLOB type , Same maximum length and storage requirements .

    BLOB Columns are treated as binary strings .TEXT Columns are treated as character strings , similar CHAR and BINARY.

     stay TEXT or BLOB Column storage or retrieval process , There is no case conversion .

     When not running in 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 .

     In most ways , Can be BLOB A column is considered to be large enough VARBINARY Column . Again , Can be TEXT Column as VARCHAR Column .

    BLOB and TEXT It is different from VARBINARY and VARCHAR:

  • When saving or retrieving BLOB and TEXT The value of the column does not delete trailing spaces .( This is related to VARBINARY and VARCHAR The columns are the same ).
  • When comparing, you will use spaces to TEXT Expand to fit the object of comparison , just as CHAR and VARCHAR.
  • about BLOB and TEXT Column index , You must specify the length of the index prefix . about CHAR and VARCHAR, Prefix length is optional .
  • BLOB and TEXT Column cannot have default value .

    MySQL Connector/ODBC take BLOB Value is defined as LONGVARBINARY, take TEXT Value is defined as LONGVARCHAR.

    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 .

     Every BLOB or TEXT Values are represented by internally assigned objects .

    they (TEXT and BLOB Same as ) The length of :

  • Tiny: Maximum length 255 Characters (2^8-1)
  • BLOB or TEXT: Maximum length 65535 Characters (2^16-1)
  • Medium: Maximum length 16777215 Characters (2^24-1)
  • LongText Maximum length 4294967295 Characters (2^32-1)

    The actual length is related to the coding , such as utf-8 It's going to be halved .

    MySql Medium ENUM Is a string object , Its value comes from a column value explicitly enumerated in the column specification when the table is created .

    You can insert an empty string "" and NULL:

  • If you insert an illegal value ENUM( in other words , String other than allowed value column ), An empty string will be inserted as a special error value . The string is associated with “ Ordinary ” Empty string is different , The string has a numeric value 0.
  • If you will ENUM Column is declared as allowed NULL,NULL Value is a valid value of the column , And the default value is NULL. If ENUM The column is declared as NOT NULL, Its default value is the... Of the allowed value column 1 Elements .

    The indexing rules for values are as follows :

  • From the allowed values specified in the column, the values in the column are from 1 Numbered starting .
  • The index value of the null string error value is 0. therefore , You can use the following SELECT Statement to find out that the allocation is illegal ENUM Row of values :mysql> SELECT * FROM tbl_name WHERE enum_col=0;
  • NULL The index of values is NULL.

    Here's an example :

    

    ENUM Up to 65,535 Elements . When you create a table ,ENUM The trailing space of the member value will be automatically removed .

    Usage mode :

  1. CREATE TABLE shirts (
  2. name VARCHAR(40),
  3. size ENUM('x-small','small','medium','large','x-large')
  4. );
  5. INSERT INTO shirts (name,)('dress shirt','large'),('t-shirt','medium'),('polo shirt','small');
  6. SELECT name,='medium';
  7. UPDATE shirts SET size ='small'='large';

    If the return value is set to a value , The index value will be returned , For example, the above query statement is changed to :

  1. SELECT name,+0='medium';

     If you save a number to ENUM Column , Numbers are treated as indexes , And the saved value is the enumeration member corresponding to the index ( It doesn't fit LOAD DATA, It treats all input as strings ). It is not recommended to use enumeration values like numbers to define a ENUM Column , Because it is easy to cause confusion .

    ENUM Values are sorted by index number ). for example , about ENUM('a','b'),'a' be ranked at 'b' front , But for the ENUM('b','a'),'b' be ranked at 'a' front . Empty strings precede non empty strings , also NULL Value comes before all other enumerated values . To prevent unexpected results , Specify in alphabetical order ENUM Column . You can also use GROUP BY CAST(col AS CHAR) or GROUP BY CONCAT(col) To ensure that columns are sorted by vocabulary instead of index numbers .

    SET Is a string object , Can have zero or more values , Its value comes from the allowable column of values specified when the table is created . The specification includes multiple SET Members of the SET Use commas between members when column values (‘,’) Interval on . for example , Designated as SET('one', 'two') NOT NULL The column of can have any of the following values :

  • ''
  • 'one'
  • 'two'
  • 'one,two'

    SET At most... Can be set 64 It's worth . Create table time ,SET The trailing space of the member value will be automatically removed . Search time , Save in SET The value of the column is displayed using the case used in the column definition .

    MySQL Save in numbers SET value , The lower order of the saved value corresponds to 1 individual SET member . If you retrieve a SET value , The bit setting of the retrieved value corresponds to the SET member .

    for example , You can do this from a SET Column retrieves numeric values :

  1. mysql>+0;

     If you save a number to SET In the column , Digital, binary 1 The position of determines SET member . For specified as SET('a','b','c','d') The column of , Members have the following decimal and binary values :

    

     If you assign a value to this column 9, Its binary form is 1001, So the first 1 And the first 4 individual SET Value members 'a' and 'd' Selected , The result value is 'a,d'.

    For multiple SET The value of the element , When inserting values, the order in which the elements are listed is not important . It doesn't matter how many times a given element is listed in the value . When the value is retrieved later , Once for each element in the value , List the elements according to the order specified when the table is created . for example , Suppose a column is specified as SET('a','b','c','d'):

  1. CREATE TABLE myset (col SET('a','b','c','d'));
  2. INSERT INTO myset (col)('a,d'),('d,a'),('a,d,a'),('a,d,d'),('d,a,d');
  3. SELECT *,col+0;
  4. SELECT *,col+0where='a,b';

    result :

  1. a,d 9
  2. a,d 9
  3. a,d 9
  4. a,d 9
  5. a,d 9

    SET Values are sorted in numerical order .NULL Value ranks in non NULL SET Before value .

    General situation , have access to FIND_IN_SET() Function or LIKE Operator search SET value :

    mysql> SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;

    mysql> SELECT * FROM tbl_name WHERE set_col LIKE '%value%';

    The first 1 Find SET_col contain value set Member's row . The first 2 A similar one , But it's different : It finds out elsewhere set_col contain value The line of , Even in another SET Member's substring .

    The following statement is also legal :

    mysql> SELECT * FROM tbl_name WHERE set_col & 1;

    mysql> SELECT * FROM tbl_name WHERE set_col = 'val1,val2';

    The first 1 A statement looks for a containing 1 individual set Members of the value of the . The first 2 Statements to find an exact matching value . Attention should be paid to section 2 Class comparison . take set Value and 'val1,val2' Compare the returned results with 'val2,val1' The results returned from the comparison are different . The order of specifying values should be the same as that listed in the column definition .

    If you want to SET Column determines all possible values , Use SHOW COLUMNS FROM tbl_name LIKE set_col And parse the... In the output 2 Column SET Definition .

     What practical applications are there ?

    For example, we set the permission control of users , A user may have multiple permissions , We use all permissions to create a SET Type field , We don't need to use a series int To define various permissions , Use one... Directly SET Field can be :

  1. /*
  2. User permissions permission surface
  3. */
  4. create table user_permission(
  5. id intnotnull,
  6. user_id intnotnull,
  7. permission set(' read ',' Comment on ',' Post ')notnull,
  8. primary key(id),
  9. unique (user_id)
  10. );
  11. desc user_permission;
  12. insert into(0,1,' read '),(0,2,' read '),(0,3,' read , Comment on ');
  13. insert into(0,4,' read , Comment on , Post ');
  14. select*,permission+0from;
  15. selectfromwhere=1;
  16. select*fromwhere&10;
  17. SELECT *(' Comment on ',permission)>0;

 

4、 Time date type (5)

    their “0” Values are as follows :

    

    These three are actually related , Are used to indicate a date or time .

     Use... When you need values that contain both date and time information DATETIME type .MySQL With 'YYYY-MM-DD HH:MM:SS' Format search and display DATETIME value . The scope of support is '1000-01-01 00:00:00' To '9999-12-31 23:59:59'.

    Use... When you only need the date value and not the time part DATE type .MySQL use 'YYYY-MM-DD' Format search and display DATE value . The scope of support is '1000-01-01' To '9999-12-31'.

    TIMESTAMP Type also includes date and time , Range from '1970-01-01 00:00:01' UTC To '2038-01-19 03:14:07' UTC.

     You can specify DATETIME、DATE and TIMESTAMP value :

  • 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' Format string . allow “ Not strictly ” grammar : Any punctuation mark can be used as a separator between date parts or time parts . for example ,'98-12-31 11:30:45'、'98.12.31 11+30+45'、'98/12/31 11*30*45' and '[email protected]@31 11^30^45' It is equivalent. .
  • 'YYYY-MM-DD' or 'YY-MM-DD' Format string . It is also allowed to use “ Not strictly ” grammar . for example ,'98-12-31'、'98.12.31'、'98/12/31' and '[email protected]@31' It is equivalent. .
  • YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS' Format string without delimiter , Assume that the string is meaningful for the date type . for example ,'19970523091528' and '970523091528' Be interpreted as '1997-05-23 09:15:28', but '971122129015' It's illegal ( It has a meaningless minute part ), Will be '0000-00-00 00:00:00'.
  • 'YYYYMMDD' or 'YYMMDD' Format string without delimiter , Assume that the string is meaningful for the date type . for example ,'19970523' and '970523' Be interpreted as '1997-05-23', but '971332' It's illegal ( It has a meaningless month and day part ), Will be '0000-00-00'.
  • YYYYMMDDHHMMSS or YYMMDDHHMMSS Number in format , Assume that numbers are meaningful for date types . for example ,19830905132800 and 830905132800 Be interpreted as '1983-09-05 13:28:00'.
  • YYYYMMDD or YYMMDD Number in format , Assume that numbers are meaningful for date types . for example ,19830905 and 830905 Be interpreted as '1983-09-05'.
  • The result returned by the function , Its value is suitable for DATETIME、DATE perhaps TIMESTAMP Context , for example NOW() or CURRENT_DATE.

     For string values that include date part delimiters , If the value of day and month is less than 10, You do not need to specify two digits .'1979-6-9' And '1979-06-09' It's the same . Again , For string values that include time part delimiters , If the time is 、 The values of minutes and seconds are less than 10, You do not need to specify two digits .'1979-10-30 1:2:3' And '1979-10-30 01:02:03' identical .

     The numeric value should be 6、8、12 perhaps 14 Bit length . If it is a value 8 or 14 Bit length , Then assume YYYYMMDD or YYYYMMDDHHMMSS Format , front 4 The number of digits represents the year . If the number yes 6 or 12 Bit length , Then assume YYMMDD or YYMMDDHHMMSS Format , front 2 The number of digits represents the year . Other numbers are interpreted as if filled with zeros to the nearest length .

     The value specified as a non qualifier string is interpreted with the given length . If the string is 8 or 14 Characters long , front 4 The number of digits represents the year . otherwise , front 2 The number of digits represents the year . Explain the parts appearing in the string from left to right , In the year of discovery 、 month 、 Japan 、 Hours 、 Minutes and seconds . This indicates that less than... Should not be used 6 String of characters . for example , If you specify '9903', Think it means 1999 year 3 month ,MySQL Will insert a “ zero ” Date value . This is because the annual and monthly values are 99 and 03, But the Japanese part is completely lost , Therefore, this value is not a legal date . however , You can obviously specify a zero value to represent the missing month or day part . for example , have access to '990300' To insert values '1999-03-00'.

    You can assign the value of a date type to a different date type . however , The value may change or lose some information :

  • If you are for a DATETIME or TIMESTAMP Object to assign a DATE value , The time part of the result value is set to '00:00:00', because DATE Value does not contain time information .
  • If you are for a DATE Object to assign a DATETIME or TIMESTAMP value , The time part of the result value is deleted , because DATE Value does not contain time information .
  • Remember that although you can specify in the same format DATETIME、DATE and TIMESTAMP value , Different types of values have different ranges . for example ,TIMESTAMP Value can't be earlier than 1970 Or later than 2037. This indicates a date , for example '1968-01-01', Although for DATETIME or DATE The value is valid , But for the TIMESTAMP The value is invalid , If assigned to such an object, it will be converted to 0.

    Please pay attention to some defects when specifying the date value :

  • The value specified as a string may be tricked by the allowed non strict format . for example , value '10:11:12' because ‘:’ The secant may look like a time value , But if used for date context value, it is interpreted as year '2010-11-12'. value '10:45:15' Converted to '0000-00-00' because '45' Not a legal month .
  • In the non strict mode ,MySQL The server only checks the validity of the date : year 、 The range of month and day is 1000 To 9999、00 To 12 and 00 To 31. Any date that contains parts beyond these ranges is converted to '0000-00-00'. Please note that you are still allowed to save illegal dates , for example '2002-04-31'. To ensure that dates are valid when strict mode is not used , The application should be checked . In strict mode , Illegal dates are not accepted , And do not convert .
  • Dates with two digit year values can be confusing , Because century doesn't know .MySQL Use the following rules to interpret two digit year values : o 00-69 The annual value of the range is converted to 2000-2069. o 70-99 The annual value of the range is converted to 1970-1999.

    Various related operations :

    MySQL With 'HH:MM:SS' Format search and display TIME value ( Or for large hourly values 'HHH:MM:SS' Format ).

    TIME The range of values can be from '-838:59:59' To '838:59:59'. The reason why the hour part is so big is TIME Type can be used not only to represent the time of day ( Must be less than 24 Hours ), It may also be the time in the past of an event or the time interval between two events ( Can be greater than 24 Hours , Or even negative ).

    You can specify in various formats TIME value :

  • 'D HH:MM:SS.fraction' Format string . You can also use any of the following “ Not strictly ” grammar :'HH:MM:SS.fraction'、'HH:MM:SS'、'HH:MM'、'D HH:MM:SS'、'D HH:MM'、'D HH' or 'SS'. here D Means day , Can take 0 To 34 Between the value of the . Please note that MySQL Don't save the score .
  • 'HHMMSS' Format string without delimiter , Suppose it's a meaningful time . for example ,'101112' To be understood as '10:11:12', but '109712' It's illegal ( It has a meaningless minute part ), Will be '00:00:00'.
  • HHMMSS The numerical value of the scheme , Suppose it's a meaningful time . for example ,101112 To be understood as '10:11:12'. The following format can also be understood :SS、MMSS、HHMMSS、HHMMSS.fraction. Please note that MySQL Don't save the score .
  • The result returned by the function , Its value is suitable for TIME Context , for example CURRENT_TIME.

    For strings specified as including the time part delimiter TIME value , If the time is 、 The value of minute or second is less than 10, There is no need to specify two digits .'8:3:2' And '08:03:02' identical .

    by TIME When assigning abbreviated values to columns, you should pay attention to . No colon ,MySQL When interpreting the value, it is assumed that the rightmost two digits represent seconds .(MySQL explain TIME The value is the time in the past, not the time of the day ). for example , You might think '1112' and 1112 Express '11:12:00'(11 Click over 12 branch ), but MySQL Interpret them as '00:11:12'(11 branch ,12 second ). Again ,'12' and 12 Be interpreted as '00:00:12'. contrary ,TIME If a colon is used in the value, it must be regarded as the time of the day . in other words ,'11:12' Express '11:12:00', instead of '00:11:12'.

    beyond TIME Range, but the legal value is cut to the closest endpoint of the range . for example ,'-850:00:00' and '850:00:00' Converted to '-838:59:59' and '838:59:59'.

    Invalid TIME The value is converted to '00:00:00'. Please note that due to '00:00:00' Itself is a legal TIME value , Only one saved from the table '00:00:00' The value cannot say that the original value is '00:00:00' Or illegal value .

    YEAR Type is a single byte type used to represent year .

    MySQL With YYYY Format search and display YEAR value . The scope is 1901 To 2155.

    You can specify various formats YEAR value :

  • Four bit string , The scope is '1901' To '2155'.
  • Four digit number , The scope is 1901 To 2155.
  • Two bit string , The scope is '00' To '99'.'00' To '69' and '70' To '99' The value of the range is converted to 2000 To 2069 and 1970 To 1999 Scope YEAR value .
  • Two digit integer , The scope is 1 To 99.1 To 69 and 70 To 99 The value of the range is converted to 2001 To 2069 and 1970 To 1999 Scope YEAR value . Note that the two digit integer range is slightly different from the two digit string range , Because you can't directly specify zero as a number and interpret it as 2000. You must specify it as a string '0' or '00' Or it is interpreted as 0000.
  • The result returned by the function , Its value is suitable for YEAR Context , for example NOW().

    illegal YEAR The value is converted to 0000.

 

5、 Geometric type (8)

    The geometry type hierarchy is as follows :

    

    Let's talk about it when we use it .

    Official documents :​ ​http://dev.mysql.com/doc/refman/5.7/en/opengis-geometry-model.html​

 

6、 Storage occupied by various types

    

    Fixed point numbers are special , And it is also related to the specific version , It is explained separately here :

     Use binary format to 9 Decimal system ( be based on 10) Number compressed to 4 In bytes DECIMAL The column value . The storage of the integer and fractional parts of each value is determined separately . Every 9 Multiples of digits need 4 Bytes , also “ remainder ” You need 4 Part of a byte . The following table shows the storage requirements beyond the digits :

    

    

    From version 5.6.4 Start , Storage requirements change , Depending on the accuracy . The storage required for the uncertain part is as follows :

    

    such as ,TIME(0), TIME(2), TIME(4), and TIME(6) Separate use 3, 4, 5, 6 bytes.

    

 

7、 The choice of type

     To optimize storage , In any case, the most precise type should be used .

    for example , If the range of values of the column is from 1 To 99999, If you use integers , be MEDIUMINT UNSIGNED It's a good type . Among all types that can represent the value of this column , This type uses the least storage .

    The accuracy is 65 A decimal number ( be based on 10) Yes DECIMAL Column for all basic calculations (+、-、*、/).

    Use double precision operation to DECIMAL Values are calculated . If accuracy is not too important or if speed is the highest priority ,DOUBLE Type is enough . In order to achieve high precision , Can be converted to save in BIGINT The fixed point type in . So you can use that 64 Bit integer for all calculations , Convert the results back to floating-point values as needed .

 

8、 Using other databases SQL sentence

     In order to use SQL Execute code ,MySQL Map the column types as shown in the following table . Through these mappings , You can easily import table definitions from other database engines into MySQL in :

原网站

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