当前位置:网站首页>MySQL - data type

MySQL - data type

2022-06-21 13:35:00 Longbow dog learning C

Catalog

Data type classification

bit type

Decimal type

String type

Time type

 enum and set


Data type classification

There are four types

  • value type
  • Text binary type
  • Time date
  • string type

  We need to understand value type What are the different allowable maximum and minimum values

  Let's do some cross-border tests :

  We can find out , Shaping is signed by default . So how to create unsigned ?

bit type

//bit Basic grammar 
bit[(M)]:  Bit field type .M Represents the number of digits in each value , Range from 1 To 64. If M Be ignored , The default is 1.

ask :  Why? a The corresponding value is not displayed ?

because bit When the field is displayed , Is in accordance with the ASCII The value corresponding to the code displays .

Decimal type

In numeric types , among float The type and decimal Type belongs to decimal type .

//float grammar 
float[(m, d)] [unsigned] : M Specify the display length ,d Specify the number of decimal places , Occupancy space 4 Bytes 

such as float(4,2) The range of representation is -99.9~99.99.

We can see from the inserted values ,float It will be rounded when saving .

When float Is unsigned ,float(4,2)unsigned The scope is 0~99.99

 

//decimal grammar 
decimal(m, d) [unsigned] :  Fixed-point number m Specify the length ,d Number of decimal places 

such as decimal(5,2) The range of representation is -999.99~999.99

Above ,decimal(5,2) unsigned Range of representation 0 ~ 999.99

look decimal and float Very similar , Let's see the difference between the two

String type

String types are char and varchar

//char grammar 
char(L):  Fixed length string ,L Is the length that can be stored , Unit as character , The maximum length value can be 255

//varchar grammar 
varchar(L):  Variable length string ,L Represents character length , Maximum length 65535 Bytes 

 

char and varchar Compare two string types

How to choose which one to use ? Please follow the following rules

  • If the data is determined to be the same length , Just use a fixed length (char), such as : Id card , cell-phone number ,md5
  • If the data length changes , Just use lengthening (varchar), such as : name , Address , But you have to make sure that the longest can be saved .
  • Fixed length disk space is a waste , But it's efficient .
  • Longer disk space is more economical , But it's inefficient .
  • The meaning of fixed length is , Directly open up the corresponding space
  • The meaning of lengthening is , Without exceeding the custom scope , How much to use , How much .

Time type

There are three common dates :

  • date : date 'yyyy-mm-dd' , Take up three bytes
  • datetime Time date format 'yyyy-mm-dd HH:ii:ss' Indicates a range from 1000 To 9999 , Take up the octet
  • timestamp : Time stamp , from 1970 Year begins yyyy-mm-dd HH:ii:ss Format and datetime Exactly the same , Occupy four bytes

We can see , The timestamp will automatically generate the current time . Next let's revise t1 Time for .

 enum and set

// Both syntax 
enum(' Options 1',' Options 2',' Options 3',...);
set(' Option value 1',' Option value 2',' Option value 3', ...);

 

原网站

版权声明
本文为[Longbow dog learning C]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221438095068.html