mysql学习笔记
安装
数据库常见概念
DB
database 数据库
DBMS
数据库管理系统:mysql、sqlserver...
SQL
用于操作数据库的简单语言
基本使用
登录mysql
mysql [-h localhost -p 3306] -u root -p
命令分类
1. DQL
查询语句
1.1 基础查询
/* *
语法:
select 查询列表 from 表名;
*/
# 查询列表可以是 (字段,常量,表达式,函数)
select first_name from xxx;
select 100;
select 100+1;
select rand();
# 为查询到字段起别名
select 100 as xxx; --100的字段名变成xxx
select 100 xxx; --一样
# 去重
select distinct xx from xxx;
1.2 条件查询
/* *
语法:
select
查询列表
from
表名
where
筛选条件;
*/
# *条件表达式:
# * 条件运算符:
# ? > < = != >= <=
# * 逻辑运算符:
# todo and or not
# todo && || !
# * 模糊查询:
# todo like
# ? |-- like '%a%' --> %通配符 匹配任意多个字符
# ? |-- like '_' --> _通配符 匹配一个字符
# todo between and
# ? |-- xx between 0 and 100 == xx>=0 and xx<=100
# todo in
# ? |-- xx = 'xx' or 'xxx' or 'xxx'
# ? |-- xx in ('xx','xxx','xxxx')
# todo is null / is not null
# todo <=>
# ? |-- 安全等于 可以判断null和普通值
1.3 排序查询
/* *
语法:
select
*
from
xxx
order by
xx [desc|asc] ...;
*/
# ? desc:降序 asc:升序 默认升序
# ! order by 一般放在最后
1.4 分组查询
/* *
select
[分组函数],分组字段
from
表
group by
分组字段;
*/
# todo 分组后筛选 group by ... having 筛选条件 (where 是分组前筛选)
1.5 连接查询(多表查询)
-
1.5.1 sql92标准
- 1.5.1.1 等值连接
select * from 表1,表2 where 表1.字段 = 表2.字段; # todo 表1的某个字段等于表2的某个字段 - 1.5.1.2 非等值连接
# todo 表1的字段[>、< ]表2的某个字段 - 1.5.1.3 自连接
自己的一个字段和另一个字匹配
- 1.5.1.1 等值连接
-
1.5.2 sql99标准
不在是 from 表1,表2 where [表1.xxx = 表2.xxx ...]
而是 from 表1 [inner、left outer...] join 表2 on [表1.xxx = 表2.xxx ...]-
1.5.2.1 内连接(inner)
查询表与表之间的交集部分
inner可以省略
- 等值连接
# ? 进行多表连接 SELECT concat(e.last_name,' ',e.first_name) , d.department_name , j.job_title FROM employees e INNER JOIN -- 内连接语句 departments d ON -- 连接条件 e.department_id = d.department_id INNER JOIN -- 多个表连接 jobs j ON e.job_id = j.job_id ORDER BY d.department_name DESC ; - 非等值连接
- 自连接
- 等值连接
-
1.5.2.2 外连接
1.查询一个表中有另外一个表没有的字段
2.分主表和从表-
左外(left outer)
left outer 左边是主表 左表全集
-
右外(right outer)
right outer 右边是主表 右表全集
-
全外(full outer)
并集
mysql 不支持该语法
-
-
1.5.2.3 交叉连接(cross)
笛卡尔乘积
-
1.6 联合查询
将多个查询条件的结果合并为一条结果
/* *
注意:
1.连接查询的列个数必须一致
2.默认去重,在union all 不去重
*/
SELECT
*
FROM
employees e
WHERE
e.email LIKE '%a%'
OR
e.department_id > 90
;
# todo 这两个结果一样
SELECT * FROM employees e WHERE e.email LIKE '%a%'
UNION
SELECT * FROM employees e2 WHERE e2.department_id > 90;
1.7 子查询
-
1.7.1 where和having后
- 标量子查询(单行子查询 *)
子查询结果只有一行一列
/* * 配合 > < = != >= <= 使用 */ # ? 查询工资高于Abel的员工 SELECT salary,last_name FROM employees e WHERE salary > ( # todo 结果只有一行的子查询 SELECT salary FROM employees e WHERE last_name = 'Abel' ); - 列子查询(多行子查询 *)
多行一列
/* * 配合 in not in any/some all 使用 any/some xx > [x,xx,xx...]中的任意一个 all xx > [x,xx,xx...]中的所有 */ # ? 返回location_id是1400或1700的部门中的所有就员工姓名 SELECT last_name FROM employees e WHERE department_id IN ( SELECT department_id FROM departments d WHERE location_id IN (1400,1700) ); - 行子查询(多列多行)
子查询一行多列或多行多列
- 标量子查询(单行子查询 *)
-
1.7.2 放在select后面
-
1.7.3 放在from后面
-
1.7.4 放在exists后面
返回bool值
1.8 分页查询
/**
语法:
select
*
from
表
limit offset,size
;
offset: 索引从0开始
size: 查询条目
*/
# ! limit 语句放在最后
2. DML
数据操作语言
- 插入(insert)
/* * 语法: 1. insert into 表名([字段...]) values (值...),(...)... ; 2. insert into 表名 set 字段=值,... ; */ # ? 方式一 # 1. 支持多行插入 # 2. 支持子查询 INSERT INTO XX(xx,xxx,xxxx) VALUES (xx,xxx,xxxx),(...) ; # ? 方式二 INSERT INTO XX SET xx=xx,xxx=xxx... ; - 修改(update)
/* * 语法: update 表名 set 字段=新值,... where 筛选条件 ; */ # ! 一般修改都会加上筛选条件,不然全改了 - 删除(delete)
语法: 1. delete from 表名 [where 条件]; 2. truncate table 表名;
3. DDL
数据定义语言,库和表的操作
1 创建 create
2 修改 alter
3 删除 drop
数据库
- 创建数据库
create database 库名; # todo 判断是否存在该数据库,存在不创建 create database if not exists XX; - 修改数据库
# todo 修改字符集 alter database xx character set 字符集; # ! 一般不修改数据库,这是个非常危险的操作 - 删除数据库
drop database [if exists] xxx;
数据表
- 创建表
create table 表名( 字段名 类型 [约束], ... ); - 修改表
# ? 1. 修改字段名 alter table 表名 change column oldName newName 类型; # ? 2. 修改类型 alter table 表名 modify column 字段名 新类型; # ? 3. 添加字段 alter table 表名 add column 字段名 类型; # ? 4. 删除字段 alter table 表名 drop column 字段名; # ? 5. 修改表名 alter table 表名 rename to 新表名; - 删除表
drop table 表名; - 复制表
# ? 复制表结构 create table copy_xx like xx; # ? 复制表结构+数据(可以只复制部分字段) create table copy_xx select * from xx;
约束
限制表中的数据,保证表中数据的准确性和可靠性
| 约束 | 作用 |
|---|---|
| not null | 不能为空 |
| default | 默认值 |
| primary key | 主键,不为空,唯一 |
| unique | 唯一 |
| check | [mysql不支持] |
| foreign key | 外键,字段来自主表 |
foreign key: 从表设置外键,被引用的表叫主表
-
创建表时添加列级约束
create table xx( id int primary key # 字段 类型 约束 ); # ! 不支持添加外键约束 -
创建表时添加表级约束
create table xxx( id int, stuname varchar(10), gender char(1), seat int, age int, majorid int, [constraint xx] primary key(id), # 添加主键约束 [constraint xx] unique(seat), # 唯一约束 [constraint xx] foreign key(majorid) references major(id) # 外键:foreign key(字段) references 外键主表(字段) # xx是约束名字,可以省略 # ! 不支持 not null default ) -
主键和唯一键的区别
键名 唯一 为空 组合(多个字段组合成一个约束) primary key √ × √ unique × √(但是只能一行为空) √ -
外键
- 再从表设置外键
- 外键字段要和主表中的关联列类型兼容
- 外键字段要来自主表中的key (primary key ,unique ,foreign key)
- 创建数据时先创建主表,删除时先删除从表
-
修改表时添加约束
# 添加列级约束 alter table xx modify column xxx int not null; # 添加表级约束 alter table xx add primary key(id); -
修改表时s删除约束
# 删除列级约束 alter table xx modify column xx; # 删除表级约束 alter table xx drop primary key;
4. TCL
事务控制语言
数据类型
数值型
-
整型
整数类型 字节 范围 tinyint 1 smallint 2 mediumint 3 int 4 bigint 8 设置无符号在类型后面加 unsigned -
小数
-
定点数
比浮点数精确
decimal(M,D)
M:总宽度
D:小数宽度 -
浮点数
类型 字节 float 4 double 8
-
字符型
包含文本和二进制
-
短字符
类型 特性 char(M) 长度不可变,占空间省时间 varchar(M) 长度可变,占时间省省间 enum(列表) 插入只能为列表中的值(一个) 单选框set(列表) 插入只能为列表中的值(多个) 复选框... ... M:字符数 -
长字符
日期型
| 类型 | 字节 | 特性 |
|---|---|---|
| date | 4 | 2021.11.11 |
| datetime | 8 | 2021.11.11 11:11:11 |
| timestamp | 4 | 123456789 |
| time | 3 | 11:11:11 |
| year | 1 | 2021 |
函数
函数分为单行函数和分组函数两类
1. 流程控制函数
/* *
语法:
case 值
when 值 then 输出结果
when 值 then 输出结果
else 输出结果
end;
或:
case
when 条件1 then 输出结果
when 条件2 then 输出结果
else 输出结果
end;
*/
# todo 第一种相当于switch case语句
# todo 第二中相当于if else
2. 单行函数
一行一个结果
3. 分组函数
多行一个结果,如:sum()
本文来自博客园,作者:panmengxiang,转载请注明原文链接:https://www.cnblogs.com/pmxisme/p/14991487.html

浙公网安备 33010602011771号