数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

Oracle中的概念:同义词、序列与视图


发布日期:2020年02月15日
 
Oracle中的概念:同义词、序列与视图

同义词 synonym

CREATE [PUBLIC]SYNONYM synonym For schemaobject

隐藏对象的名称和所有者:

select count(*) from hremployees;

create synonym emp for hremployees; 默认属于donny用户是donny的私有对象private

select count(*) from emp;

为分布式数据库的远程对象提供了位置透明性:

访问其他数据库时要首先建立数据库连结

CREATE DATABASE LINK test_link CONNECT TO username IDENTIFIED BY pass USING orabase;

Select count(*) from hremployees@test_link;

create synonym link_emp for hremployees@test_link;

select count(*) from link_emp;

提供对象的公共访问:

create public synonym pub_emp for hremployees;

pub_emp属于public用户数据库所有用户都可以访问

同义词类型

–私有 emp 实际上donnyemp

–公用 pub_emp 所有用户都可以直接访问

当公有对象和私有对象同名时(因为数据不同的用户所以可以)以私有对象优先(类似于局部变量)

desc dba_synonyms/ user_synonyms/ all_synonyms 数据字典复数

tab公有同义词

建立私有的tab表查看效果

删除同义词

drop synonym donnyemp;

drop public synonym pub_emp;

序列sequence

CREATE SEQUENCE donnyseq也是属于某个用户的以下参数均可省略使用默认值

INCREMENT BY 指定序列之间的间隔正负整数;默认正为升序负为降序

START WITH 第一个序列号默认=MINVALUE

NOMAXVALUE 设置最大值此处表示默认次幂MAXVALUE

NOMINVALUE 设置最小值此处表示默认次幂MINVALUE

NOCYCLE 或者CYCLE;表示序列达到最大或者最小(降序)后要不要从头开始

CACHE ; 默认CACHE 事先分配多少序列号放在内存中提高速度

访问序列

oracle为序列提供了两个伪列可以看作其属性

Nextval: 根据increment by得到的一个新的序列值每次执行都会得到一个新值

Currval: current value当前值已经被取得的值

Select seqnextval from dual;

Select seqcurrval from dual;

使用序列

insert into t values(seqnextval);

修改序列

alter sequence seq …重新指定各个参数

不能修改start with;除非删除重建

删除序列

drop sequence seq;

数据字典

desc dba_sequences / user_…/ all…

视图view:

CREATE [OR REPLACE][FORCE/ NOFORCE] VIEW AS

Create view mytable

As

Select first_name||||last_name

from hremployees;

[试验]如何使用视图作为安全  target=_blank>安全机制

desc考察hremployees看作一个公司的员工信息数据库表简单说明

目标实现每个员工都可以访问公司中所有雇员的name email phone_number方便通讯

方案

a) 赋予所有员工访问hremployees表的权限?salary

b) 建立一个只包含合适字段的视图然后赋予所有员工访问这个视图的权限而不是表的权限

Alter user hr account unlock;

Conn hr/hr

Create view company_phone_book as

Select first_name|| ||last_name name email phone_number

From employees;

Grant select on company_phone_book to public;

Desc company_phone_book 对比列的长度

Select * from company_phone_book;

name隐藏数据的复杂性

数据字典

dba_views

text字段long

select text from dba_views where view_name=upper(company_phone_book)

改变视图定义

新需求想要在现有视图上增加员工的ID号(employee_id)

Create view company_phone_book as

Select employee_id emp_id

first_name||||last_name name email phone_number

From employees;

报错;

如果删掉重建会有什么缺点?会把关联的授权全部删掉Create or replace view保留原有授权

Create or replace view company_phone_book as

Select employee_id emp_id

first_name||||last_name name email phone_number

From employees;

Desc company_phone_book

Drop view company_phone_book

视图中增加约束

create view yearly_hire_totals as

select to_char(hire_dateYYYY) year

count(*) total

from hremployees

group by to_char(hire_dateYYYY)

order by to_char(hire_dateYYYY);

联接视图

desc hremp_details_view

set long

select text from dba_views where view_name=upper(emp_details_view)

with read only

验证视图有效性

基本表的一些改变可能会导致视图无效

) 改变出现在视图中列的名称或删掉列

) 删除构建视图的基本表或视图

[试验]使视图无效并重新编译并使其有效

) 基本表create table base(id numberdata varchar());

insert into base values(abc); commit;

) view: create view view_b as

select id view_id data view_data from t;

select * from view_b;

) 更新基本表 alter table base modify(id numberdata varchar());

alter table base add(data varchar());

) 视图无效select object_name status from dba_objects where object_name=upper(view_b)

) 使视图有效只需要从视图中选取即可oracle会自动对视图编译

select * from view_b;

) 手动编译alter view view_b compile;

FORCE 选项

强制ORACLE接受无效的视图定义

) 比如开发过程中A负责建立基本表B负责建立视图这样B不必依赖于A的工作进度就可以将视图建立并编译进数据库

) 或者B需要建立在A用户表上视图但是还暂时没有对A用户表select 的权限可以先建立等待授权后再使用

Create view invalid_view as

Select * from table_not_exist;

Create force view invalid_view as

Select * from table_not_exist;

通过视图进行更新和删除

类似于company_phone_book是可以跟新的

可以通过dba_updatable_columns查看那些列可以做那些更新;

desc pany_phone_book

select * from dba_updatable_columns where table_name=upper(company_phone_book)

尝试更新email和name

update pany_phone_book

set name=Chen Donny

where emp_id=

使用instead of 触发器更新视图

create trigger update_name_company_phone_book

INSTEAD OF

Update on pany_phone_book

Begin

Update hremployees

Set employee_id=:newemp_id

First_name=substr(:newname instr(:newname)+)

last_name= substr(:newnameinstr(:newname))

phone_number=:newphone_number

email=:newemail

where employee_id=:oldemp_id;

end;

With check option 约束

作用阻止更新不能通过视图访问的数据

试验

) 建立视图只能看到department_id=的雇员

create view department_ as

select * from hremployees where department_id=

With check option

) 选择select employee_idfirst_namelast_name from department_;

) 查看可更新列

select * from dba_updatable_columns

where table_name=upper(department_)

) 尝试将此人移动到部门

update department_

set department_id=

where employee_id=

报错!!

这个视图限制我们只能访问department=的数据我们要通过视图修改department=的数据被禁止

[试验]关于前

) 谁是公司前名的雇员

select last_namehire_date

from hremployees

order by hire_date;

) 只想取回前五名数据呢?

select last_namehire_date

from hremployees

where rownum<

order by hire_date;

结果不正确先取了前条数据再排序

)select last_namehire_date

from (select last_namehire_date

from hremployees

order by hire_date)

where rownum<

               

上一篇:oracle9i中Rman的备份使用点滴

下一篇:oracle建立的分区表数据问题