一. 视图
-
在oracle中单引号表示字符串, 在别名定义时需要用到双引号,别名是纯数字必须使用双引号
-
什么是视图
是一个虚表, 可以对视图进行增删改查
视图是不能存储数据的,所有的数据都在基本表(table)中
操作视图就是操作基本表
视图简单的说,就是封装了一个[复杂]的DQL;
对视图操作,只能操作哪些查询出来的列, 一般不作为修改 -
视图的语法
create [or replace] view 视图名称 as DQL; -
视图的使用
– create [or replace] view 视图名称 as DQL;
– 创建视图
create or replace view emp_view as select * from emp;
– 查询视图
select * from emp_view;
– 修改(增删改)视图
insert into emp_view(empno ,ename) values(1004, ‘laowang’);
– 查询原表
select * from emp; -
视图的特点
– 可以屏蔽敏感列
create or replace view emp_view as
select empno,ename,job,mgr,hiredate ,deptno from emp;
select * from emp_view;
– 可以创建一个只读的视图
create or replace view emp_view as
select empno,ename,job,mgr,hiredate ,deptno from emp with read only;
insert into emp_view(empno ,ename) values(1004, ‘laowang’);
二. 索引
-
索引的作用
效率高,提高检索效率
前提:
经常查询的列
百万条记录以上
不经常修改的列(修改列会导致索引重建)
主键列和唯一键列为索引列 -
索引的创建语法
create index 索引名称 on 表(列,…) -
测试效率
–1. 创建表
create table a(
id number primary key ,
text varchar2(200)
)
create sequence a_seq;
–2. 准备百万条记录
select sys_guid() from dual;declare
begin
for i in 1…1000000 loop
insert into a values(a_seq.nextval , sys_guid());
end loop;
end ;
–3. 在创建索引前查询 – 0.547
select * from a where text = ‘5F39F437DAB04E76A43DA83D0FDEC169’;
–4. 创建索引
create index a_text_index on a(text);
–5. 在创建索引后查询 – 0.032
select * from a where text = ‘286A6E4C385345A88BD5018093A9F3B4’; -
复合索引(多列索引):触发条件 – (address ,name) 了解
select * from 表 where address = ‘’ and name = ‘’ 触发索引
select * from 表 where address = ‘’ or name = ‘’ 不会触发索引
select * from 表 where address = ‘’ 触发索引
select * from 表 where name = ‘’ 不会触发索引