一、掌握oracle数据库的安装
二、Oracle中常用数据类型 字符类型 数值类型 日期类型 LOB类型
三、Oracle伪列包含 ROWNUM、ROWID
四、SQL语言分为DDL、DCL、TCL和DML
五、SQL函数分为 单行函数 分组函数 分析函数
六、mysql和oracle的不同?
七、
1、分页查询 rownum
查询中间数据
--分页查询,查询中间数据select * from(select e.*,rownum rn from emp e) e1where e1.rn>=2 and e1.rn<=4;--查询员工表中的薪水,由高到低的5-9条数据select * from (select e1.*,rownum rn from(select * from emp e0 order by e0.sal desc) e1 )e2where e2.rn>=5 and e2.rn<=9;select * from stuinfo s;
2、去重
--不重复显示学员姓名和年龄distinctselect distinct stuname,stuage from stuinfo;
3、排序
--按照姓名升序,姓名相同按年龄降序排序select * from stuinfo order by stuname asc ,stuage desc;
4、取别名
--起别名 中文或者特殊字符加双引号select stuname "姓名",stuage"年 龄" from stuinfo;
5、创建新表—3种方法
--创建新表1 复制表的结构和记录create table stuinfo1 as select * from stuinfo;select * from stuinfo1;drop table stuinfo1;--创建新表2 用选定的列创建新表create table stuinfo2 as select stuname , stuage from stuinfo;select * from stuinfo2;drop table stuinfo2;--创建新表3 只复制结构不复制记录create table stuinfo3 as select * from stuinfo where 1=2;select * from stuinfo3;drop table stuinfo3;
6、不重复或重复取一条数据
--按照姓名和年龄分组,找出不存在重复的记录select stuname,stuage,max(stuno)from stuinfo group by stuname,stuagehaving count(stuname||stuage)=1;--重复的姓名和年龄,只保留一条数据select s.*,rowid from stuinfo s;delete from stuinfo where rowid not in(select max(rowid) from stuinfogroup by stuname,stuage);
7、连接符
--连接操作符 ||看成字符串连接的+--查询emp表,查询两个字段并连接在一起select e.job||'_'||e.enamefrom emp e;
8、联合 union
--创建表,emp-->表emp42create table emp42 as select * from emp;select * from emp42;update emp42 set empno=8934 where empno=7934;--联合 union 重复的只显示一次,不重复的全显示select * from empunionselect * from emp42;
9、联合所有 union all
--联合所有 union all 无论是否重复,2 个表的数据全部显示select * from empunion allselect * from emp42;
10、交集 intersect
--交集 intersect 显示公共的部分select * from empintersectselect * from emp42;
11、减集 minus
--减集 minus 显示第一张表-第二张表不重复的数据select * from empminusselect * from emp42;--只显示两表不同的部分,先联合所有,再去重
12、函数
(1)、日期函数 to_date
--日期函数 字符串转日期select to_date('2021-4-6','yyyy-mm-dd') from dual;select 3+2 from dual;--sysdate 返回当前时间select sysdate from dual;
(2)、字符函数 to_char
--字符函数 将日期转字符串select to_char(sysdate,'yyyy-mm-dd HH24:mi:ss') from dual;
(3)、数字函数 to_number
--数字函数 将字符串转数字 sqrt()开平方select sqrt( to_number('25')) from dual;
(4)、滤空函数
--滤空函数 过滤空值 --为空就是+0 --不为空就是工作+奖金,为空即是基本工资select ename,sal+nvl(comm,0) sal1,nvl2(comm,sal+comm,sal) sal2,decode(to_char(hiredate,'MM'),'01','一月','02','二月','03','三月','04','四月','05','五月','06','六月','下半年')from emp;
(5)、分析函数
—rank 1-1-3 并列间隔排名
—dense_rank 1-1-2 并列无间隔
—row_number 1-2-3 无并列无间隔
--分析函数 相当于分组group by 排名时能用 partition 按照什么进行分组--rank 1-1-3 并列间隔排名--dense_rank 1-1-2 并列无间隔--row_number 1-2-3 无并列无间隔select ename,deptno,sal,rank() over(partition by deptno order by sal desc) "rank",dense_rank() over(partition by deptno order by sal desc) "dense_rank",row_number() over(partition by deptno order by sal desc) "rn"from emp;
select e.*,rownum,rowid from emp e;--分页查询,查询中间数据select * from(select e.*,rownum rn from emp e) e1where e1.rn>=2 and e1.rn<=4;--查询员工表中的薪水,由高到低的5-9条数据select * from (select e1.*,rownum rn from(select * from emp e0 order by e0.sal desc) e1 )e2where e2.rn>=5 and e2.rn<=9;select * from stuinfo s;--不重复显示学员姓名和年龄distinctselect distinct stuname,stuage from stuinfo;--按照姓名升序,姓名相同按年龄降序排序select * from stuinfo order by stuname asc ,stuage desc;--起别名 中文或者特殊字符加双引号select stuname "姓名",stuage"年 龄" from stuinfo;--创建新表1 复制表的结构和记录create table stuinfo1 as select * from stuinfo;select * from stuinfo1;drop table stuinfo1;--创建新表2 用选定的列创建新表create table stuinfo2 as select stuname , stuage from stuinfo;select * from stuinfo2;drop table stuinfo2;--创建新表3 只复制结构不复制记录create table stuinfo3 as select * from stuinfo where 1=2;select * from stuinfo3;drop table stuinfo3;--按照姓名和年龄分组,找出不存在重复的记录select stuname,stuage,max(stuno)from stuinfo group by stuname,stuagehaving count(stuname||stuage)=1;--重复的姓名和年龄,只保留一条数据select s.*,rowid from stuinfo s;delete from stuinfo where rowid not in(select max(rowid) from stuinfogroup by stuname,stuage);--连接操作符 ||看成字符串连接的+--查询emp表,查询两个字段并连接在一起select e.job||'_'||e.enamefrom emp e;--创建表,emp-->表emp42create table emp42 as select * from emp;select * from emp42;update emp42 set empno=8934 where empno=7934;--联合 union 重复的只显示一次,不重复的全显示select * from empunionselect * from emp42;--联合所有 union all 无论是否重复,2 个表的数据全部显示select * from empunion allselect * from emp42;--交集 intersect 显示公共的部分select * from empintersectselect * from emp42;--减集 minus 显示第一张表-第二张表不重复的数据select * from empminusselect * from emp42;--只显示两表不同的部分,先联合所有,再去重--函数--日期函数 字符串转日期select to_date('2021-4-6','yyyy-mm-dd') from dual;select 3+2 from dual;--sysdate 返回当前时间select sysdate from dual;--字符函数 将日期转字符串select to_char(sysdate,'yyyy-mm-dd HH24:mi:ss') from dual;--数字函数 将字符串转数字 sqrt()开平方select sqrt( to_number('25')) from dual;--滤空函数 过滤空值 --为空就是+0 --不为空就是工作+奖金,为空即是基本工资select ename,sal+nvl(comm,0) sal1,nvl2(comm,sal+comm,sal) sal2,decode(to_char(hiredate,'MM'),'01','一月','02','二月','03','三月','04','四月','05','五月','06','六月','下半年')from emp;--分析函数 相当于分组group by 排名时能用 partition 按照什么进行分组--rank 1-1-3 并列间隔排名--dense_rank 1-1-2 并列无间隔--row_number 1-2-3 无并列无间隔select ename,deptno,sal,rank() over(partition by deptno order by sal desc) "rank",dense_rank() over(partition by deptno order by sal desc) "dense_rank",row_number() over(partition by deptno order by sal desc) "rn"from emp;
