mysql basic notes

SQL basics

sql general syntax

  1. SQL statements can be written in one or more lines, ending with a semicolon
  2. SQL statements can use spaces/indentation to enhance the readability of the statement
  3. The SQL statement of the MYSQL database is not case-sensitive
  4. Notes:
    • Single-line comments: --comment -content or # comment-content
    • Multi-line comment: / * comment content * /

SQL classification

Classification illustrate
DDL Data Definition Language, used to define database objects (databases, tables, fields)
DML Data manipulation language, used to add, delete, and modify data in database tables
DQL Data query language, used to query the records of tables in the database
DCL Data control language, used to create database users and control database access rights

DDL

  1. database operation
  • Inquire
-- 查询所有数据库
show databases;

-- 查询当前数据库
select database();
  • create
creat database [if not exists] 数据库名 [default charset 字符集][collate 排序规则];
  • delete
drop database [if exists]数据库名	
  • use
use 数据库名
  1. Table Operations - Query
-- 查询挡墙数据库所有表
show tables;

-- 查询表结构
desc 表名

-- 查询指定表的建表语句
show create table 表名;
  1. Table operation - create
create table 表名(
	字段1 字段1类型 [comment 字段1注释],
	字段2 字段2类型 [comment 字段2注释],
	...
	字段n 字段n类型 [comment 字段n注释]
);
  1. Table Operations—Modify
-- 添加字段
alter table 表名 add 字段名 类型 [comment 注释] [约束];

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

-- 修改字段名和字段类型
alter table 表名 change 旧字段名 新字段名 类型 [comment 注释] [约束];

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

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

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

DML

  • Add data (insert)
-- 1.给指定字段添加数据
insert into 表名(字段1,字段2,...) values(值1,值2,...);

-- 2.给全部字段添加数据
insert into 表名 values(值1,值2...);

-- 3.批量添加数据
insert into 表名 (字段名1,字段名2,...) values (值1,值2...),(值1,值2...);

/* 插入数据时,指定数据的字段顺序需要与值的顺序是一一对应的
   字符串和日期型数据应该包含在引号中
   插入的数据大小,应该在规定的范围内*/
  • Modify data (update)
update 表名 set 字段名1 = 值1,字段名2 = 值2,...[where 条件];
/* update语句的条件可以有也可以没有,如果没有,则修改整张表的所有数据*/
  • Delete data (delete)
delete from 表名 [where 条件];
/* delete语句的条件可以有也可以没有,如果没有,则删除整张表的所有数据
   delete语句不能删除某一个字段的值 (可以使用update)*/

DQL

-- DQL语法
select 
	字段列表
from
	表名列表
where
	条件列表
group by 
	分组字段列表
having
	分组后条件列表
order by
	排序字段列表
limit
	分页参数;
  • basic query
-- 1.查询多个字段
select 字段1,字段2,字段3...from 表名;
select * from 表名;
-- 2.设置别名
select 字段1 as 别名1,字段2 as 别名2... from 表名;
-- 3.去除重复记录
select distinct 字段列表 from 表名;
  • conditional query (where)
select 字段列表 from 表名 where 条件列表;

Please add a picture description

  • aggregate function
  1. Take a column of data as a whole and perform longitudinal calculations

  2. Common aggregate functions

function Function
count total number
max maximum value
min minimum value
avg average value
sum to sum
  1. grammar
select 聚合函数(字段列表)from 表名;
  • Group query
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];

Note: The difference between where and having

  1. Execution timing is different: where is to filter before grouping, if the where condition is not met, it does not participate in grouping, and having is filtered after grouping
  2. The judgment conditions are different: where cannot be judged using aggregate functions, but having can
  3. Execution order: where>aggregate function>having
  4. After grouping, the fields to be queried are generally aggregation functions and grouping fields, and it is meaningless to query other fields
  • Sort query
  1. grammar
select 字段列表 from 表名 order by 字段1 排序方式1,字段2,排序方式2;
  1. sort by
    • ASC ascending (default)
    • DESC descending

Note: If it is multi-field sorting, when the first field is the same, it will be sorted according to the second field

  • Paging query
select 字段列表 from 表名 limit 起始索引,查询记录数;

Notice:

  • The starting index starts from 0, starting index = (query page number - 1) * number of records displayed on each page
  • Pagination query is a database dialect, different databases have different implementations, mysql is limit
  • If the data of the first page is queried, the starting index can be omitted, and limit 10 can be abbreviated directly

case study

-- 1.查询年龄为20,21,22岁的女性员工信息
select * from emp where gender  = '女' and age in(20,21,22);
-- 2.查询性别为男,并且年龄在 20-40岁以内的姓名为三个字的员工
select * from emp where gender='男' and age between 20 and 40and name like'---';
-- 3.统计员工表中,年龄小于60岁的,男性员工和女性员工的数量
select gender,count(*) from emp where age <60 group by gender;
-- 4.查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序
select name age from emp where age <=35 order by age,entrydate desc;
-- 5.查询性别为男,且年龄在20-40的前五个员工信息,对查询结果按年龄升序排序,年龄相同按入职时间升序排序
select * from emp where gender = '男' and age between 20 and 40 order by age asc,entrydate asc limit 5;

DQL execution order

Please add a picture description

DCL

Used to manage database users and control database access rights

-- 1. 查询用户
USE mysql;
select * from user;
-- 2.创建用户
create user '用户名'@'主机名' identified by '密码';
-- 3.修改用户密码
alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';
-- 4.删除用户
drop user '用户名'@'主机名';
-- 1.查询权限
show grants for '用户名'@'主机名';
-- 2. 授予权限
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
-- 3. 撤销权限
revoke 权限列表 on 数据库名.表名 to '用户名'@'主机名';

String functions

function Function
concat(s1,s2,…sn) string concatenation
lower(str) Convert a string to all lowercase
upper(str) Convert a string to all uppercase
lapd(str,n,pad) Left padding, fill the left side of str with the string pad to reach the length of n strings
rpad(str,n,pad) Right padding, fill the right side of str with the string pad, and reach the length of n strings
trim(str) Remove leading and trailing spaces from a string
substring(str,start,len) Returns the length of len characters from the start position of the string str

Numeric function

function Function
ceil(x) Rounded up
floor(x) round down
mod(x) Returns the modulus of x/y
rand() Returns a random number between 0 and 1
round(x,y) Find the rounded value of the parameter x, retaining y digits of significant digits

date function

function Function
curdate() return current date
curtime() return current time
now() Returns the current time and date
year(date) Get the year of the specified date
month(date) Get the month of the specified date
day(date) Get the date of the specified date
date_add(date,interval expr type) Returns a date/time value plus a time value after the interval expr
datadiff(date1,date2) Returns the number of days between the start time date1 and the end time date2

process function

function Function
if(value,t,f) If value is true, return t, otherwise return f
ifnull(value1,value2) If value1 is not empty, return value1, otherwise return value2
case when [ val1] then [ res1]…else [ default] end If val1 is true, return res1, ... otherwise return defult default value
case[ expr1] when [ val1] then [ res1] …else [default] end If the value of expr is equal to val1, return res1, ... otherwise return the default default value

constraint

1.概念:约束是作用于表中字段的规则,用于限制存储在表中的数据

2.目的:保证数据库中数据的正确、有效性和完整性

3.分类

约束 描述 关键字
非空约束 限制该字段的数据不能为空 NOT NULL
唯一约束 保证该字段的所有数据都是唯一的,不重复的 UNIQUE
主键约束 主键是一行数据的唯一标识,要求非空且唯一 PRIMARY KEY
默认约束 保存数据时,如果未指定该字段的值,则采用默认值 DEFAULT
检查约束 保证字段值满足某一个条件 CHECK
外键约束 用来让两张表之间建立连接,保证数据一致性和完整性 FOREIGN KEY

多表查询

概述:从多张表中查询数据

多表关系:一对多、多对多、一对一

一对多

  • 案例:部门与员工的关系
  • 关系:一个部门对应多个员工,一个员工对应一个部门
  • 实现:在多的一方建立外键,指向一的一方的主键

多对多

  • 案例:学生与课程的关系
  • 关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择
  • 实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

一对一

  • 案例:用户与用户详情的关系
  • 关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率
  • 实现:在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(unique)

笛卡尔积:指在数学中,两个集合A和B的所有组合情况(在多表查询时,需要消除无效的笛卡尔积)
Please add a picture description

  • 多表查询分类
    • 连接查询
      • 内连接:相当于查询A、B交集部分数据
      • 外连接
        • 左外连接:查询左表所有数据,以及两张表的交集部分数据
        • 右外连接:查询右表的所有数据,以及两张表交集部分数据
      • 自连接:当前表与自身的连接查询,自连接必须使用表别名
    • 子查询

内连接

-- 隐式内连接
select 字段列表 from 表1,表2 where 条件:
-- 案例1 查询每一个员工的姓名,及关联的部门的名称
select e.name,d.name from emp e,dept d where e.dept_id = d.id;

-- 显式内连接
select 字段列表 from 表1 [inner] join 表2 on 连接条件...;
-- 案例2 查询每一个员工的姓名,及关联的部门名称
select e.name,d.name from emp e inner join dept d on e.dept_id = d.id;

外连接

-- 左外连接
select 字段列表 from 表1 left [outer] join 表2 on 连接条件;
-- 案例 查询emp表所有的数据,和相应的部门信息
select e.*,d.name from emp e left join dept d on e.dept_id = d.id;

-- 右外连接
select 字段列表 from 表1 right [outer] join 表2 on 连接条件;
-- 案例 查询dept表的所有数据,和相应的部门信息
select e.*,d.* from emp e right join dept d on e.dept_id = d.id;

自连接

自连接查询,可以是内连接查询,也可以是外连接查询

select 字段列表 from 表A 别名A join 表A 别名B on 条件...;
-- 案例 查询所有员工emp及其领导的名字emp,如果员工没有领导,也需要查询出来
select * from emp e1 left join emp e2 on e1.managerid = e2.id;

联合查询 -union,union all

对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集

select 字段列表 from 表A... 
union[all]
select 字段列表 from 表B ...;

1.对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致

2.union all会将全部的数据直接合并在一起,union会对合并之后的数据进行去重

子查询

SQL语句中嵌套SELECT语句,称为嵌套查询,又称子查询

select * from t1 where column=(select column1 from t2);

子查询外部的语句可以是 insert、update、delete、select的任何一个

根据子查询的的结果不同,分为

  • 标量子查询(查询结果为单个值)

  • 列子查询 (查询结果为一列)

  • 行子查询(查询结果为一行)

  • 表子查询(查询结果为多行多列)

标量子查询

子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式

常用操作符:= < > >= <=

列子查询

返回的结果是一列(可以是多行)

常用操作符:in 、not in、any、some、all

操作符 功能
in 在指定的集合范围之内,多选一
not in 不在指定的范围之内
any 子查询返回列表中,有任意一个满足即可
some 与any等同,使用some的地方都可以使用any
all 子查询返回列表的所有值都必须满足

行子查询

子查询的结果是一行(可以是多列)

常用操作符:=、<>、in、not in

表子查询

子查询返回的结果是多行多列

常用操作符:in

多表查询案例

--1.查询员工的姓名、年龄、职位、部门信息(隐式内连接)
select e.name,e.age,d.job,d.name from emp e,dept d where e.dept_id = d.id;

--2.查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显式内连接)
select e.name,e.age,e.job,d.name from emp e inner join dept d on e.dept_id=d.id where e.age <30;

--3.查询拥有员工的部门id,部门名称
select distinct d.name,d.name from emp e,dept t where e.dept_id = d.id;

--4.查询所有年龄大于40岁的员工,及其归属的部门名称,如果没有归属部门,也需要展示出来
select e.*,e.name from emp e left join dept d on e.dept_id = d.id where e.age >40;

--5.查询所有员工工资等级
select e.*,s.grade from emp e,salgrade s where e.salary >=s.losary and e.salary <=s.hisal

--6.查询研发部所有员工的信息及工资等级
-- 连接条件:emp.salary >= s.losary and e.salary <=s.hisal,emp.dept_id = dept.id
select e.*,s.grade from emp e,dept d,sargrade s where e.dept_id = d.id and (emp.salary >= s.losary and e.salary <=s.hisal) and e.name = '研发部';

--7.查询研发部的平均工资
select avg(e.salary) from emp e,dept d where e.dept_id = d.id and d.name = '研发部';

--8.查询工资比灭绝 高的员工信息
select * from emp where salary >(select sarlary from emp where name = '灭绝');

--9.查询比平均薪资高的员工信息
select * from emp where salary >(select avg(sarlary) from emp);

--10.查询低于本部门平均工资的员工信息
select * from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);

--11.查询所有的部门信息,并统计部门的员工人数
select d.id,d.name,(select count(*) from emp e where e.dept_id = d.id) '人数'from dept d;

--12.查询所有学生的选课情况,展示出学生名称,学号,课程名称
--student,course, student_course三张表
--连接条件:studenet.id  = student_course.studentid,course.id = student_course.courseid

select s.name,s.no,c.name from student s,student_course sc,course c where s.id = sc.studentid and sc.courseid = c.id;

事务

​ 事务是一组操作的集合,他是一个不可分割的工作单位,事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败

事务操作

  • 查看/设置事务提交方式
-- 查看事务的自动提交方式 1 自动提交   0 手动提交
select @@autocommit;
-- 修改事务提交方式为 手动提交
set @@autocommit = 0; 
  • 开启事务
start transaction;
  • 提交事务
commit;
  • 回滚事务
rollback;

案例 转账操作

select @@autocommit;

set @@autocommit = 0;

-- 转账操作(张三给李四转1000)
-- 1. 查询张三账户余额
select * from account where name = '张三';
-- 2.将张三账户余额-1000
update account set money = money-1000 where name = '张三';

程序执行报错。。。 -- 若抛出异常
-- 3.将李四的账户余额+1000
update account set money = money+1000 where name = '李四';

-- 提交事务
commit;

-- 回滚事务
rollback;

事务的四大特性(ACID)

  • 原子性(Atomicity):事务是不可分割的最小操作单元,要么全部成功,要么全部失败
  • 一致性(Consistency):事务完成时,必须使所有的数据都保持一致状态
  • 隔离性(Isolation):数据库系统提供的隔离机制,保证事务在不受外部并发操作影响的独立环境下运行
  • 持久性(Durability):事务一旦提交或回滚,他对数据库的改变就是永久的

并发事务引发的问题

问题 描述
脏读 一个事务读到另外一个事务还没提交的数据
不可重复读 一个事务先后读取同一条记录,但两次读取的数据不同,称之为不可重复读
幻读 一个事务按照条件查询数据时,没有对应的数据行,但是在插入数据时,又发现了这行数据已经存在,好像出现了‘幻影’

事务的隔离级别

隔离级别 脏读 不可重复读 幻读
Read uncommitted
Read committed ×
Repeatable Read(默认) × ×
Serializable × × ×

从上到下,隔离级别越来越高,性能越来越差,数据越来越安全

  • Read uncommitted:是事务中最低的级别,在该级别下的事务可以读取到其他事务中未提交的数据,这种读取的方式也被称为脏读(Dirty Read)。简而言之,脏读是指一个事务读取了另外一个事务未提交的数据。

    例如,Alex 要给 Bill 转账100元购买商品,Alex开启事务后转账,但不提交事务,通知Bill来查询,如果Bill的隔离级别较低,就会读取到Alex的事务中未提交的数据,发现Alex确实给自己转了100元,就给Alex发货。等Bill发货成功后,Alex将事务回滚,Bill就会受到损失,这就是脏读造成的。

  • Read committed:是大多数 DBMS (如 SQL Server、Oracle) 的默认隔离级,但不包括MySQL。

    在该隔离级下只能读取其他事务已经提交的数据,避免了脏读数据的现象。但是在该隔离级别下,会出现不可重复读(NON-REPEATABLE READ)的问题。

    不可重复读是指在一个事务中多次查询的结果不一致,原因是查询的过程中数据发生了改变。

    例如,在网站后台统计所有用户的总金额,第1次查询Alex有900元,为了验证查询结果,第2次查询Alex有800元,两次查询结果不同,原因是第2次查询前Alex取出了100元。

  • Repeatable Read: MySQL's default transaction isolation level, which solves the problems of dirty reads and non-repeatable reads, and ensures that multiple instances of the same transaction will see the same results when reading data concurrently. But in theory, the phenomenon of phantom reading (PHANTOM READ) will appear in this isolation level.

    Phantom reading, also known as phantom reading, means that the number of data items in two queries in a transaction is inconsistent. Phantom reading and non-repeatable reading are somewhat similar, and they also occur during the two query processes. The difference is that phantom reading is due to the operation of inserting records by other transactions, resulting in an increase in the number of records. However, MySQL's InnoDB storage engine solves the problem of phantom reading through a multi-version concurrency control mechanism.

    For example, when counting the total amount of all users in the background of the website, there are currently only two users, and the total amount is 2,000 yuan. If you add a new user at this time and deposit 1,000 yuan, the total amount becomes 3,000 yuan when you count again , resulting in a phantom read.

  • Serializable: It is the highest level of isolation. It locks each read data row so that there will be no conflicts, thus solving the problems of dirty reads, non-repeatable reads, and phantom reads. However, since locking may lead to timeout (Timeout) and lock contention (Lock Contention), SERIALIZABLE is also an isolation level with the lowest performance. This isolation level will only be selected when it is necessary to forcibly reduce concurrency for the sake of data stability.

-- 查看事务隔离级别
select @@TRANSACTION_ISOLATION;

-- 设置事务隔离级别
set [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {Read uncommitted|Read committed|Repeatable Read|Serializable}

In theory, the phenomenon of phantom reading (PHANTOM READ) will appear in this isolation level.

Phantom reading, also known as phantom reading, means that the number of data items in two queries in a transaction is inconsistent. Phantom reading and non-repeatable reading are somewhat similar, and they also occur during the two query processes. The difference is that phantom reading is due to the operation of inserting records by other transactions, resulting in an increase in the number of records. However, MySQL's InnoDB storage engine solves the problem of phantom reading through a multi-version concurrency control mechanism.

For example, when counting the total amount of all users in the background of the website, there are currently only two users, and the total amount is 2,000 yuan. If you add a new user at this time and deposit 1,000 yuan, the total amount becomes 3,000 yuan when you count again , resulting in a phantom read.

  • Serializable: It is the highest level of isolation. It locks each read data row so that there will be no conflicts, thus solving the problems of dirty reads, non-repeatable reads, and phantom reads. However, since locking may lead to timeout (Timeout) and lock contention (Lock Contention), SERIALIZABLE is also an isolation level with the lowest performance. This isolation level will only be selected when it is necessary to forcibly reduce concurrency for the sake of data stability.
-- 查看事务隔离级别
select @@TRANSACTION_ISOLATION;

-- 设置事务隔离级别
set [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {Read uncommitted|Read committed|Repeatable Read|Serializable};

Summarize:

The above is the knowledge of Mysql basics, from general syntax to DDL, DCL, DML, DQL statements, functions, constraints, multi-table queries and transactions. The content of this article is my study notes summed up while studying MySQL. It can be used as a reference for learning. There may be some omissions and errors. You are welcome to criticize and correct. The remaining advanced articles include database programming, engines, indexes, locks, etc. The content will continue to be updated after learning. I hope everyone will learn and progress together!

Guess you like

Origin blog.csdn.net/m0_51353633/article/details/129678427