MySQl的基础

yuzai-xin / 2023-07-31 / 原文

1.MySQl 简介


  • MySQl 是一个关系型数据库
    • 关系型数据库是以表的形式存储和管理数据的
    • 关系型数据库的优点,关系明确、支持事务处理和数据的完整性

2.MySQl 操作


  • MySQl 的库可以看作是一个文件夹
  • MySQl 的表可以看作是一个文件
  • MySQl 的数据可以看作是文件内容

2.1库操作


  • 显示所有的库:show databases;
  • 显示指定创建的库:show create databse '库名';
  • 显示当前所在的库:select database();
  • 创建库:create database 库名 charset utf8;
    • 数据库的命名规则:
      • 可以是字母、数字、下划线、@、#、$
      • 区分大小写
      • 唯一名字
      • 不能使用关键字如:create select
      • 不能单独使用数字
      • 最长 128 位
  • 删除库:drop 库名;
  • 切换库:use 库名;

2.2表操作


  • 显示所有的表:show tables;
  • 创建表:
create table test(
'字段名1 类型1'/约束,
'字段名2 类型2'
);
  • 删除表:drop table ‘表名’;
  • 修改表
    • 添加列:alter table '表名' add '字段 类型 约束';
    • 删除列:alter table '表名' drop '字段 类型 约束';
    • 修改列:alter table '表名' change '旧的字段' '新的字段' 类型 约束;
    • 修改列的数据类型:alter table '表名' modify '字段' 'new类型';
    • 添加主键约束:alter table '表名' add primary key '字段';
    • 添加外键约束:alter table '表名' add foreign key '字段' reference '关联的表名(字段名)';

3.MySQl 的存储引擎


什么是存储引擎

  • 存储引擎简单来说就是表的类型
  • 不同的存储类型有不同的特性和适用场景
  • 它决定了数据的存储方式、访问方式、支持的特性等

查看 MySQl 引擎命令: show engines;
image.png

4.MySQl 的数据类型


  1. 数值类型 (Numeric Types):
    • tinyint: 1 字节整数,范围为 -128 到 127 (有符号) 或 0 到 255 (无符号)。
    • smallint: 2 字节整数,范围为 -32,768 到 32,767 (有符号) 或 0 到 65,535 (无符号)。
    • mediumint: 3 字节整数,范围为 -8,388,608 到 8,388,607 (有符号) 或 0 到 16,777,215 (无符号)。
    • int (或 integer): 4 字节整数,范围为 -2,147,483,648 到 2,147,483,647 (有符号) 或 0 到 4,294,967,295 (无符号)。
    • bigint: 8 字节整数,范围为 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 (有符号) 或 0 到 18,446,744,073,709,551,615 (无符号)。
    • decimal (或 numeric): 定点数,用于存储精确的小数,需要指定精度和标度。
    • float:单精度浮数
    • double:双精度浮数
  2. 字符串类型 (String Types):
    • char: 固定长度字符串,最多 255 个字符。
    • varchar: 变长字符串,最多 65535 个字符。
    • text: 长文本数据,最多存储 65535 个字符。
    • tinytext: 短文本数据,最多存储 255 个字符。
    • mediumtext: 中等长度文本数据,最多存储 16777215 个字符。
    • longtext: 长文本数据,最多存储 4294967295 个字符。
  3. 日期与时间类型 (Date and Time Types):
    • date: 日期,格式为 'YYYY-MM-DD'。
    • time: 时间,格式为 'HH:MM:SS'。
    • datetime: 日期与时间组合,格式为 'YYYY-MM-DD HH:MM:SS'。
    • timestamp: 日期与时间组合,存储从 '1970-01-01 00:00:01' 开始的秒数。
    • year: 年份,格式为 'YYYY'。
  4. 布尔类型 (Boolean Type):
    • boolean (或 bool): 存储真值 (true/false),通常使用 TINYINT(1) 来表示。
  5. 二进制类型 (Binary Types):
    • binary: 固定长度二进制字符串,最多 255 个字节。
    • varbinary: 变长二进制字符串,最多 65535 个字节。
    • blob: 二进制大对象,最多存储 65535 个字节。
    • tinyblob: 短二进制大对象,最多存储 255 个字节。
    • mediumblob: 中等长度二进制大对象,最多存储 16777215 个字节。
    • longblob: 长二进制大对象,最多存储 4294967295 个字节。
  6. 枚举类型 (Enum Types):
    • enum: 一个字符串对象,从一个预定义的可能值列表中进行选择。
  7. 集合类型 (Set Types)set: 一个字符串对象,可以从一个预定义的可能值列表中选择多个值。

5.约束条件


  • not null:非空约束,确保值不为空
  • default:默认值
  • unique:唯一值约束,确保一个列或多个列的值唯一,一个表可以有多个唯一值约束
  • primary key:主键约束,唯一标识符,不能为空,不能重复
  • auto_increment:自增,与主键 primary key 相配合
  • foreign key:外键关联,和其他表建立关联关系

6.foreign key 表的关联关系


6.1多对一

一个表中的一条对数据对应另一个表中多条数据

多对一的关联关系,foreign key 在多的那张表中

案例:书籍的出版社和书相对应

# 创建出版社表
create table public(
id int primary key auto_increment,
name varchar(20)
);

# 创建书籍表
create table book(
id int primary key auto_increment,
name varchar(20),
public_id int,
foreign key(public_id) references public(id) on delete cascade on update cascade
);

# 插入数据
insert into public(name) values
('北京大学出版社'),
('清华大学出版社'),
('山东大学出版社');

insert into book(name,public_id) values
('python', 1),
('java',2),
('操作系统',3),
('数据结构',1),
('算法',2),
('c语言',3),
('php语言',2);

6.2多对多

多对多实际上就是两个多对一的表相互关联起来,但是由于无法相互设置 foreign key,所以需要第三张表来分别关联两张表,两张表和第三张表的关系都是多对一

案例:老师与课程,一门课可以有多个老师教,一个老师也可以教多门课程

# 创建老师表
create table teacher(
id int primary key auto_increment,
name varchar(20)
);

# 创建课程的表
create table class(
id int primary key auto_increment,
name varchar(20)
);

# 第三张表
create table relate(
id int primary key auto_increment,
teacher_id int,
class_id int,
foreign key(teacher_id) references tercher(id) on delete cascade on update cascade,
foreign key(class_id) references class(id) on delete cascade on update cascade
);

# 插入数据
insert into teacher(name) values
('张三'),
('李四'),
('王五'),
('小明');

insert into class(name) values
('python'),
('java'),
('javascript'),
('操作系统'),
('算法'),
('数据结构'),
('计算机网络'),
('网络安全');

insert into relate(teacher_id, class_id) values
(1,2),
(1,3),
(1,5),
(1,8),
(2,2),
(2,3),
(2,8),
(3,2),
(3,5),
(3,6)
(4,2),
(4,1),
(4,4),
(4,7);

6.3一对一

一对一:是两个关系是绝对绑定的,一个萝卜一个坑

案例:一个班级一个班长

# 创建班级表
create table class(
id int primary key auto_increment,
name varchar(20)
);

# 创建班长表
create table presider(
id int primary key auto_increment,
class_id int unique,
foreign key(class_id) references class(id) on delete cascade on update cascade
);

7.表的增删改查的 SQL 语句


  • 创建表create table '表名'('字段 类型 约束');
  • 删除表drop table '表名';
  • 增加表的字段alert table '表名' add '字段 类型 约束';
  • 添加数据insert into '表名'('字段') values('值');
  • 修改数据update '表名' set '要修改的字段的值' where '条件';
  • 查询数据: select '字段1,字段2.../使用*查所有的值' from '表名' where '条件';
  • 删除数据delete from '表名' where '条件';

8.where 约束


where 相当于 if 条件判断

select * from '表名' where '条件'

9. group by 分组


group by 分组就是将指定字段中相同的值进行分组

group_concat ('字段') 是将分组的值按字段用逗号分隔

案例

# 使用分组group by
select * from employee group by(sex);
select * from employee group by(name); # 得到的值是表中相同字段值的第一个数据

group by 一般与聚合函数一起使用

  • 聚合函数
    • avg:平均数
    • sum:求和
    • max:最大值
    • min:最小值
    • cout:计算数量

案例

# 按照部门进行分组,得到的结果是:post部门name,employees员工名
select post,group_concat(name) as employees from employee where group by (post)

10.having 过滤


having

# 查找员工id > 2 所在部门的所有员工
select post,group_concat(name) from employee group by (post) having (id>2)

10.1 where 与 having 的区别

  • 优先级 where > group by > having
    • where 在 group by 之前
    • having 在 group by 之后
  • 聚合函数的使用
    • where 不能使用聚合函数
    • having 可以使用聚合函数
  • 性能
    • where 先筛选条件然后再和另一张表建立链接性能较快
    • having 直接每个先建立连接,然后在进行筛选,性能相对较差

11.order by 排序

select * from employee order by (salary) asc # 升序
select * from employee order by (salary) desc # 降序
select * from employee order by (salary) asc, id desc # 先按照salary升序排,salary相等按照id降序排

12.limit 限制查询的条数


13. 连表操作


连表查询:

select * from employee where on department_id = department.id;

四种连接方式

  • 内连接:两张表同时都有的部分
select * from employee inner join on employee.dep_id = department.id;
  • 左连接:在内连接的基础上只取左边表的部分
select * from employee left join on employee.dep_id = department.id;
  • 右连接:也是在内连接的基础上只取右边表的部分
select * from employee right join on employee.dep_id = department.id;
  • 全外连接:在内连接的基础上取两张表没有关系的部分
select * from employee full join on employee.dep_id = department.id;

总结


语法顺序

  • select (字段)from 库. 表
    • where 条件
      • group by 分组
        • having 过滤
          • order by 排序
            • limit 过滤