129 MySQL data types (important)

First, the data type (important)

mysql database supports the following data types:

  • Int | float | Character | Time Type | enumeration type | type of collection

Integer

Key: tinyint, int, constraint (unsigned, zerofill)

Types of

tinyint: 1 byte -128 to 127 (for determination logic) Key
smallint: 2
bytes -32768 to 32767
mediumint: 3
bytes
int: 4
bytes -2147483648 2147483647 ~ (int length 11, if it exceeds the length of the string)
Key

bigint : 8 bytes

Constraints (master key *)

unsigned: Unsigned
zerofill : 0 filled

# 建表
mysql>: create table tb1(x tinyint, y smallint, z int(6));

# 插入数据(在安全模式下直接报错)
mysql>: insert into tb1 values(128, 32768, 32768);  # 结果:127,32767,32768


# 结论:整型的长度由所占字节(取值范围)决定,可以自定义长度,但是不影响所占字节(取值范围)
#    所有整型变量的长度一般都省略不写

'''宽度
1.不能决定整型存放数据的宽度, 超过宽度可以存放, 最终由数据类型所占字节决定
2.如果没有超过宽度,且有zerofill限制, 会用0填充前置位的不足位
3.没有必要规定整型的宽度, 默认设置的宽度就为该整型能存放数据的最大宽度 *
'''

# 整型约束(在安全模式下直接报错)
mysql>: create table tb2(x tinyint unsigned);  # 0~255
mysql>: insert into tb2 values(256), (-1);    # 255, 0
    
# 0填充约束
mysql>: create table tb3(x tinyint unsigned zerofill);
mysql>: insert into tb3 values(10);  # 010

Float

Key: float (255, 30), decimal (65, 30)

Types of

float(M, D): 4 bytes , 3.4E-38 is ~ 3.4E + 38 is
double(M, D): . 8 bytes , 308 ~ 1.7E + 1.7E-308
decimal(M, D): where byte M, D + 2 on the basis of a large value, in fact, is the decimal value of M + 2 number of bytes field

width

Memory width limit
(M, D) => M is the total number of bits, D is a decimal, M greater than or equal to D

float(255, 30): Lowest accuracy, the most common
double(255, 30) : high precision, multi placeholder
decimal(65, 30) : String deposit, full precision

# 在安全模式下测试浮点型类型

# 建表:
mysql>: create table tb4 (age float(256, 30)); # Display width out of range for column 'age' (max = 255)
mysql>: create table tb5 (age float(255, 31)); # Too big scale 31 specified for column 'age'. Maximum is 30.
mysql>: create table tb5 (age float(255, 30)); # 在合理取值范围
    
mysql>: create table t12 (x float(255, 30));
mysql>: create table t13 (x double(255, 30));
mysql>: create table t14 (x decimal(65, 30));
mysql>: insert into t12 values(1.11111111111111111119);     # 进度最低,最常用
# 1.111111164093017600000000000000 
mysql>: insert into t13 values(1.11111111111111111119);     # 进度高,占位多
# 1.111111111111111200000000000000  
mysql>: insert into t14 values(1.11111111111111111119);     # 字符串存,全精度
# 1.111111111111111111190000000000

# 重点:长度与小数位分析
# 报错,总长度M必须大于等于小数位D
mysql>: create table t14 (x decimal(2, 3));

# 能存储 -0.999 ~ 0.999,超长度的小数位会才有四舍五入,0.9994可以存,就是0.999,0.9995不可以存
mysql>: create table t14 (x decimal(3, 3));  # 整数位 3 - 3,所以最大为0

# 能存储 -9.999 ~ 9.999,超长度的小数位会才有四舍五入,9.9994可以存,就是9.999,9.9995不可以存
mysql>: create table t14 (x decimal(4, 3));  # 整数位 4 - 3,所以最大为9

# 能存储 -99.999 ~ 99.999,超长度的小数位会才有四舍五入,99.9994可以存,就是99.999,99.9995不可以存
mysql>: create table t14 (x decimal(5, 3));  # 整数位 5 - 3,所以最大为99

Character: Database optimization - char efficiency is higher than varchar

Key : char can be used on a char. Database Optimization

Types of

char: Fixed length of 4 bytes, the data length of the memory always use settings
varchar: variable length, provided within the length of the variable length data storage

width

Action: memory width limitation

char to length to store, if the data length changes, usually more space, but the access to data in a fixed predetermined length, high efficiency

Varchar, storing data, first calculates a length, a dynamic variable-length data stored in the data to be stored, it is generally more space-saving, time-consuming but the calculation is required, the efficiency is low

varchar calculated data length information is needed to open up space to store, in the data header (before the start of data), but also requires an additional consumption of 1 ~ 2 bytes of storage

Therefore, if the data are fixed length, or a small range of fluctuation, char will not be compared more space, and efficiently

# 建表:
mysql>: create table ts1 (s1 char(4), s2 varchar(4));
mysql>: insert into ts1 values('adcde', 'xyzabc');  # 'adcd', 'xyza'

Time Type

Key: datetime (bytes. 8, may be null), timestamp (4 bytes, and has a default value CURRENT_TIMESTAMP)

Types of

year:yyyy(1901/2155)
date:yyyy-MM-dd(1000-01-01/9999-12-31)
time:HH:mm:ss
datetime:yyyy-MM-dd HH:mm:ss(1000-01-01 00:00:00/9999-12-31 23:59:59)
timestamp:yyyy-MM-dd HH:mm:ss(1970-01-01 00:00:00/2038-01-19 ??)

# 建表:
mysql>: create table td1 (my_year year, my_date date, my_time time);
mysql>: insert into td1 values(1666, '8888-8-8', '8:8:8');  # 时间需要在取值访问内

mysql>: create table td2 (my_datetime datetime, my_timestamp timestamp);   
mysql>: insert into td2 values('2040-1-1 1:1:1', '2040-1-1 1:1:1');  # 时间需要在取值访问内   
mysql>: insert into td2(my_datetime) values('2040-1-1 1:1:1');  # timestamp不复制会才有系统当前时间

# datetime:8字节,可以为null
# timestamp:4字节,有默认值CURRENT_TIMESTAMP

Enumerated types and collection types

Types of

enum: Radio
set: multiple choice

# 建表
# enum、set默认值为NULL
mysql>: create table tc1 (name varchar(20), sex enum('男', '女', '哇塞'), hobbies set('男', '女', '哇塞'));
mysql>: insert into tc1 values('ruakei', '哇塞哇塞', '未知');  
    
# enum、set手动设置默认值 '男' 与 '哇塞'
mysql>: create table tc2 (name varchar(20), sex enum('男', '女', '哇塞') default '男', hobbies set('男', '女', '哇塞') default '哇塞');
mysql>: insert into tc2 values('ruakei', '哇塞哇塞', '未知');  
mysql>: insert into tc2(name) values('ruakei');
    
# 对sex、hobbies两个字段赋值错误,系统默认用空字符串填充(非安全模式),安全模式抛异常
# 如果对出sex、hobbies两个字段外的其他字段进行赋值,这两个字段会才有默认值

# 注:对set类型的字段进行赋值,用一个字符串,字符串内部用,将多个选项隔开,且不能添加空格等其他额外字符
mysql>: insert into tc2 values('ruakei_1', '女', '男,女,哇塞');  

Guess you like

Origin www.cnblogs.com/XuChengNotes/p/11588525.html