实训日记Day3(数据库篇)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CNZSWYMP/article/details/86089102

/*2019.1.8
1.统计函数,count,max,min,sum,avg
2.分组函数group by
3.having字句
4.联系8-12
5.链接查询
6.子查询
*/

工具 SQLDevelopment

--count
select count(*) from emp;
select count(1) from emp;
select count(ename) from emp;
select count(comm) from emp;--选择不为空的字段
select count(distinct deptno) from emp;--distinct过滤重复信息
--count(*)和count(1)的效果一样 count(1)效率更高

--max
select max(sal) from emp;
select max(sal) from emp where deptno=20;
--min
select min(sal) from emp ;
select min(sal) from emp where deptno<>20;
--sum 
select sum(sal) from emp;
--avg
select avg(sal) from emp;
select round(avg(sal),2) from emp;--保留两位小数(四舍五入)
select trunc(avg(sal),2) from emp;--保留两位小数(截取)
--分组函数(和统计函数组合)
--select 语句中的查询列一定要在分组函数group by后体现
select deptno, max(sal) from emp group by deptno;--求各部门的最高工资
select deptno, min(sal) from emp group by deptno;--求各部门的最低工资
select deptno,round(avg(sal),2) from emp group by deptno;--求各部门平均工资
select deptno, sum(sal) from emp group by deptno;--各部门工资总和
select deptno,job, count(*) from emp group by deptno,job;--统计各部门各个岗位的数量;select子句中出现字段,group by子句中必须出现
--having字句;有group by字句不一定有having字句,但是有having字句一定有group by语句
select deptno,count(*) from emp group by deptno having count(*)>4;--查询人数大于四的部门的信息;where字句不能放分组函数。
--链接查询
--内连接
  --显式内连接
  select e.*,d.dname,d.loc 
  from emp e
  inner join dept d on e.deptno=d.deptno
  where d.deptno!=10;
  --隐式内连接
  select e.*,d.dname,d.loc 
  from emp e,dept d
  where e.deptno=d.deptno and d.deptno!=10;
--外连接
  --左外连接
  select e.*,d.dname,d.loc 
  from emp e
  left join dept d on e.deptno=d.deptno
  where d.deptno!=10;
  --右外连接
  select e.*,d.dname,d.loc 
  from emp e
  right join dept d on e.deptno=d.deptno
  where d.deptno!=10;
  --完整外连接
  select e.*,d.dname,d.loc 
  from emp e
  full join dept d on e.deptno=d.deptno
  where d.deptno!=10;
--内连接查询左表和右表部分信息
--左外连接查询左表所有信息和右表部分信息
--右外连接查询左表部分信息和右表全部信息
--完整外连接查询左表全部信息和右表全部信息

--单行子查询
select 身高 from student where sname='wangchen';
select * from student where high>176;
select * from student where high>(select 身高 from student where sname='wangchen');
--在emp表中查询工资比ALLEN高的员工信息
select sal from emp where ename ='ALLEN';
select * from emp where sal>1600;
--括号内的语句叫做子查询语句,括号外的部分叫做主查询语句
select * from emp where sal>(select sal from emp where ename ='ALLEN');
--多行子查询
  --any all in
--在emp表中查询比30部门员工工资高的员工信息
select sal from emp where deptno=30;
--大于any等效于大于最小值
select * from emp where sal>any(select sal from emp where deptno=30);
--大于all等效于大于最大值
select * from emp where sal>all(select sal from emp where deptno=30);
--小于any等效于小于最大值
select * from emp where sal<any(select sal from emp where deptno=30);
--小于all等效于小于最小值
select * from emp where sal<all(select sal from emp where deptno=30);
--in
select * from emp where sal in (select sal from emp where deptno=30);
--即select * from emp where sal=1600 or sal=1250 or sal=2950 or sal=1500 or sal=950
--等于any等效于in
select * from emp where sal=any(select sal from emp where deptno=30);
--相关子查询
--查询部门员工大于二的部门信息
select * from dept d where 2<(select count(*) from emp e where d.deptno=e.deptno);

/*2019.1.8 下午
1.联系1 13-16
2.excel数据导入数据库
3.视图
  简单试图
  复杂试图
  连接视图
  只读视图
  检查视图
*/

--简单视图 可进行增删改查,会影响表中数据,表进行增删改查,会影响视图
create view vw_emp1 as select empno,ename,sal,deptno from emp;
select * from emp;
select * from vw_emp1;
insert into vw_emp1 values(7777,'doom',3800,30);
update vw_emp1 set ename='DOOM' where EMPNO=7777;
update emp set ename='doom' where EMPNO=7777;
delete from VW_EMP1 where EMPNO=7777;
--复杂视图 只可以查询,不可以修改插入删除。
create view vw_emp2(deptno,total,avgsal) as 
select deptno,count(*),round(avg(sal),2) from emp group by deptno;
--连接视图 可以查询,不可以插入,满足条件时刻进行修改,删除
create view vw_emp3 as
select empno,ename,sal,dname,loc from emp,dept where emp.deptno=dept.deptno;
select * from VW_EMP3;
update VW_EMP3 set sal='801' where empno=7369;
--只读视图 只可以进行查询,不能进行插入,修改,删除
create view vw_emp4 as select empno,ename,sal,deptno from emp
with read only;
  --insert into vw_emp4 values(7777,'doom',3800,30);
--check约束 可以查看和删除,满足条件下可以修改和插入
create view vw_emp5 as
select empno,ename,sal,deptno from emp where sal>1500--此处约束对视图生效,对表不生效
with check option constraint ck_sal;
insert into VW_EMP5 values(7778,'COCO',5300,10);
update VW_EMP5 set sal='530' where empno=7778;--子句违规
delete from VW_EMP5 where empno=7778;
insert into VW_EMP5 values(7778,'COCO',500,10);--子句违规

实训日记数据库篇结束啦,明天考试

猜你喜欢

转载自blog.csdn.net/CNZSWYMP/article/details/86089102