diff --git "a/12\346\236\227\344\277\212\344\274\237/1018\344\275\234\344\270\232.md" "b/12\346\236\227\344\277\212\344\274\237/1018\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..ca5eba1506fb6fa778137bbd0d1daaa9e3b713dd --- /dev/null +++ "b/12\346\236\227\344\277\212\344\274\237/1018\344\275\234\344\270\232.md" @@ -0,0 +1,53 @@ +index 索引 高效获取数据结构 + +普通索引:单纯提高效率 + +唯一索引:unique index 唯一索引只能够建立在数据不重复的列上,唯一约束,不能重复可以null,有多个唯一约束。 + +主键索引:primary key 唯一性,非空,不能重复,不能null,一个只能有一个主键 + +--------------- + +create index 索引名称 on 表名(列) + +----------------------------------------------------- + +alter table 表名 add index 索引名(列) + +--------------------------------------- + +在建表的同时也能一起建立索引 + +-------------------------------- + +drop index 索引名 on 表名 -- 删除 + +shou index from 表名 --查询 + +explain 用于查询某个语句的执行详情 + +explain select ··· from ··· --语句 + +---------------------------------------------- + +1.name字段为姓名字段,该字段的值可能会重复,为该字段创建索引。 + +create index tb_name on tb_user(name); + +2.phone手机号字段的值,是非空,且唯一的,为该字段创建唯一索引。 + +desc tb_user; + +create unique index tb_phone on tb_user(name); + +3.为profession、age、status创建联合索引。 + +create index tb_pas on tb_user(profession,age,status); + +4.为email建立合适的索引来提升查询效率。 + +create index tb_email on tb_user(email); + +5.查看tb_user表的所有的索引数据 + +show index from tb_user; \ No newline at end of file diff --git "a/12\346\236\227\344\277\212\344\274\237/1019\344\275\234\344\270\232.md" "b/12\346\236\227\344\277\212\344\274\237/1019\344\275\234\344\270\232.md" index ca5eba1506fb6fa778137bbd0d1daaa9e3b713dd..82840f859b0967552feec4bfa82cee6895daefde 100644 --- "a/12\346\236\227\344\277\212\344\274\237/1019\344\275\234\344\270\232.md" +++ "b/12\346\236\227\344\277\212\344\274\237/1019\344\275\234\344\270\232.md" @@ -1,53 +1,112 @@ -index 索引 高效获取数据结构 - -普通索引:单纯提高效率 - -唯一索引:unique index 唯一索引只能够建立在数据不重复的列上,唯一约束,不能重复可以null,有多个唯一约束。 - -主键索引:primary key 唯一性,非空,不能重复,不能null,一个只能有一个主键 - ---------------- - -create index 索引名称 on 表名(列) - ------------------------------------------------------ - -alter table 表名 add index 索引名(列) - ---------------------------------------- - -在建表的同时也能一起建立索引 - --------------------------------- - -drop index 索引名 on 表名 -- 删除 - -shou index from 表名 --查询 - -explain 用于查询某个语句的执行详情 - -explain select ··· from ··· --语句 - ----------------------------------------------- - -1.name字段为姓名字段,该字段的值可能会重复,为该字段创建索引。 - -create index tb_name on tb_user(name); - -2.phone手机号字段的值,是非空,且唯一的,为该字段创建唯一索引。 - -desc tb_user; - -create unique index tb_phone on tb_user(name); - -3.为profession、age、status创建联合索引。 - -create index tb_pas on tb_user(profession,age,status); - -4.为email建立合适的索引来提升查询效率。 - -create index tb_email on tb_user(email); - -5.查看tb_user表的所有的索引数据 - -show index from tb_user; \ No newline at end of file +部门表 +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 年工资; \ No newline at end of file