From 2969bb8985d6663324b0746a4f6b9aaa81a5d4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=B1=AA?= <2936219414@qq.com> Date: Thu, 19 Oct 2023 22:15:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10.18\347\254\224\350\256\260.md" | 85 +++++++++++++ .../10.19\344\275\234\344\270\232.md" | 114 ++++++++++++++++++ .../10.19\347\254\224\350\256\260.md" | 114 ++++++++---------- 3 files changed, 251 insertions(+), 62 deletions(-) create mode 100644 "54 \345\217\266\345\255\220\350\261\252/10.18\347\254\224\350\256\260.md" create mode 100644 "54 \345\217\266\345\255\220\350\261\252/10.19\344\275\234\344\270\232.md" diff --git "a/54 \345\217\266\345\255\220\350\261\252/10.18\347\254\224\350\256\260.md" "b/54 \345\217\266\345\255\220\350\261\252/10.18\347\254\224\350\256\260.md" new file mode 100644 index 0000000..7f80ad4 --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/10.18\347\254\224\350\256\260.md" @@ -0,0 +1,85 @@ +索引(index) + +索引是帮助mysql高效获取数据的数据结构 + +索引的分类 + +单列索引:一个索引建立在一个列上一个表可以建立多个单列索引 + +普通索引:单纯为了提高搜索效率 + +唯一索引:唯一索引只能建立在数据不同的列上 + +主键索引:不能重复只能有一个 + + + +直接建立索引 + +create index 索引名 on 表名(列名) + + + +修改表建立索引 + +alter table 表名 add index 索引名(列名) + +创建表时直接建立 + + + +如何查看一个表中有哪些索引 + +show index from 表名 + + + +删除索引 + +drop index 索引名 on 表名 + + + +唯一索引 + +与普通索引类似但索引列的值必须唯一 + +建立唯一约束时会自动创建唯一索引,名称就是列名 + + + +主键索引 + +是一种特殊的唯一索引不允许有空值创建主键索引就是创建主键约束 + +删除主键约束不需要列名 + + + +联合索引 + +create index 索引名 on 表名(列名1,列名2,...,列名n) + + + +explain 执行计划 + +可以检查某个语句的执行详细情况 + +explain select ... from ... + + 最左前缀法则(联合索引) + +如果使用了联合索引,要遵守最左前缀发则 + +指的是查询从最左列开始不跳过索引中的列,如果跳过后面索引失效 + + + +sql提示 + +select * from 表名 use index (索引) where ... 建议 + +select * from 表名 ignore index (索引) where ... 忽略 + +select * from 表名 force index (索引) where ... 强制 diff --git "a/54 \345\217\266\345\255\220\350\261\252/10.19\344\275\234\344\270\232.md" "b/54 \345\217\266\345\255\220\350\261\252/10.19\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b9c0fc8 --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/10.19\344\275\234\344\270\232.md" @@ -0,0 +1,114 @@ +````mysql +-- 部门表 +create table dept( + deptno int primary key auto_increment, -- 部门编号 + dname varchar(14) , -- 部门名字 + loc varchar(13) -- 地址 +) ; +-- 员工表 +create table emp( + empno int primary key auto_increment,-- 员工编号 + ename varchar(10), -- 员工姓名 - + job varchar(9), -- 岗位 + mgr int, -- 直接领导编号 + hiredate date, -- 雇佣日期,入职日期 + sal int, -- 薪水 + comm int, -- 提成 + deptno int not null, -- 部门编号 + foreign key (deptno) references dept(deptno) +); +insert into dept values(10,'财务部','北京'); +insert into dept values(20,'研发部','上海'); +insert into dept values(30,'销售部','广州'); +insert into dept values(40,'行政部','深圳'); +insert into emp values(7369,'刘一','职员',7902,'1980-12-17',800,null,20); +insert into emp values(7499,'陈二','推销员',7698,'1981-02-20',1600,300,30); +insert into emp values(7521,'张三','推销员',7698,'1981-02-22',1250,500,30); +insert into emp values(7566,'李四','经理',7839,'1981-04-02',2975,null,20); +insert into emp values(7654,'王五','推销员',7698,'1981-09-28',1250,1400,30); +insert into emp values(7698,'赵六','经理',7839,'1981-05-01',2850,null,30); +insert into emp values(7782,'孙七','经理',7839,'1981-06-09',2450,null,10); +insert into emp values(7788,'周八','分析师',7566,'1987-06-13',3000,null,20); +insert into emp values(7839,'吴九','总裁',null,'1981-11-17',5000,null,10); +insert into emp values(7844,'郑十','推销员',7698,'1981-09-08',1500,0,30); +insert into emp values(7876,'郭十一','职员',7788,'1987-06-13',1100,null,20); +insert into emp values(7900,'钱多多','职员',7698,'1981-12-03',950,null,30); +insert into emp values(7902,'大锦鲤','分析师',7566,'1981-12-03',3000,null,20); +insert into emp values(7934,'木有钱','职员',7782,'1983-01-23',1300,null,10); +-- 完成以下练习题 +-- +-- 1、列出最低薪金大于1500的各种工作。 +-- +select job,sal from emp where sal>1500; +-- 2、列出在部门 "销售部" 工作的员工的姓名,假定不知道销售部的部门编号。 +-- +select * from emp; + +select d.dname,ename from emp e,dept d where e.deptno=d.deptno and dname='销售部'; +-- 3、列出薪金高于公司平均薪金的所有员工。 +-- +select * from emp; + +with +a as (select avg(sal) over() as n from emp) +select distinct ename,sal,a.n from emp e,a where e.sal>n; +-- 4、列出与"周八"从事相同工作的所有员工。 +-- +select * from emp; + +with +a as (select job from emp where ename='周八') +select * from emp e,a where e.job=a.job; +-- 5、列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。 +-- +select * from emp; + +with +a as (select sal from emp where deptno=30) +select ename,e.sal from emp e,a where e.sal=a.sal; +-- 6、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。 +-- +select * from emp; + +with +a as (select max(sal) over() n from emp where deptno=30) +select distinct ename,e.sal from emp e,a where e.sal>a.n; +-- 7、列出在每个部门工作的员工数量、平均工资、平均服务年限。 +-- +select * from emp; + +with +a as (select distinct count(*) over(partition by dname) as a1,dname from emp e,dept d where e.deptno=d.deptno), +b as (select distinct avg(sal) over(partition by dname) as b1,dname from emp e,dept d where e.deptno=d.deptno), +c as (select distinct avg(floor(datediff(now(),hiredate)/365)) over(partition by dname) as c1,dname from emp e,dept d where e.deptno=d.deptno) +select a.a1 员工数量,b.b1 平均工资,c.c1 平均服务年限,d.dname 部门 from a,b,c,dept d where a.dname=b.dname and b.dname=c.dname and c.dname=d.dname; +-- 8、列出所有员工的姓名、部门名称和工资。 +-- +select * from emp; + +select ename,dname,sal from emp e,dept d where e.deptno=d.deptno; +-- 9、列出所有部门的详细信息和部门人数。 +-- +select * from emp; + +with +a as (select distinct count(*) over(partition by dname) as a1,dname from emp e,dept d where e.deptno=d.deptno) +select d.*,a.a1 部门人数 from dept d,a where d.dname=a.dname; +-- 10、列出各种工作的最低工资。 +-- +select * from emp; + +select distinct job,min(sal) over(partition by job) from emp; +-- 11、列出各个部门的 经理 的最低薪金。 +-- +select * from emp; + +select dname,min(sal) over(partition by e.deptno) from emp e,dept d where e.deptno=d.deptno and job='经理'; +-- 12、列出所有员工的年工资,按年薪从低到高排序。 +-- +select * from emp; + + +select *,(sal+ifnull(comm,0))*12 as 年工资 from emp order by 年工资; +```` + diff --git "a/54 \345\217\266\345\255\220\350\261\252/10.19\347\254\224\350\256\260.md" "b/54 \345\217\266\345\255\220\350\261\252/10.19\347\254\224\350\256\260.md" index 7f80ad4..4495e6c 100644 --- "a/54 \345\217\266\345\255\220\350\261\252/10.19\347\254\224\350\256\260.md" +++ "b/54 \345\217\266\345\255\220\350\261\252/10.19\347\254\224\350\256\260.md" @@ -1,85 +1,75 @@ -索引(index) +#### 事务 -索引是帮助mysql高效获取数据的数据结构 +​ 数据库中的事务是指对数据库执行一批操作,在同一个事务中,这些操作最终要么全部执行成功,要么全部失败,不会存在部分成功的情况。 -索引的分类 +##### 事务的几个特性(ACID) -单列索引:一个索引建立在一个列上一个表可以建立多个单列索引 +1.原子性(Atomicity) -普通索引:单纯为了提高搜索效率 +2.一致性(Consistency) -唯一索引:唯一索引只能建立在数据不同的列上 +3.隔离性(Isolation) -主键索引:不能重复只能有一个 +4.持久性(Durability) +##### 事务分为隐式事务和显式事务 +​ 1.隐式事务:事务的自动开启,提交或回滚,由mysql内部自动控制 -直接建立索引 +``` mysql +-- 查看变量autocommit是否开启了自动提交、 + show variables like 'autocommit';(为ON表示开启了自动提交) +``` -create index 索引名 on 表名(列名) +​ 2.显式事务:事务需要手动开启,提交或回滚,由开发者自己控制 +``` mysql +-- 设置不自动提交事务 + set autocommit = 0(off); +-- 开启事务 + start transaction; +-- 执行事务操作 + commit;(提交事务) + rollback;(回滚事务) +``` +##### savepoint关键字 -修改表建立索引 +``` mysql + savepoint p1; -- 设置一个保存点 + rollback to p1; -- 将p1到当前句的语句全部回滚 +``` -alter table 表名 add index 索引名(列名) +``` mysql +-- 只读事务 + start transaction read only; +``` -创建表时直接建立 +##### 事务中的一些问题 +1.更新丢失 +2.脏读 -如何查看一个表中有哪些索引 +3.读已提交 -show index from 表名 +4.不可重复读 +5.可重复读 +6.幻读 -删除索引 +#### 事务中的隔离级别 -drop index 索引名 on 表名 - - - -唯一索引 - -与普通索引类似但索引列的值必须唯一 - -建立唯一约束时会自动创建唯一索引,名称就是列名 - - - -主键索引 - -是一种特殊的唯一索引不允许有空值创建主键索引就是创建主键约束 - -删除主键约束不需要列名 - - - -联合索引 - -create index 索引名 on 表名(列名1,列名2,...,列名n) - - - -explain 执行计划 - -可以检查某个语句的执行详细情况 - -explain select ... from ... - - 最左前缀法则(联合索引) - -如果使用了联合索引,要遵守最左前缀发则 - -指的是查询从最左列开始不跳过索引中的列,如果跳过后面索引失效 - - - -sql提示 - -select * from 表名 use index (索引) where ... 建议 - -select * from 表名 ignore index (索引) where ... 忽略 - -select * from 表名 force index (索引) where ... 强制 +``` mysql +-- 读未提交 + read_uncommitted +-- 读已提交 + read_committed +-- 可重复读(默认) + repeatable_read +-- 串行 + serializable +-- 以上4种隔离级别越来越强,会导致数据库的并发性也越来越低 +-- 查看隔离级别 + show variables like 'transaction_isolation'; \ No newline at end of file -- Gitee