MySQL - Data Types

content

Data Type Classification

bit type

Decimal type

String type

time type

 enum and set


Data Type Classification

Divided into four types

  • Numeric type
  • text binary type
  • time and date
  • string type

 We need to know what the maximum and minimum allowable values ​​are for different numeric types

 Let's run a few sets of out-of-bounds tests:

 We can find that integers are signed by default. So how do you create an unsigned one?

bit type

//bit基本语法
bit[(M)]: 位字段类型。M表示每个值的位数,范围从1到64。如果M被忽略,默认为1。

Q: Why is the value corresponding to a not displayed?

Because the bit field is displayed according to the value corresponding to the ASCII code.

Decimal type

Among the numeric types, the float type and the decimal type belong to the decimal type.

//float语法
float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节

For example, the range represented by float(4,2) is -99.9~99.99.

We can see from the interpolated value that the float is rounded when saved.

When float is unsigned, float(4, 2) unsigned range is 0~99.99

 

//decimal语法
decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数

For example, the range represented by decimal(5, 2) is -999.99~999.99

As above, decimal(5,2) unsigned represents the range 0 ~ 999.99

It seems that decimal and float are very similar, let's take a look at the difference between the two

String type

String types are char and varchar

//char语法
char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255

//varchar语法
varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节

 

Comparison of two string types, char and varchar

So how to choose which one to use? Please follow the rules below

  • If the length of the data is determined to be the same, use a fixed length (char), such as: ID card, mobile phone number, md5
  • If the data length changes, use variable length (varchar), such as: name, address, but you must ensure that the longest can be stored in it.
  • Fixed-length disk space is wasteful, but efficient.
  • The variable length disk space is more economical, but the efficiency is low.
  • The meaning of fixed length is to directly open up the corresponding space
  • The meaning of variable length is how much to use and how much to open without exceeding the custom range.

time type

There are three commonly used dates:

  • date : date 'yyyy-mm-dd', occupying three bytes
  • datetime time date format 'yyyy-mm-dd HH:ii:ss' means the range is from 1000 to 9999, occupying eight bytes
  • timestamp : timestamp, yyyy-mm-dd HH:ii:ss format from 1970 is exactly the same as datetime, occupying four bytes

As we can see, the timestamp automatically generates the current time. Next we modify the time of t1.

 enum and set

//两者语法
enum('选项1','选项2','选项3',...);
set('选项值1','选项值2','选项值3', ...);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324304186&siteId=291194637