数据库入门3

时间:2021-09-05
本文章向大家介绍数据库入门3,主要包括数据库入门3使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

存储引擎

概念

MySQL内部针对数据的存储有很多不同的方式,我们可以简单地把这些存储方式理解为存储引擎。

查看方法

输入:show engines;

主要的存储引擎

InnoDB

MySQL5.5之后的版本默认的存储引擎。

支持事务、行锁、外键,这样会让数据更安全。

事务:保证多个数据的操作要么全部完成要么全部失败

行锁:多一行行数据加锁 同一时间只能有一个人操作

外键:建立表关系(后面详细的讲)

MyISAM

MySQL5.5之前的版本默认的存储引擎

由于不支持事务、外键等功能,所以安全性和InnoDB相比较低,但是存取数据的速度比InnoDB更快。

memory

数据直接存在内存,速度快但是断电立刻丢失。

blackhole

写入其中的数据都会丢失。

存储引擎表文件

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有两个文件

.frm      表结构

.ibd      表数据、索引(相当于书的目录)

MyISAM有三个文件

.frm     表结构

.MYD  表数据

.MYI    表索引

memory有一个文件

.frm    表结构

blackhole有一个文件

.frm    表结构

存储数据特性

insert into t1 values(1);
insert into t2 values(1);
insert into t3 values(1);
insert into t4 values(1);

创建表的完整语法

create table 表名(
	字段名 字段类型(宽度) 约束条件,
	字段名 字段类型(宽度) 约束条件,
	字段名 字段类型(宽度) 约束条件
); 

注意:

1.字段名和字段类型是必须的,宽度和约束条件是可选的。

2.约束条件可选,并且一个字段可支持多个约束条件。

3.最后结尾的字段语句不能有逗号。

字段类型

整型

整型的分类有:tinyint,smallint,int,bigint,不同的整型类型能存储的数字范围是不同的。

默认的整型都是带正负号的。

消除符号的方法--unsigned

create table t7(id tinyint unsigned);  # unsigned约束条件 无符号
insert into t7 values(-129),(256);  # 0 255

浮点型

浮点型的分类有:float,double,decimal,不同的浮点型存储小数的范围和精度不一样。

float(255,30)   # 总共255位小数位占30位
double(255,30)  # 总共255位小数位占30位
decimal(65,30)  # 总共65位小数位占30位


create table t8(id float(255,30));
create table t9(id double(255,30));
create table t10(id decimal(65,30));
insert into t8 values(1.111111111111111111111111111111);
insert into t9 values(1.111111111111111111111111111111);
insert into t10 values(1.111111111111111111111111111111);
# 精确度:float < double < decimal

精确度问题在很多场景下都会发生 有时候我们会采取不同的类型来存储,比如手机号全部是数字存储的时候应该使用整型 但是有时候我们使用字符类型。

字符类型

字符类型分为:char和varchar,这两个数据类型在存储数据上有本质的区别

char(4)

最多可以存储4个字符,超过了报错,没有超过也按照四个字符存储,默认空格填充。

varchar(4)

最多可以存储4个字符,超过了报错,没有超过则按照实际有几个字符存几个。

create table t11(id int,name char(4)); 
create table t12(id int,name varchar(4)); 
insert into t11 values(1,'jason');
insert into t12 values(1,'jason');

5.6版本超出了范围没有报错是因为我们没有开启严格模式(5.7及之后版本默认都有)

char_length() 统计数据长度

默认情况下MySQL会自动填充存储并在查询的时候自动去除填充的空格

set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";

严格模式

查看严格模式

show variables like '%mode%';

修改严格模式

set global sql_mode = 'strict_trans_tables';

最后需要退出客户端重新进入

时间类型

date		年月日
time		时分秒
Datetime	年月日时分秒
Year		年

create table student(
	  id int,
      name char(16),
      born_year year,
      birth date,
      study_time time,
      reg_time datetime
);

时间数据很多时候都是自动获取,这里仅仅是演示一下

insert into student values(1,'jason','2022','2022-05-09','11:11:00','2022-11-11 11:11:11');

枚举与集合类型

enum:多选一

set:多选多,包含多选一

枚举类型

create table t13(
    id int,
    name varchar(16),
    gender enum('male','female','others')
);
'''插入数据的时候只能够插入提前规定好的'''
insert into t13 values(1,'jason','男');  # 报错
insert into t13 values(2,'tony','male');

集合类型

create table t14(
	id int,
    name char(16),
    hobby set('basketball','football','doublecolorball')
);
'''插入数据的时候只能够插入提前规定好的'''
insert into t14 values(1,'jason','read');  # 报错
insert into t14 values(2,'kevin','basketball');  # 可选一
insert into t14 values(3,'tony','basketball,football');  # 可选多

宽度说明

针对数字类型,宽度并不是用来限制存储长度而是用来表示展示长度。

但是对数字无效,所以在定义数字的时候不需要手动添加宽度。

约束条件

插入数据的方式

方式1:insert into t1 values()

这个方式是按照字段顺序依次传入的,一个都不能少。

方式2:insert into t1(id,name) values()

这个方式是按照指定的字段传入,可以少。

unsigned  # 无符号
zerofill  # 0填充




# unique  唯一
'''单列唯一'''
create table t20(
	id int,
    name varchar(16) unique
);
'''多列唯一'''
create table t21(
	id int,
    host int,
    port int,
    unique(host,port)
);

not null 不能为空 使用频率很高

create table t18(

  id int,

  name varchar(16) not null

);

default 默认值

create table t19(   

  id int, 

  name varchar(16) default '匿名用户'

);

unique 唯一

单列唯一

create table t20(

  id int,

  name varchar(16) unique

);

多列唯一

create table t21(

  id int,

  host int,

  port int,

  unique(host,port)

);

原文地址:https://www.cnblogs.com/wddwyw-jyb/p/15223543.html