启动 : net start mysql

查看有没有进程

1
tasklist| findstr "mysql"  

杀死进程

1
taskkill/f /t /im mysqld.exe  

登录mysql:

语法:

1
2
3
4
5
mysql -u用户名 -p密码 [-h数据库服务器IP地址 -P端口号]

// 一般直接
mysql -uroot -p
后输入密码登录

卸载:


SQL

一门操作关系型数据库的编程语言

  • 分类
    • DDL:数据定义语言,定义数据库对象
    • DML:数据操作语言,对数据库数据进行修改
    • DQL:数据查询语言,查询
    • DCL:数据控制语言,控制,用来控制数据库访问权限

DDL - 数据库

  • 操作语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 查询所有数据库
show databases;

-- 查询当前数据库
select database();

-- 使用 / 切换数据库名字
use 数据库名字

-- 创建数据库(如果不存在),数据库不能重复
create database [if not exist] 数据名字 [default charset utf8mb4]
-- utf8mb4可以使用很多表情等,默认是这个

drop database [if exists] 数据库名字

上述database也可以替换成schema

  • 表结构
1
2
3
4
5
create table tablename(
字段一 字段类型 [约束] [comment 字段一注释],
....
字段二 字段类型 [约束] [comment 字段二]
)[comment 表注释]
1
2
3
4
5
6
7
8
-- 表操作  
create table user(
id int comment 'ID, 唯一标识',
username varchar(50) comment '用户名',
name varchar(10) comment '姓名',
age int comment '年龄',
gender char(1) comment '性别'
) comment '用户消息表';

约束:作用在表中的字段上的规则,用来限制储存在表中的数据

关键字 约束
not null 非空约束,限制字段值不能是null
unique 保证字段所有数据唯一不重复
primary key 主键是一行数据的唯一标识,要求是非空且唯一
default 保存数据时,如果没有指定字段值,采用默认值
foreign key 外键约束,让两张表建立连接保证数据一致性和完整性。
auto_increment 主键按递增顺序增加
1
2
3
4
5
6
7
8
-- 表操作  
create table user2(
id int primary key auto_increment comment 'ID, 唯一标识', -- 主键约束
username varchar(50) not null unique comment '用户名', -- 非空且唯一
name varchar(10) not null comment '姓名', -- 非空约束
age int comment '年龄',
gender char(1) default '男' comment '性别' -- 默认约束
) comment '用户消息表';
  • 数据类型

在满足业务需求的情况下尽可能选择占用磁盘空间较小的。

char和varchar的优劣:char性能较高但是浪费磁盘空间,节省磁盘空间但是性能略低

  • 表结构操作
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    show tables; -- 查询数据库所有表
    desc 表; -- 查看表结构
    show create table 表; -- 查询建表语句

    -- 添加字段
    alter table 表名 add ?? 数据类型 [约束] comment;

    -- 修改字段类型
    alter table 表名 modify ?? 新数据类型 [] comment;

    -- 修改字段名字
    alter table 表名 change 原来的名字 新的名字 和原来一样

    alter table 表名 drop column 字段名 -- 删除字段

    alter table 表名 rename to 新名字 -- 修改表名

    drop table [if exists] 表名; -- 删除表

DML

对于数据表中数据增删改

添加

1
2
3
4
5
6
7
8
-- 指定字段添加数据
insert into 表名(字段1, 字段2) values (值一,值二);

-- 为表中所有字段插入值(直接把字段名字不写就默认是添加所有)
insert into 表名 values(...);

-- 批量操作
insert into 表名(字段1, 字段2) values (值一,值二), (1, 2), ...;

更新

1
2
3
4
5
6
-- 修改数据,注意要写where,不屑where就是对整个表操作
update 表名 set 字段名1 = name, 字段名2 = ??, ...[ where 条件 ];

update user set username = 'zhangsan' where id = 19;


删除

1
2
3
-- 删除数据
delete from 表名 [where 条件]

DQL

  • 关键字: SELECT

完整的DQL语句语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
select
字段列表
from
表名列表
where
条件列表
group by
分组字段列表
having
分组后条件列表
order by
排序字段列表
limit
分页查询
  • 基本查询

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    -- 查询多个字段
    select 字段1,2,3 from 表名

    -- 查询所有字段(通配符)
    select * from 表名

    -- 为查询字段设置别名
    select 字段1 [as 别名] from 表名

    -- 去除重复记录
    select distinct 字段列表 from 表名
  • 条件查询

    1
    2
    -- 条件查询
    select 字段列表 from 表名 where 条件列表
比较运算符 功能
>, <, >= , <=, =
<> 或 != 不等于
between … and … 在某个范围内
in(…) 在in之后的列表中的值,多选一
like 占位符 模糊匹配(_匹配单个字符, \%匹配多个字符)
is null 是null
逻辑运算符 功能
and, &&
or, \ \
not, !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
select * from user where name = '彩解';

select * from user where age <= 50 && name = 1;

select * from user where name is null;

select * from user where job in (2, 3, 4);

-- 查询名字是两个字的
select * from user where name like '__';

-- 查询姓名包含某个字的
select * from user where name like '%二%';
  • 分组查询

聚合函数

函数 功能
count 统计数量
max 最大值
min 最小值
avg 平均
sum
  • 注意,聚合函数不参与null统计
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    -- count(字段) // 注意,聚合函数不参与null统计  
    select count(id) from user;

    -- count(*) 优先
    select count(*) from user;

    -- count(常量) 和 * 差不多
    select count(1) from user; -- 底层实一行一行

    -- avg, min, max, sum
    select avg(age) from user;

语法

1
2
-- 分组查询
select 字段列表 from 表名 [where 条件列表] group by 分组字段名 [having 分组后过滤的条件];

  • where 和 having 的区别
    1. 执行时间不同: where是分组之前过滤,不满足where条件,不参与分组,而having是分组之后对结果进行过滤。
    2. 判断条件不同: where不能对聚合函数判断,而having可以。
  • 注意: 分组之后字段不可以随便写 * ,否则会造成歧义

    1
    2
    3
    4
    5
    6
    7
    -- 错误
    select * from user group by gender
    -- 正确
    select gender / age from user group by gender;

    -- 查询入职时间在2025一起,并对结果分类,获取员工大于等于2的职位
    select job, count(*) from user where entrydata <= 2025 group by job having count(*) >= 2;
  • 排序查询

    1
    2
    -- 排序查询
    select 字段列表 from 表名 [where 条件列表] [group by 分组名字 having 分组后过滤] order by 排序字段 排序方式

排序方式: 升序(asc), 降序(desc), 默认是升序

注意: 如果是多字段排序,当地一个字段值相同时,才会根据第二个字段排序

1
2
3
4
5
6
select * from user order by age;  

select * from user order by age desc;

-- 多字段排序
select * from user order by age, id desc;
  • 分页查询
    1
    2
    -- 排序查询
    select 字段 from 表名 [where 条件] [group by 分组字段 having 过滤条件] [order by ] limit 起始索引, 查询记录数

说明:

  1. 起始索引从0开始
  2. 分页查询不同数据库实现不同,mysql是limit
  3. 如果从0开始可以省略
    1
    2
    3
    4
    5
    6
    -- 从0开始,每页显示5条  
    select * from user limit 0, 5;
    -- 第二页
    select * from user limit 5, 5;
    -- 第三页
    select * from user limit 10, 5;

页码和索引的转换

索引 = (页码 - 1) * 每一页数量