Day 31 Storage Engine Data Type Integer Strict Mode Floating Point Character Type Date Type Enumeration and Collection Type

Storage engine In
daily life, there are many file formats, and there will be different storage methods and processing mechanisms for different file formats (txt, pdf, word, mp4...)

There should be corresponding different processing mechanisms to store different data

Storage engine is a different processing mechanism

mysql main storage engine

1.innodb
	三大特点:事务,行级锁,外键
    是mysql5.5版本之后默认的存储引擎
    存储数据更加安全
2.myisam
    是mysql5.5版本之前默认的存储引擎
    速度要比innodb更快 但是我们更加注重数据的安全
3.memory
    内存引擎(数据全部存放在内存中)断电数据丢失
4.blackhole
    无论存什么都立刻消失(黑洞)
查看所有的存储引擎:show engines;

4种不同的存储引擎代码验证
不同的存储引擎在存储表的时候 异同点
用4种不同的存储引擎去建4张表
create table t1(id int) engine=innodb;
create table t2(id int) engine=myisam;
create table t3(id int) engine=memory;
create table t4(id int) engine=blackhole;

打开对应的文件目录:
    innodb:t1.frm  (表结构)
            t1.ibd  (表数据)
    myisam:t2.frm   (表结构)
            t2.MYD  (表数据)
            t2.MYI  (索引(index),基于索引查找数据,速度更快)
    menory:t3.frm   (表结构(数据在内存,无需文件存储))
    blackhole:t4.frm   (表结构(黑洞,不需要帮忙存数据)

存数据:
    insert into t1 values(1);
    insert into t2 values(2);
    insert into t3 values(3);
    insert into t4 values(4);

查数据:
    select * from t1;
    select * from t2;
    select * from t3;
    select * from t4;

Create a table full syntax of
grammar

creat table 表名(
    字段名1 类型(宽度)约束条件,
    字段名2 类型(宽度)约束条件,
    字段名3 类型(宽度)约束条件,
)

注意:
1.在同一张表中字段名不能重复
2.宽度和约束条件是可选的(可写可不写),而字段名和字段类型是必须的
    约束条件写的话 也支持写多个 
    字段名1 类型(宽度)约束条件1 约束条件2...
    create table t5(id);  报错
3.最后一行不能有逗号
    create table t6(
        id int,
        name char,
    ); 报错

Width
generally refers to the limitation on stored data

create table t7(name char);  默认宽度是1
insert into t7 values("nana"); 只存成功了一个字符,
insert into t7 values("null"); 关键字null(空)
    针对不同的版本会出现不同的效果
    如果没有开启严格模式,规定只能存一个字符你给了多个字符,那么我会自动帮你截取一个字符存储
    如果开启了严格模式,那么规定只能存几个,就不能超,一旦超出范围就会立刻报错 Data too long for ...
    
严格模式到底要不要开?
mysql5.7之后的版本多是默认开启严格模式的
使用数据库的准则:
    能尽量少让数据库干活就尽量少让数据库干活,不用给数据库增加额外的压力

约束条件null not null 不能插入null
create table t8(id int,name char not null);

宽度和约束条件到底是什么关系??
    宽度是用来限制数据的存储
    约束条件是在宽度的基础之上增加额外的约束

Basic data type


Insert picture description here
The storage width of the integer type int is 4 Bytes, which is 32 bits, which is 2**32

The unsigned maximum value is: 4294967296-1

Signed maximum value: 2147483648-1

Both signed and unsigned maximum numbers require a display width of 10, while for the signed minimum, 11 bits are required to display completely, so the default display width of the int type is 11 is very reasonable

Finally: the plastic type, in fact, there is no need to specify the display width, use the default is ok

Integer
Classification
TINYINT SAMLLINT MEDUIMINT INT BIGINT
Function
Store age, grade, id, number, etc.

   以TINYINT为例:
        是否有符号
            默认情况下是带符号的
        超出会如何
            超出限制只存最大可接受值
            
    命令验证
	create table t9(id tinyint);
	insert into t9 values(-129),(256);
	select * from t9;   (显示结果为(-128127))
	约束条件值unsigned 无符号,
	命令验证
	create table t10(id tinyint unsigned);
	insert into t10 values(-1),(256);
	select * from t10;  (显示结果为(0255)
    验证int类型
    create table t11(id int);  (不写宽度,默认是11)
    insert into t11 values(-1),(256);
    select * from t11; (显示结果为(-1256)
    int默认也是带符号的
整型默认的情况下都是带有符号的

For integers, what is the width in the brackets?

	create table t12 (id int(8));
	insert into t12 values(123456789);
	select * from t12; (显示结果为(123456789)

Special case: only the number in the integer brackets does not indicate the limited number of digits but the display length

id int8);
    如果数字没有超过8位,那么默认用空格填充至8位
    如果数字超出了8位,那么有几位就存几位(但是还是要遵守最大范围)
    命令验证:
create table t13(id int(8) unsigned zerofill);   	无符号,默认用0填充
insert into t13 values(1);  (数字不超过8位,用默认的0填充至8)
insert into t13 values(112233445566778899);
select * from t13;   (显示结果为(00000001)(4294967295)

Summary:
There is no need to specify the width in the brackets for the integer field because its default width is enough to display all the data

Strict mode

    如何查看严格模式命令 show variables like "%mode";
    sql_mode(严格模式)
    
模糊匹配/查询
    关键字 like 
    %:匹配任意多个字符
    _:匹配任意单个字符
    
修改严格模式
    set session 只在当前窗口有效(临时修改)
    set global 全局有效
    修改成严格模式完整命令:
    set global sql_mode="STRICT_TRANS_TABLES";
    修改完之后 重新进入服务端即可
修改完严格模式,如果是字符或者整型超出范围会直接报错,而不会让数据库去自动截取数据。

Floating point
classification
float, double, decimal
function
height, weight, salary

存储限制
    float25530)   总共255位 小数部分占30double255,30)  总共255位 小数部分占30位
    decimal(65,30)  总共65位 小数部分占30位
精确度验证
    create table t15(id float(255,30));
    create table t16(id double(255,30));
    create table t17(id decimal(65,30));
    
    insert into t15 values(1.111111111111111111111111111111);
    insert into t16 values(1.111111111111111111111111111111);
    insert into t17 values(1.111111111111111111111111111111);
    
    select * from t15;  显示结果( 1.111111164093017600000000000000)
    select * from t16;  显示结果( 1.111111164093017600000000000000)
    select * from t17;  显示结果( 1.111111111111111111111111111111)
    
精确度 :float<double<decimal

Character type
char
fixed-length
char(4) If the data exceeds four characters, directly report an error and not enough. Four characters are filled with blank space.
Varchar
variable length
varchar(4) If the data exceeds four characters, directly report an error.

Differential code verification between fixed length and variable length

create table t18(name char(4));
create table t19(name varchar(4));

insert into t18 values("a");
insert into t19 values("a");

select * from t18;
select * from t19;


介绍一个小方法 char_length统计字段长度
select char_length(name) from t18;     (长度显示为1)
select char_length(name) from t19;     (长度显示为1)

首先可以肯定的是char硬盘上存的绝对真正的数据,带有空格的
但是在显示的时候mysql会自动将多余的空格操作剔除

修改sql_mode 让mysql不要自动剔除操作
set global sql_mode=
"STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH";  (全局添加替换操作,不是添加PAD_CHAR_TO_FULL_LENGTH)

重新统计字段长度
select char_length(name) from t18;      (长度显示4)
select char_length(name) from t19;      (长度显示1

Char and varchar comparison

char
    缺点:浪费空间
    优点:存取都很简单
        直接按照固定的字符存取数据即可
        jason nana marker
        存按照五个字符存 取也按照五个字符取
varchar
    优点:节省空间
    缺点:存取较为麻烦
        1bytes+jason 1bytes+nana 1bytes+marker
        存的时候需要制作报头
        取的时候也需要先读取报头 之后才能读取真实数据

In the past, basic numbers were all using char. Now many varchar are used (char or varchar can be used for access).
Supplement: After
joining the company, you don’t need to consider the field type and field name
because the product manager will send it to you. All have been specified on the mail

Time Type
Classification

date: Date 2021-2-7

datetime: year, month, day, hour, minute and second, 2021-2-7 11:11:11

time: hours, minutes and seconds 11:11:11

year:2021

Time type code demo

create table student(
    id int,
    name varchar(16),
    born_year year,
    birth date,
    study_time time,
    reg_time datetime      # 注意最后一个数据结束不需要加逗号
);   

insert into student values(1,"nana","2000","2000-11-11","11:11:11","2021-11-11 11:11:11");

desc student
select * from student

Enumeration and collection types
Classification
Enumeration (enum) select one more enumerate
set (set) multiple selection

Demonstration of specific usage code of enumeration

 create table user(
    id int,
    name char(16),
    gender enum("male","female","other")
    );
    
insert into user values(1,"nana","male");   正常
insert into user values(2,"dudu","xxxooo");   报错

desc user
select * from user 

Only one storage can be selected from the enumeration when the data is stored in the enumeration field later.

Demonstration of the specific usage code of the collection

    create table teacher(
    id int,
    name char(16),
    gender enum("male","female","other"),
    hobby set("read","run","basketball")
    );

insert into teacher values(1,"nana","female","read");   正常
insert into teacher values(2,"dudu","male","basketball,run");  正常
insert into teacher values(3,"dada","other","干饭");  报错

You can write only one set, but you can't write the ones that are not listed.

Guess you like

Origin blog.csdn.net/Yosigo_/article/details/113747914