这两种都用来:
- 精简语句
- 隐藏细节
同义词(Synonym)
为 表或对象 创建别名:
- 可以简化 sql 语句
- 可以隐藏细节
- 提供公共访问(公有同义词)
-- 查看跟同义词有关的权限
-- 分两种:
--- synonym, 创建来自己使用
--- public synonym, 创建来所有人都可以使用
select * from system_privilege_map where name like upper('%syn%');
-- 将创建同义词的权限授权给用户
grant create synonym to vip;
grant create public synonym to vip;
-- 创建
create synonym e for scott.emp;
create or replace public synonym d for scott.dept;
-- 可以为表之外的对象创建同义词
create sequence seq_aaaaaaforme;
create synonym seq_aaa for seq_aaaaaaforme;
-- 使用,在一切可以使用原先对象的地方
select * from e join d using (deptno);
desc d;
select seq_aaa.nextval from dual;
-- 删除
drop synonym d;
drop public synonym d;
7.2 视图(View)
视图, 默认指的是关系视图, 又叫虚表。
- 不占用数据空间
- 可以简化语句
- 可以隐藏细节
- 可以提升安全
除了关系视图,广义的视图包括:
- 关系视图(狭义的视图,虚表)
- 内嵌视图(子查询中的临时结果)
- 对象视图(面向对象)
- 物化视图(以空间换时间)
-- 查询跟视图相关的权限
select * from system_privilege_map where name like '%VIEW%';
-- 确保用户有足够权限
grant create view to vip;
grant all on scott.emp to vip;
grant all on scott.dept to vip;
-- 创建视图
create view vemp as select * from scott.emp; -- 最简形式,为单个表创建视图
create view v_emp_vip as select * from scott.emp where sal >= 2000; -- 可以为表的部分数据创建视图
create view vvevip as select * from vevip; -- 可以为视图查询创建视图
create or replace view vevip as -- 可以为多表联合查询的结果集创建视图。
select e.*, d.dname, d.loc
from emp e, dept d
where e.deptno = d.deptno(+)
and e.sal >= 2000;
-- 使用
select * from e;
select * from vemp;
select * from v_emp_vip;
select * from vevip;
select * from vvevip;
-- 删除
drop view vemp;
-- force 强迫!!!不管 select 语句是否有错,都要创建。
-- or replace, 如果视图已存在,覆盖
-- with read only, 创建只读视图
create or replace force view vaaa as select * from wohaoshuai with read only;
-- with check option, 防止更新后的数据, 超出视图的范围
insert into emp (empno, ename, sal) values (9988, '张思', 3333);
create or replace view vbbb as select * from emp where sal > 3332;
select * from vbbb; -- 两条数据,King 和 张思
update vbbb set sal = 2000 where empno = 9988;
select * from vbbb; -- 只剩一条数据。
-- 按照道理,update 只是更新数据,不应该导致结果集变少。所以,需要通过 with check option 来限制不合理的修改
create or replace view vcc as select * from emp where sal > 1500 with check option;
update vbbb set sal = 1000 where empno = 9988; -- 会报错