Oracle数据库控制台操作3

-- 常用操作
--连接符 连接hellow world
select concat('hello','world') from dual;

--连接字符2
select ename||'的薪水是'||sal from emp;

--2、where 过滤条件
--查询部门编号为10的员工
select * from emp where deptno=10;

--3、字符和日期要包含在单引号中,字符的大小写敏感,日期格式敏感
select * from emp where ename='King';
--是查不出名字为KING的员工的
--需要写成select * from emp where ename='KING';
--查询入职时间为17-11月-81的员工,默认的时间格式为DD-MM-YY
select * from emp where hiredate='17-11月-81';
--但是日期格式改为我们认识的1981-11-17是查询不了的
select * from emp where hiredate='1981-11-17';
--修改本次会话的日期格式
alter session set nls_date_format='YYYY-MM-DD';
select * from emp where hiredate='1981-11-17';就可查询出结果了


--4、比较运算符
= 等于
> 大于
< 小于
>= 大于等于
<= 小于等于
<> 不等于也可以是!=
between .. and .. 在两个值之间,包含边界
--查询工资在800-2450的员工
select * from emp where sal between 800 and 2450;
--in 等于列表中的一个值
--not in 不在列表中的值
--查询部门编号10和20下的所有员工
select * from emp where deptno in(10,20);
--like 模糊查询
--查询员工姓名为S开头的
select * from emp where ename like 'S%';
--查询运功姓名是4个字的员工,一个下划线代表一位
select * from emp where ename like '____';
--插入一条语句
insert into emp(empno,ename,sal,deptno) values(10086,'联_通',3000,10);
--查询姓名带下划线的所有员工,这样是查询不出的
select * from emp where ename like '%_%';
--设置转义字符就可以了
select * from emp where ename like '%\_%' escape '\';
--注意:oracle中的事务是手动开启的,如果现在输入rollback;事务会回滚,要插入的数据保存需要手动commit;
--is null 是空值
--is not null 是非空

--5、逻辑运算符
and
or
not

6、排序
order by  
asc:升序
desc:降序
order by 语句在select语句的结尾,默认是升序
查询员工号,按照月薪排序
select * from emp order by sal;
order by后可以跟 列名,表达式,别名,序号
根据列名排序
select ename,sal,sal*12 as money from emp order by sal;
根据别名排序
select ename,sal,sal*12 as money from emp order by money desc;
根据序号排序,3指的是第3个列,只能写有的列
select ename,sal,sal*12 as money from emp order by 3 desc;
写多个排序条件,desc只能作用于离它最近的列
select * from emp order by deptno desc,sal desc;
null值的排序
按照奖金升序排列,null值排在后面,是正常的
select * from emp order by comm;
按照奖金降序排列,oracle中,null值最大,null值排在前面了,需要将null值排在最后
select * from emp order by comm desc nulls last;


二、子查询
子查询要少用
子查询解决不能一步到位的查询
1、查询工资比SCOTT高的员工
SCOTT的工资
select sal from emp where ename='SCOTT'
比SCOTT的工资3000高的人
select ename,sal from emp where sal>3000;
使用子查询
select * from emp where sal>(select sal from emp where ename='SCOTT');

2、子查询可以放在列上
select ename,sal,(select job from emp where empno=7839) job from emp;

3、主查询和子查询可以不是同一张表;只要子查询返回的结果主查询可以使用即可
查询部门名称是SALES的员工信息
select * from emp where deptno=(select deptno from dept where dname='SALES');

三、oracle中的伪列
1、rowid
rowid是物理存在的,唯一的,但是其值并未存储在表中,所以不支持增删改操作
select rowid,emp.* from emp;
创建class表,用于模拟重复数据
create table class(
classid number(4),
cname varchar2(20)
);
利用rowid删除重复行数据
 分析
 * 找到重复的数据
 方法1:select distinct * from class;
 方法2:select * from class group by classid,cname having count(*)>1;
 只保留一个重复的就可以了,利用rowid的唯一性,将条件改为>=1,保留没有重复的
 select max(rowid) from class group by classid,cname having count(*)>=1;
 使用子查询,删除重复的数据
 delete from class where rowid not in(select max(rowid) from class group by classid,cname having count(*)>=1);


2、rownum
rownum伪列是Oracle进行查询获取到结果集之后在加上去的一个伪列,
这个伪列对符合条件的结果添加一个从1开始的序列号
select rownum,ename,job,sal from emp;
rownum永远按照默认的顺序生成
select rownum,ename,job,sal from emp order by sal;
如果要按照排序后的效果重新生成rownum,必须使用子查询
select rownum,ename,job,sal from(select * from emp order by sal);

3、利用rownum进行分页查询
oracle中没有top命令,也没有limit命令,分页查询可以通过rownum来实现
rownum不能直接写> >=的条件 >=1是特例,只能使用< <=的条件
select * from emp where rownum>1;
所以不能使用这种来进行分页
select * from emp where rownum<=5 and rownum>=1;这句可以正常返回
但是下一页的数据就取不到了,我们以每页显示5条,取第二页的数据为例
select * from emp where rownum<=10 and rownum>=6
利用子查询,重新给查询的结果来进行生成rowid,考虑到可能会使用到排序
select rownum,ename,sal from emp order by sal;
所以,里面不能直接使用rownum来限制了,变为:
select rownum,e.* from(select * from emp) e where rownum<=10;
这样就设置了结果集的结束位置
rownum不能直接写大于和大于等于条件,但是可以给rownum取别名,将rownum变为一列数据
select rownum r,e.* from (select * from emp) e where rownum<=10;
但是查询出来的r在这里不能作为where条件,所以需要再次套一层子查询,形成最终的分页查询语句
最终的分页查询SQL:
select * from
 (select rownum r,e.* from 
  (select * from emp) e where rownum<=10) where r>=6;
以后只要改起始的记录数和结束的记录数
如果有排序,有where条件,记得加在最里层的select语句后面

四、索引
大量数据的查询优化,第一选择—索引
单列索引:基于单个列建立的索引,如:
create index 索引名 on 表名(列名);
复合索引:基于两列或多列的索引,在同一张表上可以有多个索引,但是要求列的组合必须不同,如:
create index emp_index1 on emp(ename,job);
create index emp_index1 on emp(job,ename);

1、索引使用原则:
(1)在大表上建立索引才有意义
(2)在where子句或是连接条件上经常使用的列上建立索引
(3)索引的层次不超过4层
2、索引缺点:
(1)建立索引,系统要占用大约为表1.2倍硬盘和内存空间来保存索引。
(2)更新数据时,系统必须要额外的时间来同时对索引的更新,以保持数据和索引的一致性。
实践表明:不恰当的索引不但对性能没有优化,反而会降低系统的性能。
因为大量的索引在进行插入、修改和删除时比没有索引花费更多的系统时间。
在很少或从不引用的字段上或逻辑型的字段(如男或女、是或否)等列上建立索引是不恰当的。
发布了62 篇原创文章 · 获赞 6 · 访问量 2598

猜你喜欢

转载自blog.csdn.net/qq_44424498/article/details/100120927