编程练习生的第e二十天 (数据库操作)

今天学习了数据库的一些操作

  1. distinct 这个关键字的用法 去除重复
    select distinct * from TEACHER ;
    用在字段之前 去除重复的字段
  2. BETWEEN 字段 and 字段 --between 小范围值 and 大范围的值 两者之间 <= >=
 select * from Score  where DEGREE BETWEEN 60 AND 80;

3 降序和升序
ORDER BY 默认是升序 降序在字段后面 加上 desc

select * from score  ORDER BY cno , Degree desc;

先按照升序排列, 如果有一样的 按照降序排列。
4. OR AND NOT or(或者) and (并且) not (取反)

select ename,job,sal from emp where sal=1600 or sal=3000;
select ename,job,sal from emp where not (sal=1600 or sal=3000);
select cno , avg(degree)from score group by cno having cno like '3%' and  count(*)>5;

5.集合
–集合
–Union,并集(去重) 对两个结果集进行并集操作,不包括重复行同时进行默认规则的排序;
–Union All,全集(不去重) 对两个结果集进行并集操作,包括重复行,不进行排序 ;
–Intersect,交集(找出重复) 对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
–Minus,差集( 减去重复 ) 对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序

select ename,sal,comm from emp where sal>1500 or comm is not null;
select ename,sal,comm from emp where sal>1500;
select ename,sal,comm from emp where comm is not null;
--并集
select ename,sal,comm from emp where sal>1500 
Union
select ename,sal,comm from emp where comm is not null;
select ename,sal,comm from emp where sal>1500 
Union all
select ename,sal,comm from emp where comm is not null;

6.函数类
–单行函数:一条数据返回一个结果
–多行函数|组函数|聚合函数:多条数据返回一个结果

select sno, cno from score (select  max(degree) from score);
先走from 然后求出最大的成绩 用最大的成绩去匹配学号和课程号

7.时间函数
当前时间

select sysdate from dual;
select current_date from dual;
select ename,sysdate from emp;

– 加减日期
– 2天以后是几号

select sysdate+2 from dual;

– 查询当前月的最后一天

select last_day(sysdate) from dual;
select last_day(hiredate) from emp;

– 下一个星期三是几号

select next_day(sysdate,'星期一') from dual;

时间和字符串之间的关系
1.转换

-- to_date('字符串','识别日期字符串模板')
-- 设定一个特定的时间(用一个特定的时间字符串转换为日期)
select to_date('2019-07-30 10:11:13','yyyy-mm-dd hh24:mi:ss')+3 from dual;
select to_date('2019年07月30日 10:11:13','yyyy"年"mm"月"dd"日" hh24:mi:ss') from dual;

– 将日期转为特定格式的字符串 to_char()

select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh24:mi:ss') from dual;

8.判断方法
– decode 判断decode(判定字段,校验字段值1,结果1,校验字段2,结果2。。,默认值)
–给每个部门后后面添加一个伪列,如果10部门,伪列显示为十,二十,三十…

select deptno,dname,decode(deptno,10,'十',20,'二十',30,'三十',40,'四十') 中文名字 from dept;

9.组函数的使用
组函数有-- count() sum() max() min() avg()
–对确定的结果集使用函数得结果

–注意: select后 组函数不能和非组函数或分组字段一起使用
–注意: where 不能使用组函数

select cno , avg(degree)from score group by cno having cno like '3%' and  count(*)>5;
select *
  from score
 where cno = '3-105'
   and degree > (select degree
                   from score
                  where sno = '109'
                    and cno = '3-105');
select degree from score where sno in 
(select sno from score
 group by sno having  count(1)>1) and degree <> (select max (degree)from score );

10.分组 group by 方法
用法:–select 数据 from 数据源 where 行过滤条件 group by 分组字段 having 组过滤信息 order by 排序字段;
–执行顺序: from --where–group by–having–select–order by

select tname from teacher
	 where tno = (select tno from course where cno = (select cno from score group by cno having count(*)>5));

–先过滤再分组 select max(sal) from emp where deptno in(10,30) group by deptno ;
–先分组再过滤 select max(sal),deptno from emp group by deptno having deptno in(10,30);

**--如果有分组,select后只能跟分组字段和组函数**

11.rowin 和 rownum 方法
–rowid和rownum都是伪列
–rowid相当于表中每一个条记录的地址,数据插入到表中的时候就已经存在,后续不会改变

select empno,ename,rowid from emp;
**rowin是去重的另一种方法**
去重,没有主键,没有唯一的字段,可以存在多条数据重复,想要达到去重,可以使用rowid

–rownum结果集中数据的序号 可以用来分页查询
/*
不是太会 以后再补

*/
12.连表查询
这个就比较简单了,分为92语法和99语法, 下面给大家介绍92语法
92语法就是求笛卡尔积

select * from emp,dept order by sal;

–等值连接 可以是两个表中的相同字段做连接,可以是不同字段做连接,但是类型要保持一致

查询所有学生的Sname、Cno和Degree列。
select sno, cname, degree from course cou, score sco where sco.cno= cou.cno;
查询所有学生的Sname、Cname和Degree列。
select sname, cname,degree from student ,score , course where student.sno=score.sno and score.cno=course.cno;

–非等值连接
–查询每个员工的信息以及这个员工的薪资等级

select * from emp,salgrade where sal between losal and hisal;

–外链接

–做为主表的表中的数据全部显示
–在连接条件的位置,在主表对面的表的连接条件后添加(+)
– emp e1,emp e2 主表的位置确定,主表在左边叫做左连接,主表在右边,叫做右连接

select * from emp e1,emp e2 where e1.mgr=e2.empno(+); --左连接
select * from emp e2,emp e1 where e1.mgr=e2.empno(+); --右连接


											2019.07.23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值