標籤:
Oracle學習系列7************************************************************************************ 關聯表的約束: 強制移除關聯表中的父表: drop table tab_name cascade constraint ; 約束本身是可以修改的,但是不建議修改約束 知識點: 1,掌握視圖的作用及定義 2,掌握序列的使用:SEQUENCE 3,掌握PowerDesigner設計工具的使用 4,瞭解同義字,瞭解使用者管理,瞭解巢狀表格及可變數組 5,理解資料庫的設計範式-------------------------------------------------------- 1,視圖: 封裝了一條複雜的查詢語句 文法: create view view_name as 子查詢 {with check option | with read only } ; //不能更新視圖的建立條件,不能更改視圖資料 建立一個視圖,此視圖包含了全部部門20的資訊: create view view_emp20 as select empno,ename, job,hiredate from emp where deptno=20 ; 查詢檢視: sele * from view_emp20 ; 刪除視圖: drop view view_name ; ex: drop view view_emp20 ; 完整文法格式: create or replace view view_name as 子查詢 //系統會為使用者自動進行刪除及重建的功能 ex: create or replace view view_emp20 as select d.dname, count(e.empno), avg(e.sal), avg(months_between(sysdate,e.hiredate)/12) years from emp e, dept d where e.deptno=d.deptno group by d.dname ; 更新視圖: 修改視圖中7369的部門編號: update view_emp20 set deptno=30 where empno=7369; //提示更新成功,但是視圖表中無編號7369的僱員--------------------------------------------------------序列(重點) 在oracle完成自動序列增長的功能,則只能依靠序列完成 格式: create sequence seq_name [increment by n] [start with n] [{maxvalue n |nomaxvalue}] [{minvalue n | nominvalue }] [{cycle | nocycle}] [{cache n | nocache }]; 刪除: drop sequence seq_name ex:建立一個myseq的序列,驗證自動成長的操作: create sequence myseq increment by 2; 在序列中提供兩種操作: nextVal:取得序列的下一個內容 currVal:取得序列的當前內容 建立表test_seq: create table test_seq( curr number, next number ); 使用序列: insert into test_seq(curr,next) values(myseq.currval,myseq.nextVal) ;--------------------------------------------------------同義字(瞭解): 功能:可以讓其他使用者通過一個名稱方便的訪問‘user.table_name’. 文法: create synonym syn_name for user.tab_name ; //建立 drop synonym syn_name ; //刪除 ex: create synonym dual for sys.dual ; ------------------------------------------ select sysdate from dual ;//dual是張虛擬表 conn sys/change_on_install as sysdba ; select * from tab where TNAME=‘DUAL‘;//在sys使用者下存在此表 --------------------------------------------------------使用者管理(瞭解): 文法: create user user_name identified by passwd //建立使用者(sys使用者權限) [default tablespace default_tablespace] [temporary tablespace temporary_tablespace ] grant 許可權1,許可權2,許可權3.. to user ;//給使用者授權 create session alter user user_name identified by passwd_new ; //修改使用者密碼 eg: create user kevin identified by root ; grant conncet, resource to kevin ; 角色:connect 、resource //每個角色有好多不同的許可權 解/鎖住使用者: alter user user_name account lock /unlock; eg: alter user kevin account lock /unlock ; 授權emp表: grant 許可權1,許可權2 on user_name.tabl_name to user_name ; 將scott使用者下的emp表的查詢許可權及刪除許可權給kevin: grant select ,delete on scott.emp to kevin; 回收許可權: revoke 許可權1,許可權2,.. on user.tab_name from user_name ; eg: revoke select ,delete on scott.emp from kevin ; grant/(revoke) 許可權1,許可權2,許可權3 on user.tab_name to / (from) user_name ; --------------------------------------------------------資料庫的備份與恢複(瞭解): Database Backup: exp D:\data>exp 資料庫恢複: imp D:\data>impf 查看錯誤:show error --------------------------------------------------------巢狀表格(瞭解): 在一個表中包含另外一個子表 //建立project_ty類型 create type project_ty as object( proid number(4), proname varchar(50), prodate date ) ; / //最後一個‘/‘ 不可少 //使用porject_nt類型 create type project_nt as table of project_ty; / //最後一個‘/‘ 不可少 create table department( deptno number(2) primary key not null, dname varchar2(50) not null, projects project_nt ) nested table projects store as project_nt_tab_temp ; 對於插入資料來講,需要指定每個project_ty的資料類型 insert into department(deptno,dname,projects) values( 1,‘技術部‘, project_nt( project_ty(1001,‘erp‘,sysdate), project_ty(1002,‘crm‘,sysdate), project_ty(1003,‘oa‘,sysdate), ) ); 查詢: select * from department ; 若此時需要查看一個部門的全部項目的話,則需要查詢巢狀表格: select * from table( select projects from department where deptno=1 ) ; 更新: update table (select projects from department where deptno=1 ) pro set values(pro )=project_ty(‘1001‘,‘測試專案‘,to_date(‘2016-02-12‘,‘yyyy-mm-dd‘)) where pro.proid=1001 ; --------------------------------------------------------可變數組(瞭解): 屬於巢狀表格的升級版,在可變數組中手機上就是將內部的巢狀表格的內容的長度進行了限制。 //定義類型 create type worker_info as object( id number, name varchar2(50), sex varchar2(6) ); / //定義數群組類型 create type worker_info_list as varray(10) of worker_info ; / //建立可變數組表 create table department( deptno number(2) primary key not null, dname varchar2(50) not null, workers worker_info_list ); //插入資料 insert into department(deptno,dname,workers) values( 20,‘後勤部‘, worker_info_list( worker_info(1,‘dustin‘,‘F‘), worker_info(2,‘kevin‘,‘F‘), worker_info(3,‘allen‘,‘M‘) ) );--------------------------------------------------------資料庫設計範式(瞭解): 1.第一範式(確保每列保持原子性) 2.第二範式(確保表中的每列都和主鍵相關) 3.第三範式(確保每列都和主鍵列直接相關,而不是間接相關) --------------------------------------------------------資料庫設計工具(重點): powerDesigner工具的使用
Oracle學習系列7