同義詞 synonym
CREATE [PUBLIC]SYNONYM synonym For schema
隱藏對象的名稱和所有者:
select count(*) from hr
create synonym emp for hr
select count(*) from emp;
為分布式數據庫的遠程對象提供了位置透明性:
訪問其他數據庫時
CREATE DATABASE LINK test_link CONNECT TO username IDENTIFIED BY pass USING
Select count(*) from hr
create synonym link_emp for hr
select count(*) from link_emp;
提供對象的公共訪問:
create public synonym pub_emp for hr
pub_emp屬於public用戶
同義詞類型
–私有 emp 實際上donny
–公用 pub_emp 所有用戶都可以直接訪問
當公有對象和私有對象同名時(因為數據不同的用戶
desc dba_synonyms/ user_synonyms/ all_synonyms 數據字典
tab公有同義詞
建立私有的tab表
刪除同義詞
drop synonym donny
drop public synonym pub_emp;
序列sequence
CREATE SEQUENCE donny
INCREMENT BY
START WITH
NOMAXVALUE
NOMINVALUE
NOCYCLE
CACHE
訪問序列
oracle為序列提供了兩個偽列
Nextval: 根據increment by得到的一個新的序列值
Currval: current value
Select seq
Select seq
使用序列
insert into t values(seq
修改序列
alter sequence seq …
不能修改start with;除非刪除重建
刪除序列
drop sequence seq;
數據字典
desc dba_sequences / user_…/ all…
視圖view:
CREATE [OR REPLACE][FORCE/ NOFORCE] VIEW
Create view mytable
As
Select first_name||
from hr
[試驗]
a) 賦予所有員工訪問hr
b) 建立一個只包含合適字段的視圖
Conn hr/hr
Create view company_phone_book as
Select first_name||
From employees;
Grant select on company_phone_book to public;
Desc company_phone_book 對比列的長度
Select * from company_phone_book;
name隱藏數據的復雜性
數據字典
dba_views
text字段
select text from dba_views where view_name=upper(
改變視圖定義
新需求
Create view company_phone_book as
Select employee_id emp_id
first_name||
From employees;
報錯;
如果刪掉重建
Create or replace view company_phone_book as
Select employee_id emp_id
first_name||
From employees;
Desc company_phone_book
Drop view company_phone_book
視圖中增加約束
create view yearly_hire_totals as
select to_char(hire_date
count(*) total
from hr
group by to_char(hire_date
order by to_char(hire_date
聯接視圖
desc hr
set long
select text from dba_views where view_name=upper(
with read only
驗證視圖有效性
基本表的一些改變可能會導致視圖無效
[試驗]使視圖無效
insert into base values(
select id view_id
select * from view_b;
alter table base add(data
select * from view_b;
FORCE 選項
強制ORACLE接受無效的視圖定義
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(
嘗試更新email和name
update pany_phone_book
set name=
where emp_id=
create trigger update_name_company_phone_book
INSTEAD OF
Update on pany_phone_book
Begin
Update hr
Set employee_id=:new
First_name=substr(:new
last_name= substr(:new
phone_number=:new
email=:new
where employee_id=:old
end;
作用
試驗
create view department_
select * from hr
With check option
select * from dba_updatable_columns
where table_name=upper(
update department_
set department_id=
where employee_id=
報錯!!
這個視圖限制我們只能訪問department=
[試驗]關於前
select last_name
from hr
order by hire_date;
select last_name
from hr
where rownum<
order by hire_date;
結果不正確
from (select last_name
from hr
order by hire_date)
where rownum<
From:http://tw.wingwit.com/Article/program/Oracle/201311/18425.html