ďť??xml version="1.0" encoding="utf-8" standalone="yes"?>www.中文字幕久久久,欧美激情www,另类av一区二区http://www.aygfsteel.com/kevinfriend/zh-cnSat, 17 May 2025 10:39:40 GMTSat, 17 May 2025 10:39:40 GMT60Manager Userhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/12/74716.htmlĺşäh–‡ĺşäh–‡Thu, 12 Oct 2006 01:49:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/12/74716.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74716.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/12/74716.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74716.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74716.htmlcreate user test1 identified by test1;
grant connect,create table to test1;
conn cyts_cc/cyts_cc@orcl2000;
create table(
id int)
tablespace user;
ERROR 位于˝W?1 čĄ?
ORA-01950: 襨犺é—?USERS'中无权限
conn cyts_cc/cyts_cc@orcl2000;
alter user test1 quota 1M on users;
create tab
(id int);
success
alter user test1 account lock;
conn test1/test1@orcl2000;
ERROR:
ORA-28000: the account is locked
1 Database Schema
 (t¨Šng)a schema is a named collection of objects
 (t¨Šng)b user is created and a corresponding schema is created
 (t¨Šng)c user can be associated only with one schema
 (t¨Šng)d username and schema are often userd interchangely.
2 Checklist for creating users
 (t¨Šng) a idntfigy tablespaces in which the usr nedds to store objects
 (t¨Šng) b decide on quotas for each tablespace
 (t¨Šng) c assign a default tablespace and temporary tablespace.if you do not specify at the time of create user,system tablespace will be the defalut tablespace and temporary.it will affect the performance of the oralce.
 (t¨Šng) d create user
 (t¨Šng) e grant privileges and roles to user
 (t¨Šng) desc dba_users;
 (t¨Šng) select * from dba_users;
3 Creating a new user:
Database Authentiacation
 (t¨Šng)set the initial password
 (t¨Šng) create user aaron
 (t¨Šng) identified by soccer
 (t¨Šng) default tablespace data
 (t¨Šng) temporary tablespace temp
 (t¨Šng) guota 15m on data
 (t¨Šng) password expire;

 (t¨Šng) alter database default temporary tablespace temp;
4 Creating a new user operating System Authentication
 (t¨Šng) os_authent_prefix initialllization parameter specifies the format of the username
 (t¨Šng) defauts to ops$
 (t¨Šng) (t¨Šng) (t¨Šng) create user arron
 (t¨Šng) (t¨Šng) (t¨Šng) identified externally
 (t¨Šng) (t¨Šng) (t¨Šng) default tablespace users
 (t¨Šng) (t¨Šng) (t¨Šng) temporary tablespace temp
 (t¨Šng) (t¨Šng) (t¨Šng) quota 15m on data
 (t¨Šng) (t¨Šng) (t¨Šng) password expire;

 (t¨Šng) (t¨Šng) (t¨Šng) conn /
 (t¨Šng) (t¨Šng) (t¨Šng) show parameter os
 (t¨Šng) (t¨Šng) (t¨Šng) os_authent_prefix (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) string (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) OPS$
 (t¨Šng) (t¨Šng) (t¨Šng) create user ops$test3
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) identified externally
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) default tablespace us
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) temporary tablespace
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) quota 10m on users
 (t¨Šng) (t¨Šng) (t¨Šng) thee test2 is an user of os ,which the oracle is installed.
5 Changing user quota on tablespace
 (t¨Šng)alter user test3 quota 4m on users;
 (t¨Šng)you cann't grant quota on temp and undotbs.
 (t¨Šng)
 (t¨Šng)alter quota 0 on uers -- means that no new table space can be allocated and cannot change the exist object in the tablespaces
6 drop user
 (t¨Šng)you cannot drop user who has connected to oracle
 (t¨Šng) drop user (cascade)
7 Obtaining User information
 (t¨Šng)information about uers can be obtained by qerying the data dictionary
 (t¨Šng) dba_users
 (t¨Šng) 名称 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
-----------------------------
USERNAME (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
USER_ID (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
PASSWORD (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
ACCOUNT_STATUS (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
LOCK_DATE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
EXPIRY_DATE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
DEFAULT_TABLESPACE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
TEMPORARY_TABLESPACE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
CREATED (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
PROFILE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
INITIAL_RSRC_CONSUMER_GROUP (t¨Šng) (t¨Šng) (t¨Šng)
EXTERNAL_NAME (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) dba_ts_quotas
 (t¨Šng) (t¨Šng) 名称 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng)---------------
 (t¨Šng)TABLESPACE_NAME
 (t¨Šng)USERNAME (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng)BYTES (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng)MAX_BYTES (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng)BLOCKS (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng)MAX_BLOCKS (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)



]]>
Using SET Operators.http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74565.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:51:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74565.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74565.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74565.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74565.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74565.html1 The Union Operator
 (t¨Šng)The union operator returns results form both queries after eliminating duplications.\
 (t¨Šng)select employee_id,job_id
 (t¨Šng)from employees
 (t¨Šng)uniion
 (t¨Šng)select employ_id ,job_id
 (t¨Šng)from job_history;
2 the all operator
 (t¨Šng)The union all opertor reutrn result from both queries,including all duplications
3 interset
 (t¨Šng)select ster_id,qty from sales where qty>20;
 (t¨Šng)intersect
 (t¨Šng)select ster_id,qty from sales where ster_id like '7%';
4 minus
 (t¨Šng)select ster_id,qty from sales where qty>20
 (t¨Šng)minus
 (t¨Šng)select ster_id from sales where ster_id like '7%'

5 set operator guidelines
 (t¨Šng). teh expressions in the select list must match in number and data type;
 (t¨Šng). Parentheses can be used to alter the sequence of the execution
 (t¨Šng).The order by clause:
 (t¨Šng) (t¨Šng) (t¨Šng) can appear only at the very end of the statement
 (t¨Šng) (t¨Šng) (t¨Šng) will accept the column name,aliases from thee firest select statement ,or thee positional notation
 (t¨Šng).Duplicate row are atuomatically eliminated except in union all.
 (t¨Šng).Column names from the first query appear in the result
 (t¨Šng).The output is sorted in ascending order by default except in union all
6 matching the select statement
 (t¨Šng)select department_id,to_number(null),location,hire_date
 (t¨Šng)from employees
 (t¨Šng)union
 (t¨Šng)select department_id,location_id,to_date(null)
 (t¨Šng)from departments;

 (t¨Šng)select employee_id,job_id,salary
 (t¨Šng)from employees
 (t¨Šng)union
 (t¨Šng)select employee_id,job_id,0
 (t¨Šng)from job_history;
7 Controlling the order of the rows
 (t¨Šng)select 'sing' as "my dream" ,3,a_dummy
 (t¨Šng)from dual
 (t¨Šng)union
 (t¨Šng)select 'like''d like to teach',1
 (t¨Šng)from dual
 (t¨Šng)union
 (t¨Šng)select 'the world to',2
 (t¨Šng)from dual
 (t¨Šng)order by 2;



]]>
开发子˜q‡ç¨‹http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74564.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:51:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74564.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74564.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74564.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74564.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74564.html (t¨Šng)1 (t¨Šng) 开发过˝E?br /> (t¨Šng)1) 建立˜q‡ç¨‹ĺQšä¸ĺ¸Śĺ‚ć•?br /> (t¨Šng) create or replace procedure out_time
 (t¨Šng) is
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(systimestamp);
 (t¨Šng) end;
 (t¨Šng) a 使用execute 命ä×o(h¨´)调用˜q‡ç¨‹
 (t¨Šng) (t¨Šng) set (t¨Šng) serveroutput on
 (t¨Šng) (t¨Šng) exec out_time;
 (t¨Šng) b 使用call 命ä×o(h¨´)调用˜q‡ç¨‹
 (t¨Šng) (t¨Šng) set serveroutput on
 (t¨Šng) (t¨Šng) call out_time();
 (t¨Šng)2) 建立˜q‡ç¨‹ĺQšĺ¸Śćœ‰in参数 靘莤ä¸ř™ž“ĺ…Ľĺ‚ć•ŽÍźŒĺŚĺ¤–äšŸĺŻäťĽä‹É用in 关键子显˝Cşçš„厚䚉
 (t¨Šng) (t¨Šng) create or replace procedure add_employee
 (t¨Šng) (t¨Šng) (eno number,name varchar2,sal number,
 (t¨Šng) (t¨Šng) (t¨Šng) job varchar default 'clerk',dno number)
 (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) e_integrity exception;
 (t¨Šng) (t¨Šng) pragma exception_init(e_intgegrity,-2291);
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) insert into emp(empno,ename,sal,job.deptno)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) values(eno.name,sal,job,dno);
 (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when dup_val_on_index then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20000,'雇员号不能重ĺ¤?);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when e_integrity then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20001,'部门号不存在');
 (t¨Šng) (t¨Šng) (t¨Šng)
调用
 (t¨Šng) (t¨Šng) exec add_employee(111,'clark',200,'manager',10);
 (t¨Šng) (t¨Šng) 3 建立˜q‡ç¨‹ĺQŒĺ¸Śćœ‰out 参数
 (t¨Šng) (t¨Šng) (t¨Šng) create or replcace procedure query_employee
 (t¨Šng) (t¨Šng) (t¨Šng) (eno number,name out varchar2,salary out number)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select ename,sal,into name,salary from emp where empno=eno;
 (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when no_data_found then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20000,'G该雇员ä(sh¨´)¸ĺ­˜ĺœ¨');
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) 调用
 (t¨Šng) (t¨Šng) (t¨Šng) var name varchar2(10)
 (t¨Šng) (t¨Šng) (t¨Šng) var salary number
 (t¨Šng) (t¨Šng) (t¨Šng) exec query_employee(7788,:name,:salary);
 (t¨Šng) (t¨Šng) (t¨Šng) print name,salary;
 (t¨Šng) (t¨Šng) (t¨Šng) 4 建立˜q‡ç¨‹ĺQŒĺ¸Śćœ‰in out 参数
 (t¨Šng) (t¨Šng) (t¨Šng) create or replace procedure compute
 (t¨Šng) (t¨Šng) (t¨Šng) (num1,in out number,num2 in out number)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v1 number;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v2 number;
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v1:=num1/num2;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v2:=mod(num1,num2);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) num1:=v1;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) num2:=v2;
 (t¨Šng) (t¨Šng) (t¨Šng) end; (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) 调用
 (t¨Šng) (t¨Šng) (t¨Šng) var n1 number
 (t¨Šng) (t¨Šng) (t¨Šng) var n2 number
 (t¨Šng) (t¨Šng) (t¨Šng) exec :n1:=100;
 (t¨Šng) (t¨Šng) (t¨Šng) exec :n2:=30
 (t¨Šng) (t¨Šng) (t¨Šng) exec compute(:n1,:n2)
 (t¨Šng) (t¨Šng) (t¨Šng) print n1 n2
 (t¨Šng) (t¨Šng) (t¨Šng) 5) 为参ć•îCź é€’ĺ˜é‡ĺ’Œć•°ćŽ 位置传递,名称传递,žl„ĺˆäź é€?br /> (t¨Šng) (t¨Šng) (t¨Šng) create or replace procedure add_dept
 (t¨Šng) (t¨Šng) (t¨Šng) (dno number,dname varchar2 default null,loc varchar default null)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into dept values(dno.dname,loc);
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) -- 位置传é€?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exec add_dept(50,'sales','new york');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exec add_dept(60);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exec add_dept(70,'admin');
 (t¨Šng) (t¨Šng) (t¨Šng) -- 名称传é€?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exec add_dept(50,loc=>'new york');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exec add_dept(60,dname=>'sales',loc=>'new york');
 (t¨Šng) (t¨Šng) 6) 昄Ą¤şĺ˝“ĺ‰ç”¨ćˆˇçš„ć‰€ćœ‰ç¨‹ĺşĺŠ(qi¨˘ng)其源代码
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select text from user_source where name='add_dept';
 (t¨Šng) (t¨Šng) 7) 删除˜q‡ç¨‹
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) drop procedure add_dept;
2 开发函ć•?br /> (t¨Šng) 可以在sql语句和其他子˜q‡ç¨‹ä¸­ć‰§čĄŒă€?br /> (t¨Šng) 1 建立函数ĺQŒä¸ĺ¸Śä“Q何参ć•?br /> (t¨Šng) create or replace function get_user
 (t¨Šng) return varchar2
 (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) v_user varchar2(100);
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) select username into v_user from user_users;
 (t¨Šng) (t¨Šng) (t¨Šng) return v_user;
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) --使用变量接收函数˜q”回ĺ€?br /> (t¨Šng) (t¨Šng) var v2 varchar2(100)
 (t¨Šng) (t¨Šng) exec :v1:=get_user
 (t¨Šng) (t¨Šng) print v1;
 (t¨Šng)-- 在sql 语句中直接调用函ć•?br /> (t¨Šng) (t¨Šng) select get_user from dual;
 (t¨Šng)-- 使用dbms_output 调用函数
 (t¨Šng) (t¨Šng) set serveroutput on
 (t¨Šng) (t¨Šng) exec dbms_output.put_line('get_user');
 (t¨Šng)2) 建立函数ĺQŒĺ¸Śćœ‰in 参数
 (t¨Šng) (t¨Šng) create or replace function get_sal(name in varchar2)
 (t¨Šng) (t¨Šng) reutnr number
 (t¨Šng) (t¨Šng) as
 (t¨Šng) (t¨Šng) v_sal emp.sal%type;
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select sal into v_sal from emp where upper(ename)=upper(name);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) return v_sal;
 (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when no_data_found then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20000,'employee does not exist');
 (t¨Šng) (t¨Šng) end;
 (t¨Šng)3) 建立函数ĺQŒĺ¸Śćœ‰out 参数
 (t¨Šng) (t¨Šng) create or replace function get_info
 (t¨Šng) (t¨Šng) (name varchar2,title out varchar2)
 (t¨Šng) (t¨Šng) return varchar2
 (t¨Šng) (t¨Šng) as
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) deptname dept.dname%type;
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select a,job,b,dname into title,deptname from emp a,dept b
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where a.deptno=b.deptno
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) and upper(a.ename)=uppder(name);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) return deptname;
 (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) var job varchar2(20)
 (t¨Šng) (t¨Šng) var dname varchar2(20)
 (t¨Šng) (t¨Šng) exec :dname:=get_info('scott',job);
 (t¨Šng) (t¨Šng) print dname job

 (t¨Šng)4) 带有 in out 参数
 (t¨Šng) create or replace function result
 (t¨Šng) (num1,number,num2 in out nu8mber)
 (t¨Šng) return number
 (t¨Šng) as
 (t¨Šng) (t¨Šng) (t¨Šng) v_result number(6);
 (t¨Šng) (t¨Šng) (t¨Šng) v_remainder number;
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) v_result:=num1/num2;
 (t¨Šng) (t¨Šng) (t¨Šng) v_remainder:=mod(num1,num2);
 (t¨Šng) (t¨Šng) (t¨Šng) num2:=v_remainder;
 (t¨Šng) (t¨Šng) (t¨Šng) retrun v_result;
 (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) when zero_divide then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20000,'zero divied');
 (t¨Šng) end;
 (t¨Šng)5) 函数调用的限ĺˆ?br /> (t¨Šng) -- 在sql 语句中只能调用存储函ć•?br /> (t¨Šng) -- 在sql 语句中只能调用带有输入参数in ĺQŒč€Œä¸čƒ˝ćœ‰čž“ĺ‡şĺ‚ć•°out 和in out 参数的函ć•?br /> (t¨Šng) -- 在sql 语句中调用的函数的参数必™ĺŔL˜Żć ‡ĺ‡†çš„sql 数据žcŐdž‹ĺQŒä¸čƒ˝ć˜Żpl/sql 所ç‰ŇŽ(gu¨Š)œ‰çš„ć•°ćŽçąťĺž?br /> (t¨Šng) -- 在sql 语句中调用的函数不能包含insertĺQŒupdate 和delete
 (t¨Šng)6) 查看源代ç ?br /> (t¨Šng) set pagesize 40
 (t¨Šng) select text form user_source where name='result';
 (t¨Šng)7) 删除函数
 (t¨Šng) drop function result;
 (t¨Šng)3 ˝ŽĄç†ĺ­ç¨‹ĺş?br /> (t¨Šng) 1) 列出当前用户的子˝E‹ĺş
 (t¨Šng) (t¨Šng) select object_name,created,status form user_objects
 (t¨Šng) (t¨Šng) where object_type in ('procedure','function');
 (t¨Šng) 2)列出子程序源代码
 (t¨Šng) (t¨Šng) select text from user_sorce where name='raise_salary'
 (t¨Šng) 3)žcŐd‡şĺ­ç¨‹ĺşçź–čݑ错čŻ?br /> (t¨Šng) (t¨Šng) (t¨Šng) -- show errors
 (t¨Šng) (t¨Šng) (t¨Šng) --使用数据字典user_errors (t¨Šng) ź‹ŽĺŽšé”™čŻŻĺŽŸĺ› ĺ’Œä˝ž|?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select line||'/'||position as "line/col",text error
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from user_errors where name='raise_salary';
 (t¨Šng) (t¨Šng) 4) 列出对象的依赖关žp?br /> (t¨Šng) (t¨Šng) (t¨Šng) -- 使用 user_dependenciess ź‹ŽĺŽšç›´ćŽĽäžčľ–ĺ…łçłť
 (t¨Šng) (t¨Šng) (t¨Šng) select name,type from user_dependencies
 (t¨Šng) (t¨Šng) (t¨Šng) where referenced_name='emp'
 (t¨Šng) (t¨Šng) (t¨Šng) -- 使用 deptree ĺ’?ideptree ź‹ŽĺŽšäžčľ–ĺ’Œč§č§Łäžčľ–ĺ…łžp?br /> (t¨Šng) (t¨Šng) (t¨Šng) select nested_level,name,type from deptree;
 (t¨Šng) (t¨Šng) 5) 重新ž~–译子程ĺş?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 当被应用对象的结构被修改以后ĺQŒĺ°ąäź?x¨Ź)将相关依赖对象转变ä(sh¨´)¸şć— ć•ˆinvalid ĺQŒéœ€čŚé‡ć–°çź–čŻ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 1
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) alter table emp add remark varchar2(100);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select object_name,object_type from user_objects
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where status='invalid'
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 3
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) alter procedure add_employee compile;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) alter view dept10 compile;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) alter function get_info compile;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng)



]]>
处理例外http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74563.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:46:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74563.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74563.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74563.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74563.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74563.html1例外˝Ž€äť?br /> (t¨Šng)1ĺQ?例外分类
 (t¨Šng) 预定义分žcťďźŒéžé˘„ĺŽšäš‰ĺˆ†çąťĺQŒč‡ŞĺŽšäš‰äž‹ĺ¤–ă€?br />2 处理预定义例ĺ¤?br /> (t¨Šng) 1) 常用预定义例ĺ¤?br /> (t¨Šng) (t¨Šng) a access_into_null;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create type emp_type as object
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (name varchar2(2),sal number(6,2));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp emp_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp.name:='scott';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when access_into_null then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line('首先初始化对象emp');
 (t¨Šng) (t¨Šng) (t¨Šng) b case_not_found
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) undef no
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_val emp.sal%type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select sal into v_sal from emp where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) case
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when v_sal<1000 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update emp set sal=sal+100 where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when v_sal<2000 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update emp set sal=sal+150 where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when v_sal<3000 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update emp set sal=sal+200 where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end case;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when case_not_found then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line();
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) c collection is null
 (t¨Šng) (t¨Šng) (t¨Šng) 在给集合元素ĺQˆĺľŒĺĽ—čĄ¨ćˆ–arrayžcŐdž‹ĺQ‰čľ‹ĺ€źĺ‰ĺQŒĺż…™ĺťéŚ–ĺ…ˆĺˆĺ§‹ĺŒ–é›†ĺˆĺ…ƒç´ 
 (t¨Šng) (t¨Šng) (t¨Šng) declare type ename_table_type is table of emp.eanme%type;
 (t¨Šng) (t¨Šng) (t¨Šng) ename_table ename_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select e_name into ename_talbe(2) from emp where empno=$no;
 (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when collection_is_null then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_lilne('必须使用构造方法初始化集合元素');
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) d currsor_already_open
 (t¨Šng) (t¨Šng) (t¨Šng) reopen curosr 如果用户已经使用äş?ji¨Łn)open 命ä×o(h¨´)打开äş?ji¨Łn)昞˝Cşć¸¸ć ‡ďźŒćˆ–ć‰§čĄŒfor循环(隐式的打开游标)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) cursor emp_surosr is select ename,sal,from emp;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) open emp_curosr;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for emp_record in emp_cursor loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(emp_record.eanme);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when cursor_already_open then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line("游标已经打开");
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) e dup_val_on_index
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when dup_val_on_index then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line("列上不能出现重复ĺ€?);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) d invalid_curosr
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) curosr emp_cursor is select ename,sla from emp;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_record emp_crusor%rowtype;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) fetch emp_cursor into emp_record;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) close emp_crusro;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line("游标没有打开");
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) f invalid_number (t¨Šng) can not convert char to nmuber successfully
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update mep set sal=sal+1oo;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when invalid_number then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) g no_data_found (t¨Šng) when select into is executed ,no row is returned (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_sal emp.sal%type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select sal into v_cal form emp where lower(ename)=lower('&name');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when no_data_found then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line('没有˜q”回žl“ćžœ');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) h too_many_row (t¨Šng) ora -01422 there are too many are return when "select into" is executed
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) i zero_divide ora-01476
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) g subscript_beyond_count ora-065533
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type emp_array_type is varray(20) of varchar2(10);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_array emp_array_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_array:=emp_array_type('scott','mary');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line('emp_array(3)');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when subscript_beyone_count then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_out.put_line('­‘…ĺ‡şä¸‹ć ‡čŒƒĺ›´');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) k subscript_outside_limit
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_array(-1);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) l value_error the length of variable cannot contain the actual value;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
3 处理非预定义例外
 (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) e_integrity exception;
 (t¨Šng) (t¨Šng) (t¨Šng) pragma exception_init(e_integrity,-2291);
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) update emp set deptno=dno where empno=&no;
 (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) when a_integrity then
 (t¨Šng) end;
4 处理自定义例ĺ¤?br /> (t¨Šng) 与oracle 错误没有äťÖM˝•联糝ĺQŒäؓ(f¨´)业务逻辑所定义的例ĺ¤?br /> (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) e_no_employee exception;
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) update emp set deptno=&dno where empno=&eno;
 (t¨Šng) if sql%notfound then
 (t¨Šng) (t¨Šng) (t¨Šng) raise e_no_employee;
 (t¨Šng) end if;
 (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) when e_no_emplyee then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line('该雇员ä(sh¨´)¸ĺ­˜ĺœ¨');
5 使用错误例外函数
 (t¨Šng) 使用例外函数可以取得错误号以ĺ?qi¨˘ng)相关的错误消息ĺQŒsqlcode 用于取得oracle 的错误号ĺQŒč€Œsqlerrm
则用于取得与之相关的错误信息ă€?br /> (t¨Šng) 1 sqlcode ĺ’?sqlerrm
 (t¨Šng) ä¸ŢZş†(ji¨Łn)在pl/sql 应用˝E‹ĺşä¸­ĺ¤„理兜䝖äؓ(f¨´)预料的到的oracle 错误ĺQŒç”¨ćˆˇĺŻäťĽĺœ¨äž‹ĺ¤–ĺ¤„ç†éƒ¨ĺˆ†çš?br />when others 自句后,引用两个函数
 (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) v_ename emp.ename%type;
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) select ename into v_ename form emp where sal='&v_sal';
 (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) when no_data_found then
 (t¨Šng) (t¨Šng) (t¨Šng) when others then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(sqlcode);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(sqlerrm);
 (t¨Šng) end;
 (t¨Šng) 2 raise_aaplicaition_error
 (t¨Šng) (t¨Šng) 只能在子˝E‹ĺşä¸­ä‹É用(˜q‡ç¨‹ĺQŒĺ‡˝ć•ŽÍźŒĺŒ…ďźŒč§Śĺ‘ĺ™¨ďź‰(j¨Ş)
 (t¨Šng) (t¨Šng) raise_application_error(error_number,message,[true|false]);
 (t¨Šng) (t¨Šng) error_number 错误ĺřPźŒĺœ¨ďź20000刎ͼ20999 之间
 (t¨Šng) (t¨Šng) message 指定错误消息ĺQŒä¸čƒ˝čś…˜q?048 (t¨Šng)
 (t¨Šng) (t¨Šng) if v_comm is null then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20001,'该雇员无补助');
 (t¨Šng) (t¨Šng) end if;



]]>
开发包http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74562.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:46:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74562.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74562.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74562.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74562.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74562.html (t¨Šng)1) 建立包规范:(x¨Ź) 用于定义包的公共žl„徏ĺQŒĺŒ…ć‹Źĺ¸¸é‡ďźŒĺ˜é‡ĺQŒć¸¸ć ‡ďꌘq‡ç¨‹ĺ’Œĺ‡˝ć•°ç­‰
 (t¨Šng)create or replace package emp_package is
 (t¨Šng) g_deptno number(3):=30;
 (t¨Šng) procedure add_employee(eno number,name varchar2,salary number dno number default g_deptno);
 (t¨Šng) procedure fire_empoyee(eno number);
 (t¨Šng) function get_sal(eno number) return number;
 (t¨Šng)end emp_package;
 (t¨Šng) 2) 建立包体
 (t¨Šng) (t¨Šng) 用于实现包规范所定义的过˝E‹ĺ’Œĺ‡˝ć•°ă€‚ĺœ¨ĺŒ…ä˝“ä¸­äšŸĺŻäťĽĺ•ç‹ŹĺŽšäš‰żUćœ‰žl„äšgĺQŒĺŒ…ć‹Źĺ˜é‡ďźŒĺ¸”R‡ĺQŒčż‡˝E‹ĺ’Œĺ‡˝ć•°˝{‰ă€‚ä˝†ĺœ¨ĺŒ…ä˝“ä¸­ć‰€ĺŽšäš‰çš„çť„äťśĺŞčƒ˝ĺœ¨ĺŒ…ĺ†…ä˝żç”¨ĺQŒč€Œä¸čƒ˝ç”ąĺ…śäť–ĺ­ç¨‹ĺşĺź•ç”¨ă€?br /> (t¨Šng) (t¨Šng) create or replace package body emp_package is
 (t¨Šng) (t¨Šng) (t¨Šng) function validate_deptno(v_deptno number)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) return boolean;
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_temp int;
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select 1 into v_temp from dept where deptno=v_deptno;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) return true;
 (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when no_data_found then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) return false;
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) procedure add_employee(eno number,name varchar2,salary number,dno number default g_deptno)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if validate_deptno(dno) then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into emp(empno,ename,sal,deptno)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) values(eno,name,salary,dno);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) esle
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20011,'部门不存ĺœ?);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;
 (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when dup_val_on_index then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20011,'该雇员已存在')
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) procedure fire_employee(eno number) is
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select from emp where empno=eno;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if sql%notfound then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise application_error(-20012,'');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) function get_sal(eno number) return number
 (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_sal emp.sal%type;
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select sal into v_sal from emp where empno=eno;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) return v_sal;
 (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when no_data_found then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-200012,'');
 (t¨Šng) (t¨Šng) end;
 (t¨Šng) end emp_package;
 (t¨Šng) 3) 调用包组ĺť?br /> (t¨Šng) (t¨Šng) (t¨Šng) ĺQďź ĺœ¨ĺŒä¸€ä¸ŞĺŒ…ĺ†…č°ƒç”¨çť„ĺť?不需要加包名前缀ĺQŒç›´ćŽĽč°ƒç”?br /> (t¨Šng) create or replace paclage body emp_package is
 (t¨Šng) (t¨Šng) (t¨Šng) procedure add_employee(eno number,name varchar2,salary number,dno number default g_deptno)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) validate_deptno(dno) then
 (t¨Šng) (t¨Šng) (t¨Šng) exception
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) -- 调用包公用变é‡?br /> (t¨Šng) (t¨Šng) exec emp_package.g_deptno:=20
 (t¨Šng) (t¨Šng) -- 调用公用˜q‡ç¨‹
 (t¨Šng) (t¨Šng) exec emp_package.add_employee(111,'mary,2000');
 (t¨Šng) (t¨Šng) -- 调用˜qœç¨‹ć•°ćŽĺş“ĺŒ…çš„ĺ…Źç”¨çť„äť?br /> (t¨Šng) (t¨Šng) exec emp_paclage.add_employee@orasrv(1116,'scott',1200);
 (t¨Šng) (t¨Šng) 4) 查看包源代码
 (t¨Šng) (t¨Šng) select text from user_source where name='emp_package' and type='package';
 (t¨Šng) (t¨Šng) 5) 删除ĺŒ?br /> (t¨Šng) (t¨Šng) drop packagee emp_package;
2 使用包重č˝?br /> (t¨Šng) 指多个具有相同名字的子程序,在调用的时候ä‹É用不同的参数传递ă€?br /> (t¨Šng) 1) 建立包规čŒ?br /> (t¨Šng) (t¨Šng) 同名˜q‡ç¨‹ĺ’Œĺ‡˝ć•°ĺż…™ĺŐd…ˇćœ‰ä¸ĺŒçš„čž“ĺ…Ľĺ‚ć•°ĺQŒä˝†ĺŒĺĺ‡˝ć•°˜q”回借的žcŐdž‹ć•°ćŽĺż…éĄťç›¸ĺŒ
 (t¨Šng) (t¨Šng) (t¨Šng) create or replace package overload is
 (t¨Šng) (t¨Šng) (t¨Šng) function get_sal(eno number) return number;
 (t¨Šng) (t¨Šng) (t¨Šng) function get_sal(name varchar2) return number;
 (t¨Šng) (t¨Šng) (t¨Šng) procedure fire_employee(eno number);
 (t¨Šng) (t¨Šng) (t¨Šng) procedure fire_employee(name varchar2);
 (t¨Šng) (t¨Šng) 2) 建立包体
 (t¨Šng) (t¨Šng) (t¨Šng) 。。ă€?br />3) 使用包构造过˝E?br /> (t¨Šng)初始化全局变量
 (t¨Šng)1 建立包规čŒ?br /> (t¨Šng) (t¨Šng) create or replace packiage emp_package
 (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) minsal number(6,2);
 (t¨Šng) (t¨Šng) maxsal number(6,2);
 (t¨Šng) (t¨Šng) procedure upd_sal(eno number,salary number);
 (t¨Šng) (t¨Šng) procedure upd_sal(name varhcar2,salary number);
 (t¨Šng) (t¨Šng) end;-- 定义äş?ji¨Łn)两全局变量和三个公用过˝E?br /> (t¨Šng) (t¨Šng) 2 建立包体
 (t¨Šng) (t¨Šng) (t¨Šng) create or replace package body emp_package is
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) procedure add_employee(cno number,name varchar2,salary number,dno number)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if salary between minsal and maxsal then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into emp(empno,ename,sal,deptno)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ........
 (t¨Šng) (t¨Šng) (t¨Šng) -- 构造过˝E‹ďźŒä˝äşŽĺ­ç¨‹ĺşĺ°žéƒ¨ďźŒĺˇ˛begin 开始已end žl“ćŸ
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select min(sal),max(sal) into minsal,maxsal from emp;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
3 调用包公用组ĺť?br /> (t¨Šng) (t¨Šng) 在同一‹ĆĄäźš(x¨Ź)话中˝WŹä¸€‹ĆĄč°ƒç”¨ĺŒ…的兏用睄坺旜ĺQŒäźš(x¨Ź)自动执行其它构造函敎ͼŒč€Œĺ°†ćĽč°ƒç”¨ĺ…śäť–çť„ĺťşć—śĺˆ™ä¸äź?x¨Ź)再调用其构造过˝E‹ă€?br />4 使用žUŻĺşŚžU§ĺˆŤ
 (t¨Šng)1 安™‡ŒĺŒ…č§„čŒ?br /> (t¨Šng) create or replcace package purity is
 (t¨Šng) minsal number(6,2);
 (t¨Šng) maxsal number(6,2);
 (t¨Šng) function max_sal return number;
 (t¨Šng) function min_sal return number;
 (t¨Šng) pragma restrict_references(max_sal,wnps);--wnps 不能修改包的变量ĺQˆä¸čƒ˝çť™ĺŒ…çš„ĺ˜é‡čľ‹ĺ€ű|ź‰(j¨Ş) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --wnds 不能执行dml
 (t¨Šng) pragma restrict_references(min_sal,wnps);--rnps 用于限制函数不能čŻŐd–ĺŒ…ĺ˜é‡?br /> (t¨Šng) end;

]]>
including Constraintshttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74561.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:45:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74561.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74561.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74561.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74561.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74561.html (t¨Šng) 1) Constrains enforce on the table level
 (t¨Šng) 2) Constrains the deletion of a table if there are dependencies
2 Constrain Guidelines
 (t¨Šng) 1) Name a constraint or the oracle generate a name by the sys_cn format
 (t¨Šng) 2) Create a constraint either
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --At the same time as the table is created.or
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --After the table has been created
 (t¨Šng) 3)Define a constraint at the column or table level
 (t¨Šng) 4)view constraint in the data dictionary
3 Crete a constraint
 (t¨Šng) create table test2
 (t¨Šng) (id int not null,-- column level
 (t¨Šng) (t¨Šng) lname varchar(20),
 (t¨Šng) (t¨Šng) fname varchar(20),
 (t¨Šng) (t¨Šng) constraint uk_test2_1 unique(lname,fname))--table level
4 The not null Constraint
 (t¨Šng) create table employees(
 (t¨Šng) (t¨Šng) employee_id number(6),
 (t¨Šng) (t¨Šng) last_name (t¨Šng) (t¨Šng) varchar2(25) not null (t¨Šng) --system named
 (t¨Šng) (t¨Šng) hire_date (t¨Šng) (t¨Šng) DATE
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) constraint emp_hire_date not null --User named
5Foreign key
 (t¨Šng) create table test3
 (t¨Šng) (rid int,
 (t¨Šng) (t¨Šng) name varchar(30),
 (t¨Šng) (t¨Šng) constraint fk_test3_1 foreign key(rid) reference test2(id));
 (t¨Šng) froeign key constraint keywords
 (t¨Šng) (t¨Šng) (t¨Šng) foreign key :Define the column in thee child table at the table constrain level.
 (t¨Šng) (t¨Šng) (t¨Šng) references (t¨Šng) :Identifies the table and column in the parent table.
 (t¨Šng) (t¨Šng) (t¨Šng) on delete cascade: Delete the dependent rows in the child table when a row in the (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) parent table is deleted
 (t¨Šng) (t¨Šng) (t¨Šng) on delete set null:Convert the dependent foreign key values to null when a row in the
 (t¨Šng) (t¨Šng) (t¨Šng) parent table is deleted.
 (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) --parent table referenced table
 (t¨Šng) (t¨Šng) (t¨Šng) --child table refernce other table
6 The check Constraint
 (t¨Šng) Define a condition that each row must be satisfy
 (t¨Šng) alter table test3
 (t¨Šng) add constrain ch_test3 check(name like 's%')
7 Dropping a Constraint
 (t¨Šng) 1) Remove the manager constraint form the employee table
 (t¨Šng) (t¨Šng) alter table test3
 (t¨Šng) (t¨Šng) drop constriant test3_manager_fk
 (t¨Šng) 2) Remove the primary key constraint on the departments table and drop the associated
 (t¨Šng) (t¨Šng) foreign key constraint on the employees.department_id column
 (t¨Šng) (t¨Šng) alter table departments
 (t¨Šng) (t¨Šng) drop primary key cascade
8 Disabling and enable Constraints
 (t¨Šng) 1)Execute the disable clause of the alter table statment to deactive an integrity constraint
 (t¨Šng) 2)Apply the cascade option to disable dependent integrity constrints
 (t¨Šng) alter table employees
 (t¨Šng) disable constraint emp_emp_id_pl cascade
 (t¨Šng) 3) enabling Constraints
 (t¨Šng) .Active an integrity constraint currently disabled in the table definition by using the enable clause.
 (t¨Šng) (t¨Šng) alter table employees
 (t¨Šng) (t¨Šng) enable constraint emp_emp_id_pk;
 (t¨Šng) a unique (t¨Šng) or a primary (t¨Šng) index is automatically created if you enable a unique key or a primary key constraint (t¨Šng)
 (t¨Šng)8 View Constraints
 (t¨Šng) select constraint_name,constriant_type,serch_condition
 (t¨Šng) from user_constraints
 (t¨Šng) where table_name='employees'
 (t¨Šng)9 view the columns associated with constraints
 (t¨Šng)select constraint_name,column_name
 (t¨Šng)from user_cons_columns
 (t¨Šng)where table_name='employees'

]]>
create viewhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74560.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:45:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74560.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74560.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74560.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74560.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74560.html (t¨Šng) to restrict data access
 (t¨Šng) to make complex query easy
 (t¨Šng) to provide data independence
 (t¨Šng) to provide defferent view of the same data
2 Creating a View
 (t¨Šng) 1)create [or replace] [force|noforce] view view
 (t¨Šng) as subquery
 (t¨Šng) force : create view wether the referenced object existed or not
 (t¨Šng)
 (t¨Šng) desc view_name;
 (t¨Šng)2)create a view by using column aliases in the subquery
 (t¨Šng) create view salv50
 (t¨Šng) as select employee_idIO_NUMBER,last_name NAME,slaary*12 ANN_SALARY
 (t¨Šng) from employees
 (t¨Šng) where department_id=50;
3 Modigy a View
 (t¨Šng) 1) Modigy the emplvu80 view by using create or replace view clause.Add an alias for each column name;
 (t¨Šng) (t¨Šng) create or replace view empvu80
 (t¨Šng) (t¨Šng) (id_number,name,sal,department_id)
 (t¨Šng) (t¨Šng) as select employee_id,first_name||" "||last_name,salary.department_id
 (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) where department_id=80;
 (t¨Šng) (t¨Šng) column aliases in the create view clause are listed in the same order as the columns in the subquery
 (t¨Šng) (t¨Šng) note : alter view_name is not a valid command.
4 Create a Complex View
 (t¨Šng) Create a complex view that contains group functions to display values from two tables
 (t¨Šng) create view dept_sum_vu
 (t¨Šng) (t¨Šng) (name,minsal,maxsal,avgsal)
 (t¨Šng) as
 (t¨Šng) (t¨Šng) select d.department_name,min(e.salary),max(e.salary),avg(e.salary)
 (t¨Šng) (t¨Šng) from employees e,departments d
 (t¨Šng) (t¨Šng) where e.department_id=d.department_id
 (t¨Šng) (t¨Šng) group by d.department_name;
5 Rules for performs DML operaton on a view
 (t¨Šng) 1) You can perform DML operation on simple views
 (t¨Šng) 2) You can not romove a row if the view contains the following:
 (t¨Šng) (t¨Šng) (t¨Šng) --group functions
 (t¨Šng) (t¨Šng) (t¨Šng) --a group by clause
 (t¨Šng) (t¨Šng) (t¨Šng) --the distinct keyword
 (t¨Šng) (t¨Šng) (t¨Šng) -- rownum keyword
 (t¨Šng) (t¨Šng) (t¨Šng) -- column defined by expressions
6 Using the with check option Clause
 (t¨Šng) 1) you can ensure that dml operatons performed on the view stay within the domain of the view by using the with check option clause.
 (t¨Šng) create view test1
 (t¨Šng) as
 (t¨Šng) select * from emp where qty>10;
 (t¨Šng) with check option;
 (t¨Šng) update testview1 set qty=10
 (t¨Šng) where ster_id=6830;
 (t¨Šng) --when you doing the following update operation
 (t¨Šng) update testview1 set qty=5 where id=10;
 (t¨Šng) -- an error will report
 (t¨Šng) --you violate the where clause
 (t¨Šng) 2)Any attempt to change the department number for any row in the view fails because it violates the with check option constraint
 (t¨Šng) (t¨Šng) create or replace view empvu20
 (t¨Šng) (t¨Šng) as
 (t¨Šng) (t¨Šng) select * where department_id=20
 (t¨Šng) (t¨Šng) with check option constriant empvu20_ck;
7 Denying DML Operations
 (t¨Šng) 1 You can ensure that no dml operations occur by adding the with read only option to your view definition.
 (t¨Šng) 2)Any attempt to a DML on any row in the view resuls in an oralce server error.
8 remove veiw
 (t¨Šng) drop view_name
9 inline view
 (t¨Šng) 1) an inline view is a subquery with an alias that you can use within a sql statement.
 (t¨Šng) 2) a named subquery in the from clause of the main query is an exqmple of an inline view
 (t¨Šng) 3) an inline view is not a schema object.
10 Top-N Analysis
 (t¨Šng)1)Top_N querise ask for the n largest or smallest values of a column.
 (t¨Šng)2)Both largest values and smallest values sets considered Top-N queries
 (t¨Šng) select * from (select ster_id,qty from sales);
 (t¨Šng)example
 (t¨Šng) To display the top three earner names and salaries from the employees
 (t¨Šng) select rownum as rank,last_name,salary
 (t¨Šng) from (select last_anme,slary from employee
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) order by slary desc)
 (t¨Šng) where rownum<=3;
 (t¨Šng)

]]>
使用触发ĺ™?/title><link>http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74559.html</link><dc:creator>ĺşäh–‡</dc:creator><author>ĺşäh–‡</author><pubDate>Wed, 11 Oct 2006 06:44:00 GMT</pubDate><guid>http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74559.html</guid><wfw:comment>http://www.aygfsteel.com/kevinfriend/comments/74559.html</wfw:comment><comments>http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74559.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/kevinfriend/comments/commentRss/74559.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/kevinfriend/services/trackbacks/74559.html</trackback:ping><description><![CDATA[ <p>1 触发器简äť?br /> (t¨Šng) 1) 触发事äšg<br /> (t¨Šng) 2) 触发条äšg<br /> (t¨Šng) 3) 触发操作<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) . 触发器代码的大小不能­‘…过32kĺQŒĺŚ‚ćžœä‹É用大量代码徏立触发器ĺQŒĺş”čŻĽĺ…ˆĺťşçŤ‹ĺ­˜ĺ‚¨˜q‡ç¨‹ĺQŒç„śĺŽĺ†č§Śĺ‘器中使用call语句调用存储˜q‡ç¨‹ă€?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) . 触发器中正能含有select ĺQŒinsertĺQŒupdate 和delete 语句ĺQŒč€Œä¸čƒ˝ĺŤćœ‰ddl 语句ĺQŒĺ’Œäş‹ç‰ŠćŽ§ĺˆśčŻ­ĺĽă€?br />2 建立dml 触发ĺ™?br /> (t¨Šng)1) 触发时机<br /> (t¨Šng) beforeĺQŒafter 表示在执行dml操作之后触发ĺ™?br /> (t¨Šng)2)触发事äšg<br /> (t¨Šng) insert ĺQŒupdate 和delete 操作。也可以使用书法事äšg<br /> (t¨Šng)3) dml 触发器是针对特定表进行的 因此必须制定dml 操作所对应的表<br /> (t¨Šng)4) 触发器类ĺž?用于指定当触发器事äšg之后ĺQŒéœ€čŚć‰§čĄŒĺ‡ ‹ĆĄč§Śĺ‘ĺ™¨ć“ä˝œă€‚ĺŚ‚ćžœćŒ‡ĺŽščŻ­ĺĽč§Śĺ‘ĺ™¨žcŐdž‹<br />那么äź?x¨Ź)执行一‹ĆĄč§Śĺ‘器䝣码ĺQšĺŚ‚ćžœćŒ‡ĺŽščĄŒč§Śĺ‘ĺ™¨çąťĺž‹ďźŒĺˆ™äźš(x¨Ź)在每个被作用行上执行一‹ĆĄč§Śĺ‘器䝣码ă€?br /> (t¨Šng)5) 触发条äšg<br /> (t¨Šng)用于指定执行行触发器代码的条äťÓžźŒĺŞćœ‰ä¸ştureć—ÓžźŒć‰äźš(x¨Ź)执行行触发器代码ă€?br /> (t¨Šng) 6) 如果使用pl/sql 存储˜q‡ç¨‹ĺQŒjava 存储˜q‡ç¨‹ĺQŒćˆ–ĺ¤–éƒ¨ĺ¤„ĺ¤„˜q‡ç¨‹éœ€čŚĺœ¨č§Śĺ‘ĺ™¨ć“ä˝œéƒ¨ĺˆ†ç›´ćŽĽä‹É用call<br /> (t¨Šng) 7) dml 触发器触发顺ĺş?br /> (t¨Šng) (t¨Šng) (1)dml 触发器在单行数据上的触发™ĺşĺşă€?br /> (t¨Šng) (t¨Šng) (t¨Šng) 对于单行数据而言ĺQŒć— čŽşć˜ŻčŻ­ĺĽć­¤č§Śĺ‘ĺ™¨ĺQŒčż˜ć˜ŻčĄŒč§Śĺ‘ĺ™¨ďźŒč§Śĺ‘ĺ™¨äťŁç ĺŽžé™…ĺŞć‰§čĄŒä¸€‹ĆĄďźŒňqśä¸”ć‰§čĄŒ<br />™ĺşĺşä¸şbefore 语句触发器,before 行触发器ĺQŒdml 操作ĺQŒafter 行触发器ĺQŒafter 语句触发ĺ™?br /> (t¨Šng) (t¨Šng) (2) dml 触发器在多行数据上的触发™ĺşĺş<br /> (t¨Šng) (t¨Šng) (t¨Šng) before 语句触发ĺ™?<br /> (t¨Šng) (t¨Šng) (t¨Šng) before 行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) after 行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) before行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) after 行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) after语句触发ĺ™?br /> (t¨Šng) (t¨Šng) 语句触发器只被执行一‹ĆĄďźŒč€ŒčĄŒč§Śĺ‘ĺ™¨ĺœ¨ćŻä¸ŞčĄŒä¸Šéƒ˝ć‰§čĄŒä¸€‹ĆĄă€?br /> (t¨Šng) 2) 语句触发ĺ™?br /> (t¨Šng) 当审计dml 操作ĺQŒćˆ–ź‹Žäżdml操作安全执行ć—ÓžźŒĺŻäťĽä˝żç”¨čŻ­ĺĽč§Śĺ‘ĺ™?br /> (t¨Šng) 1 建立before 语句触发ĺ™?br /> (t¨Šng) (t¨Šng) create or replace trigger tr_sec_emp<br /> (t¨Šng) (t¨Šng) before insert or update or delete on emp<br /> (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if to_char(sysdate,'DY','nls_dtate_language=AMERICAN') in ('sat','sun') then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) railse_application_error(-200001,'不能在休息日改变雇员?sh¨´)żĄć?);<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;<br /> (t¨Šng) (t¨Šng) end; (t¨Šng)<br /> (t¨Šng) (t¨Šng) 2 使用条äšg谓词<br /> (t¨Šng) (t¨Šng) inserting ,updating ,deleting<br /> (t¨Šng) (t¨Šng) create or replace trigger tr_sec_emp<br /> (t¨Šng) (t¨Šng) before insert or update or delete on emp<br /> (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if to_char(sysdate,'DY','nls_date_language=american')<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) in('sat','sun') then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) case<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when inserting then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application('-20001','inserting');<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when updating then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application('-20002','updating');<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when deleting then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application('-20003','deleting');<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end case;<br /> (t¨Šng) (t¨Šng) (t¨Šng) end if;<br /> (t¨Šng) (t¨Šng) end;<br /> (t¨Šng) (t¨Šng) 3 建立after 语句触发ĺ™?br /> (t¨Šng) (t¨Šng) (t¨Šng) ä¸ŢZş†(ji¨Łn)dml 操作ĺQŒćˆ–者dml 操作后执行汇总运˝Ž?br /> (t¨Šng) (t¨Šng) create table aduit_table(<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name varchar2(20),ins int,upd int,del int,<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) starttime date,endtime date<br /> (t¨Šng) (t¨Šng) );<br /> (t¨Šng) (t¨Šng) create or replace trigger tr_aduit_emp<br /> (t¨Šng) (t¨Šng) after insert or update or delete emp<br /> (t¨Šng) (t¨Šng) declare<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_temp int;<br /> (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select count(*) into v_temp from aduit_table<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where name='emp';<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if v_temp=0 then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into audit_table values<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ('emp',0,0,0,sysdate,null);<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) case<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when (t¨Šng) inserting then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update aduit_table set ins=ins+1,endtime=sysdate where name='emp';<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when updating then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update audit_table set upd=upd+1,endtime=sysdate where name='emp';<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when deleting then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update aduit_table set del=del+1,endtime=sysdate where name='emp';<br /> (t¨Šng) (t¨Šng) end;<br /> (t¨Šng) 3) 行触发器<br /> (t¨Šng) (t¨Šng) 审计数据变化可以使用行触发器<br /> (t¨Šng) (t¨Šng) 1 建立不before 行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) ä¸ŢZş†(ji¨Łn)取保数据˝WŚĺˆĺ•†ä¸šé€ťčž‘ćˆ–äźä¸šč§„ĺˆ™ďźŒĺŻščž“ĺ…Ľçš„ć•°ćŽ˜q›čĄŒĺ¤ć‚çš„çşŚćŸďźŒĺŻäťĽä˝żç”¨before行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create or replace trigger tr_emp_sal<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) before update of sal on emp<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for each row<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if :new.sal<:old.sla then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raisse_application_error(-200010,'工资只涨不降');<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end ifĺQ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2) 建立after 行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ä¸ŢZş†(ji¨Łn)厥莥dml 操作ĺQŒĺŻäťĽä‹É用语句触发器或oracle žpȝťŸćäž›çš„ĺŽĄčŽĄĺŠŸčƒ˝ďźŒč€Œäؓ(f¨´)äş?ji¨Łn)审计数据变ĺ?br />ĺQŒĺˆ™ĺş”诼使用after 行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create table audit_emp_change(<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name varchar2(10),odl number(6,2),<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) newsal number(6,2),time date);<br /> (t¨Šng) (t¨Šng) (t¨Šng) create or replace trigger tr_sal_change<br /> (t¨Šng) (t¨Šng) (t¨Šng) after update of sal on emp<br /> (t¨Šng) (t¨Šng) (t¨Šng) for each row<br /> (t¨Šng) (t¨Šng) (t¨Šng) declare<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_temp int;<br /> (t¨Šng) (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select count(*) into v_temp from audit_emp_change where name=:old.ename;<br /> (t¨Šng) (t¨Šng) (t¨Šng) if v_temp =0 then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into audit_emp_change<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) values(:old,ename,:old.sal,:new,sal,sysdate);<br /> (t¨Šng) (t¨Šng) (t¨Šng) else<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update audit_emp_change <br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) set oldsal=:old.sal,newsal=:new.sal,time=sysdate where name=:old.ename; (t¨Šng)<br /> (t¨Šng) (t¨Šng) (t¨Šng) end if;</p> <p> (t¨Šng) (t¨Šng) (t¨Šng) end;<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) )<br /> (t¨Šng) (t¨Šng) (t¨Šng) 3) 限制行触发器<br /> (t¨Šng) (t¨Šng) (t¨Šng) 当ä‹É用行触发器,默认情况下会(x¨Ź)咱每个被作用行上七星一‹ĆĄč§Śĺ‘器䝣码ĺQŒäؓ(f¨´)äş?ji¨Łn)时得再特定条äšg下执行行触发器代码,需要ä‹É用when 子句<br /> (t¨Šng) (t¨Šng) (t¨Šng) create or replace trigger tr_sal_change<br /> (t¨Šng) (t¨Šng) (t¨Šng) after update of sal on emp<br /> (t¨Šng) (t¨Šng) (t¨Šng) for each row<br /> (t¨Šng) (t¨Šng) (t¨Šng) when(old.job='salesman')<br /> (t¨Šng) (t¨Šng) (t¨Šng) declare<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_temp int..<br />2 dml 触发器ä‹É用注意事™ĺ?br /> (t¨Šng) 触发器代码不能从触发器所对应的基表中čŻŐd–ć•°ćŽ<br />3 dml 触发ĺ™?br /> (t¨Šng) ä¸ŢZş†(ji¨Łn)保证数据库满­‘łç‰šĺŽšçš„ĺ•†ä¸šč§„ĺˆ™ćˆ–äźä¸šé€ťčž‘ĺQŒĺŻäťĽä‹É用约束,触发器和子程序。约束性能最好,实现最˝Ž€ĺ•ďźŒć‰€äťĽäؓ(f¨´)售选,如果触发器不盟实玎ͼŒĺŻäťĽé€‰ć‹Šč§Śĺ‘器ă€?br /> (t¨Šng) dml 触发器可以用于实现数据安全保护,数据审计ĺQŒć•°ćŽĺŽŒć•´ć€§ďźŒĺ‚ç…§ĺŽŒć•´ć€§ďźŒć•°ćŽĺ¤ĺˆś˝{‰ĺŠŸčƒ˝ă€?br /> (t¨Šng)1) 控制数据安全<br /> (t¨Šng) create or replace trigger tr_emp_time<br /> (t¨Šng) before insert or update or delete on emp<br /> (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) if to_char(sysdate,'hh24') not between '9' and '17' then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(-20101,'not work time');<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;<br /> (t¨Šng) end;<br /> (t¨Šng) 2) 实现数据审计<br /> (t¨Šng) 使用数据审计只能审计sql 操作ĺQŒč€Œä¸äź?x¨Ź)记载数据变ĺ?br /> (t¨Šng) audit insert,update,delete on emp by access<br /> (t¨Šng) 3)实现数据完整ć€?br /> (t¨Šng) 首选约ć?alter table emp add constraint ck_sal check (sal>=800),但是在有些情况下只能使用触发器来实现数据完整ć€?br /> (t¨Šng) (t¨Šng) create or replace trigger tr_check sal<br /> (t¨Šng) (t¨Šng) before update of sal on emp<br /> (t¨Šng) (t¨Šng) for each row<br /> (t¨Šng) (t¨Šng) when (new.sla<old.sal or new.sal>1.2* old.sal)<br /> (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) raise_application_error(,,,,,,)<br /> (t¨Šng) (t¨Šng) end;<br /> (t¨Šng) 3) 使用引用完整ć€?br /> (t¨Šng) 采用 on delete cascade 可以˜q›čĄŒé›†č”ĺˆ é™¤ĺQŒä˝†ć˜Żĺ´ä¸čƒ˝˜q›čĄŒé›†č”ć›´ć–°ă€‚é‡‡ç”¨č§Śĺ‘ĺ™¨ĺŽžçŽ°é›†č”ć›´ć–°<br /> (t¨Šng) (t¨Šng) create or replace trigger tr_update<br /> (t¨Šng) (t¨Šng) after update of sal on emp<br /> (t¨Šng) (t¨Šng) for each row<br /> (t¨Šng) (t¨Šng) begin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update emp set depno=:new.deptno where dentno=:old.deptno;<br /> (t¨Šng) (t¨Šng) end; <br />4 建立instead of 触发ĺ™?br /> (t¨Šng) 对于˝Ž€ĺ•č§†ĺ›‘ÖŻäťĽç›´ćŽĽčż›čĄŒinsert update 和delete ˝{‰ć“ä˝œďźŒä˝†ć˜ŻĺŻšäşŽĺ¤ć‚č§†ĺ›žä¸ĺ…čŽ¸čż›čĄŒinsertĺQŒupdate 和delete 操作ă€?br /> (t¨Šng) 来čƒö一下条件的为复杂视ĺ›?br /> (t¨Šng) (t¨Šng) (t¨Šng) ĺ…ähœ‰ć“ä˝œé›†ĺˆ˝W?unionĺQŒunion all ĺQŒintersectĺQŒminus<br /> (t¨Šng) (t¨Šng) (t¨Šng) ĺ…ähœ‰ĺˆ†çť„函数 minĺQŒmaxĺQŒavgĺQŒsumĺQŒcount<br /> (t¨Šng) (t¨Šng) (t¨Šng) ĺ…ähœ‰group by connect ž~–译 或start with<br /> (t¨Šng) (t¨Šng) (t¨Šng) ĺ…ähœ‰distinct <br /> (t¨Šng) (t¨Šng) (t¨Šng) ĺ…ähœ‰˜qžćŽĽ<br /> (t¨Šng) ä¸ŢZş†(ji¨Łn)在复杂视图上执行dml 操作ĺQŒĺż…™ĺťčځĺŸŢZşŽinstead-of 触发器,建立insteadĺQof 触发器后ĺQŒĺ°ąĺŻäťĽĺŸŢZşŽĺ¤ć‚č§†ĺ›žć‰§čĄŒinsertĺQŒupdate和delete 语句ă€?br /> (t¨Šng) (t¨Šng) instead of 选项只ä‹É用于视图<br /> (t¨Šng) (t¨Šng) ĺŸŢZşŽč§†ĺ›žĺťşçŤ‹č§Śĺ‘器旜ĺQŒä¸čƒ˝ĺŽšäš‰before ĺ’?after<br /> (t¨Šng) (t¨Šng) 在徏立视图时不能指定 with check option <br /> (t¨Šng) (t¨Šng) 当徏立instead of 触发器时ĺQŒĺż…™ĺŔLŒ‡ĺޚfor each row 选项<br /> (t¨Šng) 1) 建立复杂视图dept_emp<br /> (t¨Šng) (t¨Šng) create or replace view dept_emp as<br /> (t¨Šng) (t¨Šng) select a.deptno,a.dname,b,empno,b,ename<br /> (t¨Šng) (t¨Šng) from dept a,emp b<br /> (t¨Šng) (t¨Šng) where a,deptno=b.deptno;<br /> (t¨Šng) 2) 建立 insteadĺQof 触发ĺ™?br /> (t¨Šng) (t¨Šng) create of replacee trigger tr_instead_of_dept_emp<br /> (t¨Šng) (t¨Šng) instead of insert on dept_emp<br /> (t¨Šng) (t¨Šng) for each row<br /> (t¨Šng) (t¨Šng) declare<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_temp int;<br /> (t¨Šng) (t¨Šng) beegin<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select count(*) into v_temp from dept where deptno=:new.deptno;<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if v_temp=0 then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into dept(deptno,dname) values(:new.deptno,:new.dname);<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select count(*)into v_temp from emp where empno=:new.empno;<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) if v_temp=0 then<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into emp(empno,ename,deptno)<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) values(:new.deptno,:new.ename,:new.deptno);<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end if;<br /> (t¨Šng) (t¨Šng) end;<br /> (t¨Šng) (t¨Šng) <br /> (t¨Šng) (t¨Šng) 可以对视图执行insert 操作äş?br /> (t¨Šng) (t¨Šng) (t¨Šng) insert into dept_emp values(50,'admin','1223','mary')<br />5 ˝ŽĄç†č§Śĺ‘ĺ™?br /> (t¨Šng) 1) 昄Ą¤şč§Śĺ‘器俥ć?br /> (t¨Šng) (t¨Šng) (t¨Šng) select trigger_name,status from user_triggers<br /> (t¨Šng) (t¨Šng) (t¨Šng) where table_name='emp';<br /> (t¨Šng) (t¨Šng) 2)ź›ć­˘č§Śĺ‘ĺ™?br /> (t¨Šng) (t¨Šng) (t¨Šng) alter trigger tr_check_sal disable;<br /> (t¨Šng) (t¨Šng) 3) ‹Č€‹zťč§Śĺ‘器<br /> (t¨Šng) (t¨Šng) (t¨Šng) alter trigger tr_check_sal enable;<br /> (t¨Šng) (t¨Šng) 4) ź›ć­˘ćˆ–ćż€‹zťčĄ¨ä¸Šçš„ć‰€ćœ‰č§Śĺ‘ĺ™¨<br /> (t¨Šng) (t¨Šng) (t¨Šng) alter table emp disable all triggers;<br /> (t¨Šng) (t¨Šng) (t¨Šng) alter table emo eanble all triggers; (t¨Šng)<br /> (t¨Šng) (t¨Šng) 5)重新ž~–čŻ‘č§Śĺ‘ĺ™?br /> (t¨Šng) (t¨Šng) (t¨Šng) alter trigger tr_check_sal compile;<br /> (t¨Šng) (t¨Šng) 6) 删除触发ĺ™?br /> (t¨Šng) (t¨Šng) (t¨Šng) drop trigger tr_check_sal;</p> <img src ="http://www.aygfsteel.com/kevinfriend/aggbug/74559.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/kevinfriend/" target="_blank">ĺşäh–‡</a> 2006-10-11 14:44 <a href="http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74559.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用复合变量http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74557.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:44:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74557.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74557.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74557.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74557.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74557.html (t¨Šng)1ĺQ‰çƒŚ(ch¨ł)引表
 (t¨Šng) type ename_table_type is table of emp.ename%type
 (t¨Šng) (t¨Šng) (t¨Šng) index by binary_integer;
 (t¨Šng) ename_table ename_table_type;
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) select ename into ename_table(-1) from emp
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line('雇员名:(x¨Ź)'||ename_table(-1));
 (t¨Šng) end;
 (t¨Šng)
 (t¨Šng) (t¨Šng) set serveroutput no
 (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type area_table_type is table of number
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) index by varchar2(10);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) rea_table area_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) area_table('beijing'):=1;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) area_table('shanghai'):=2;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) area_table('guangzhou'):=3;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(area_table.first);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(area_table.last);
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2) 嵌套čĄ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 索引表类型不能作为篏得数据类型ä‹É用,但是嵌套表可以作ä¸ř™Ą¨žcȝš„ć•°ćŽžcŐdž‹ä˝żç”¨ă€?br />当ä‹É用嵌套表元素ć—ÓžźŒĺż…éĄťĺ…ˆç”¨ĺ…śćž„é€ ć–šćł•ĺˆĺ§‹ĺŒ–ĺ…śĺľŒĺĽ—čĄ¨ĺQ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) a (t¨Šng) 在pl/sql 块中使用嵌套čĄ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type ename_table_type is table of emp.ename%type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table ename_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table:=eanme_table_type('2','2','3');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select ename into ename table(2) from emp where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_ouput.put_line(ename_table(2));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) b 在表中ä‹É用嵌套表
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create type phone_type is table of varchar2(20);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create table employee(
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) id number (4),name varchar2(10),sal number(6,2),
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) phone phone_type
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) )nested table phone store as phone_table;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) -- 为嵌套表插入数据
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into employee values(2,'scott',200,phone_type('2222','333333'));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ĺQďź‹‚€(g¨¨)索嵌套表累得数据
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) set serveroutput on
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) phone_table phone_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select phone into phone_table
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from employee where id=1;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for i in 1..phone_table.count loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(phone_table(i));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) -- 更新嵌套表列的数ć?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) phone_table phone_type:=('44444','555555');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update employee set phone=phone_table
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where id=1;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) 3) 变长数组
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 在ä‹É用varray 时必™ĺŔLŒ‡ĺŽšćœ€ĺ¤§ä¸Şć•ŽÍźŒĺ’Œć•°ćŽçąťĺž?在ä‹É用其元素时必™ĺťčż›čĄŒĺˆĺ§‹ĺŒ–
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type ename_table_type is varray(20) of emp.ename%type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table ename_table_type:=ename_table_type('1','2');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) -- 在快中ä‹É用varray
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type ename_table_type is varray(20) of emp.enameĺQ…typeĺQ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table ename_table_type:=ename_table_type('mary');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select ename into ename_table(1) form emp
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --在表列中使用varray
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create type phone type is varray(20) of varchar2(20);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) create table employee(
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) id number(4),name varchar2(10),
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sal number(6,2),phone phone_type);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 3)记录čĄ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 记录表结合了(ji¨Łn)记录和集合的优点
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type emp_table_type is table of emp%rowtype
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) index by binary_integer;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_table emp_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select * from into emp_table(1) from emp
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(emp_table(1).ename);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 4)多维集合
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 1 多çń”varray
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --define 一žl´é›†ĺ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type al_array_type is varray(10) of int;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --定义二维集合
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type nal_varray_type is varray(10) of a1_varray_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --初始化二žl´é›†ĺ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) nvl nal_varray_type:=nal_varray_type(
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)  (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) a1_varray_type(1,2),
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) a1_varray_type(2,3)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) )
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) beign
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for i in 1..nal_varray_type.count loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for j in 1..a1_array_type.count loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_out.putline(nvl(i)(j));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2 使用多çń”ĺľŒĺĽ—čĄ?br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) table a1_table_type is table of int;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) table nvl_table_type is table of a1_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) nvl nvl_table_type:=nvl_table_type(
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) a1_table_type(1,2),
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) a1_table_type(2,3)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) );
2 集合ć–ŇŽ(gu¨Š)ł•
 (t¨Šng) 1) exist
 (t¨Šng) (t¨Šng) if ename_table.exists(1) then
 (t¨Šng) (t¨Šng) (t¨Šng) ename_table(1):='scott';
 (t¨Šng) (t¨Šng) 2) count ˜q”ĺ›žĺ˝“ĺ‰é›†ĺˆĺ˜é‡ä¸­çš„ĺ…ƒç´ ć€ÖM¸Şć•?br /> (t¨Šng) (t¨Šng) (t¨Šng) ename_table.count
 (t¨Šng) (t¨Šng) 3) limit ˜q”ĺ›žé›†ĺˆĺ…ƒç´ çš„ćœ€ĺ¤§ä¸Şć•ŠW?只有varray ćœ?br /> (t¨Šng) (t¨Šng) 4)first and last
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table.first
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table.last
 (t¨Šng) (t¨Šng) 5) prior 和next
 (t¨Šng) (t¨Šng) (t¨Šng) ename_table.prior(5); ĺQďź˜q”ĺ›žĺ…ƒç´ 5的前一ä¸?br /> (t¨Šng) (t¨Šng) (t¨Šng) ename_table.next(5); (t¨Šng) -- (t¨Šng) 后一ä¸?br /> (t¨Šng) (t¨Šng) 6) extend
 (t¨Šng) (t¨Šng) (t¨Šng) 使用于varray ĺ’?嵌套表ă€?br /> (t¨Šng) (t¨Šng) (t¨Šng) extend add a null value
 (t¨Šng) (t¨Šng) (t¨Šng) extend (n) add n null value
 (t¨Šng) (t¨Šng) (t¨Šng) extend (n,i)add n i value
 (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type ename_table_type is varray(20) of varchar2(20);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table ename_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table:=ename_table_type('mary');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table.extend(5,1);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(ename_table.count);
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) 7) trim
 (t¨Šng) (t¨Šng) trim remove one element from the tail of the collection.
 (t¨Šng) (t¨Šng) trim(n) remove n element from the tail of the colleciton.
 (t¨Šng) (t¨Šng) 8)delete
 (t¨Šng) (t¨Šng) (t¨Šng) delete: delete all the elements
 (t¨Šng) (t¨Šng) (t¨Šng) delete(n) :delete the nth elements
 (t¨Šng) (t¨Šng) (t¨Šng) delete(m,n): delete the elements from m to n
3 集合赋ĺ€?br /> (t¨Šng) 1)žŽ†ä¸€ä¸Şé›†ĺˆçš„ć•°ćŽčľ‹ĺ€źçť™ĺŚä¸€ä¸Şé›†ĺ?clear the destination collectins and set the original collection
 (t¨Šng) (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) type name_varray_type is varray(4) of varchar2(10);
 (t¨Šng) (t¨Šng) (t¨Šng) name_array1 name_varray_type;
 (t¨Šng) (t¨Šng) (t¨Šng) name_array2 name_varray_type;
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name_array1:=name_varray_type('scott','smith');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name_array2:=name_array_type('a','b','c');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name_array1:=name_array2; (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng)
 (t¨Šng)
 (t¨Šng) type name_array1_type is varray(4) of varchar2(10);
 (t¨Šng) type name_array2_type is varray(4) of varchar2(10);
 (t¨Šng) name_array1 name_array1_type;
 (t¨Šng) name_array2 name_array2_type;
 (t¨Šng) ĺ…ähœ‰ç›¸ĺŒçš„ć•°ćŽçąťĺž‹ďźŒĺ•ĺ…ˇćœ‰ä¸ĺŒçš„é›†ĺˆžcŐdž‹ä¸čƒ˝ćž„čľ‹ĺ€?br /> (t¨Šng) 2) žl™é›†ĺˆčľ‹ĺŸŽnull ĺ€?br /> (t¨Šng) (t¨Šng) (t¨Šng) 可以使用delete ćˆ?trim
 (t¨Šng) (t¨Šng) (t¨Šng) 也可以ä‹Éç”?˝Işé›†ĺˆčľ‹žl™ç›ŽčĄ¨é›†ĺ?br /> (t¨Šng) (t¨Šng) (t¨Šng) type name_varray_type is varray(4) of varchar2(10);
 (t¨Šng) (t¨Šng) (t¨Šng) name_array name_varray_type;
 (t¨Šng) (t¨Šng) (t¨Šng) name_empty name_varray_type;
 (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) name_array:=name_varray_type('1','2');
 (t¨Šng) (t¨Šng) (t¨Šng) name_array:=name_empty;
 (t¨Šng) 3) 使用集合操作赋和比较集合都是10g 的内容,p176 先略˜q‡ă€?br />4 扚w‡žl‘ĺޚ
 (t¨Šng) 执行单词sql 操作能传递所有集合元素的数据ă€?br /> (t¨Šng) 1 forall 语句
 (t¨Šng) 用于insert update 和delete操作。在oracle9i 中forall 语句必须ĺ…ähœ‰˜qžçŽ‹çš„ĺ…ƒç´?br /> (t¨Šng) (t¨Šng) (t¨Šng) 1) using forall on insert
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type id_table_type is table of number(6)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) index by binary_integer;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type name_table_type is table of varchar2(2)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) index by binary integer;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) id_table id_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name_table name_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for i in 1..10 loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) id_table(i):=i;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name_table(i):='Name'||to_char(i);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) forall i in 1..id_table.count
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into demo values(id_table(i),name_table(i));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2)using forall on using update
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) forall i in 1..id_table.count
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) upate demo set name:=name_table(i)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where id:=id_table(i);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 3)using forall on using delete
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) forall i in 1..id_table.count
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) delete from demo where id:=id_table(i);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 4) using forall on part of the collection
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for i in1..10 loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) id_table(i):=i;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) name_table(i):="name"||to_char(i);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) forall i in 8..10 l
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into demo values(id_table(i),name_table(i));
 (t¨Šng) (t¨Šng) 2 bulk collect
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) is fit for select into ,fetch into and dml clause
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 1) using bulk collect
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declares (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type emp_table_type is table of emp%rowtype
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) index by binary_integer;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_table emp_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select * bulk collect into emp_table
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from emp where deptno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for i in 1..emp_tablee.count loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put_line(emp_table(i).ename);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2) 在dml 的返回字句ä‹É用bulk collect 字句
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type ename_table_type is table of emp.ename%type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ename_table ename_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) deletee from emp where deptno=&no
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) returning ename bulk_collect into ename_table;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) for i in 1..ename_table.count loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output.put(ename_table(i));
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;

]]>
advanced subqueryhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74556.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:43:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74556.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74556.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74556.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74556.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74556.html (t¨Šng) .the subquery execute (inner query) once before the main query
 (t¨Šng) .the result of the subquery is used by the main query.
2 pairwise comarison subquery
 (t¨Šng)select * from employee
 (t¨Šng)where (manager_id,department_id) in
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select manager_id,department_id
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where employee_id in (178,174))
 (t¨Šng)and employee_id not in (178,174);
 (t¨Šng)nonpairwise comparison subquery
 (t¨Šng)select employee_id,manager_id,department_id
 (t¨Šng)from employee
 (t¨Šng)where manager_id in
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select manager_id
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where employee id in (174,141))
 (t¨Šng)and department_id in
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select department_id
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where employee_id in (174,141))
 (t¨Šng)and employ_id not in (174,141);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) )
3 using a subquery in the from clause
 (t¨Šng) select a.last_name,a,salary,b.slaavg
 (t¨Šng) from employees a ,(select department_id,
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) avg(salary) salavg
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from (t¨Šng) (t¨Šng) employees
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) group by department_id) b
 (t¨Šng) (t¨Šng) where a.department_id=b.department_id
 (t¨Šng) (t¨Šng) and a.salary>b.salavg;
4 scalar subquery expressions
 (t¨Šng) . a scalar subquery expression is a subquery that return exactly on column value from one row
 (t¨Šng) . in oracle8i scalar subqueries can be used in condition and expression part and all clause.
 (t¨Šng)1) sclaar subqueries in casse expression
 (t¨Šng) (t¨Šng) select employee_id ,last_name,
 (t¨Šng) (t¨Šng) (case
 (t¨Šng) (t¨Šng) (t¨Šng) when department_id=
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select department_id from departments
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where location_id=1800)
 (t¨Šng) (t¨Šng) (t¨Šng) then 'canada'
 (t¨Šng) (t¨Šng) (t¨Šng) else 'usa'
 (t¨Šng) (t¨Šng) end) location
 (t¨Šng)from employees
 (t¨Šng) (t¨Šng) 2)scalar subqueries in order by clasue
 (t¨Šng) (t¨Šng) select employee_id,last_name
 (t¨Šng) (t¨Šng) from employees e
 (t¨Šng) (t¨Šng) order by (select department_name
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from departments d
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where e.department_id=d.department);
4 correlated subqueries
 (t¨Šng) (t¨Šng) the wubquery references a column form a table in the parment query
 (t¨Šng) (t¨Šng) select column1,solumn2....
 (t¨Šng) (t¨Šng) from table1 outer
 (t¨Šng) (t¨Šng) where column1 operator
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select column1,column2
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from table2
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where expr1=out.expr2);
 (t¨Šng) e.g 1
 (t¨Šng) (t¨Šng) select last_name,salary,department_id
 (t¨Šng) (t¨Šng) from employees outer
 (t¨Šng) (t¨Šng) where salary>
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select avg(salary)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where department_id=
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) outer.department_id);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) )
 (t¨Šng) (t¨Šng) e.g 2
 (t¨Šng) (t¨Šng) (t¨Šng) display details of those employees who have switched jobs at lease twice
 (t¨Šng) (t¨Šng) (t¨Šng) select e.employee_id,last_name,e.job_id
 (t¨Šng) (t¨Šng) (t¨Šng) from employees e
 (t¨Šng) (t¨Šng) (t¨Šng) where 2<=(select count(*)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from job_history
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where employee_id=e.employee_id);
6 using the exists operator
 (t¨Šng) . the exists operator tests for existencee of rows in the results set of the subquery
 (t¨Šng) . if a subquery row value id found:
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) the search does not continue in the inner query
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) the condition is flagged true
 (t¨Šng) .if a subquery row value is not fount
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) the condition is flagged fasle
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) the search continues in the inner query
 (t¨Šng)e.g
 (t¨Šng) (t¨Šng) (t¨Šng) find employees who have at least one person reporting to them
 (t¨Šng) (t¨Šng) (t¨Šng) select employee_id,last_name,job_id,department_id
 (t¨Šng) (t¨Šng) (t¨Šng) from employees outer
 (t¨Šng) (t¨Šng) (t¨Šng) where exists (select count(*)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where manager_id=outer.employee_id);
 (t¨Šng) not exist.
7 corelated update
 (t¨Šng) (t¨Šng) (t¨Šng) use a correlated subquery to update rows in on table based on rows from another table
 (t¨Šng) (t¨Šng) e.g
 (t¨Šng) (t¨Šng) (t¨Šng) --denormalize the employees table by adding a column to store the department name
 (t¨Šng) (t¨Šng) (t¨Šng) alter table employees
 (t¨Šng) (t¨Šng) (t¨Šng) add(department_name varchar2(14));
 (t¨Šng) (t¨Šng) --populate the table by using a correlated update
 (t¨Šng) (t¨Šng) (t¨Šng) update employees e
 (t¨Šng) (t¨Šng) (t¨Šng) set department_name=
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select department_name
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from departments d
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where e.departmentid=d.department);
8 correlated delete
 (t¨Šng) (t¨Šng) (t¨Šng) delete test1 t1
 (t¨Šng) (t¨Šng) (t¨Šng) where ster_id in(select ster_id form sales t2 where t.ster_id=t2.ster_id); (t¨Šng)
9 using the with clause ,you can use the same query block in a a select statement when it cocurs more than once within a complex query
 (t¨Šng) the with clause retrieves the results of a query block and stores it in the user's the user's templary tablespace
 (t¨Šng) the with clause improves performance.
 (t¨Šng)e.g
 (t¨Šng) (t¨Šng) with
 (t¨Šng) (t¨Šng) dept_costs as(
 (t¨Šng) (t¨Šng) (t¨Šng) select d.department_name,sum(e.salary) as dept_total
 (t¨Šng) (t¨Šng) (t¨Šng) from employee e,departments d
 (t¨Šng) (t¨Šng) (t¨Šng) where e,department_id=d.department_id
 (t¨Šng) (t¨Šng) (t¨Šng) group by d.department_name),
 (t¨Šng) (t¨Šng) avg_cost as(
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select sum(dept_total)/count(*) as dept_avg
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from dept_cost)
 (t¨Šng) select *
 (t¨Šng) from dept_costs
 (t¨Šng) where dept_total>(select dept_avg
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from afb_cost)
 (t¨Šng) order by department_name;

]]>
Ö÷ŐžÖŠÖëłŘÄŁ°ĺŁş şŁŃÎĎŘ| ÄĎ˝­ĎŘ| ÁéÇđĎŘ| Đí˛ýĎŘ| Ń­ťŻ| Î÷ťŞĎŘ| ¸ĘšČĎŘ| čë´¨ĎŘ| ÔĆĎöĎŘ| ÍÁÄŹĚŘÓŇĆě| ťłťŻĘĐ| Čđ˛ýĘĐ| ŃŘşÓ| Ć˝ÎäĎŘ| ÓŔźŞĎŘ| ľ¤ŔâĎŘ| ˝š×÷ĘĐ| Çذ˛ĎŘ| °˛ÇđĘĐ| ÁŮÔóĎŘ| ´óÍŹĎŘ| şé˝­ĘĐ| ĘŻ×ěÉ˝ĘĐ| ËŤłÇĘĐ| şÍĆ˝ĎŘ| ĂÖśÉĎŘ| ËçľÂĎŘ| ÎÄťŻ| °˛Ć˝ĎŘ| šĹ˝ťĘĐ| ł¤ÄţÇř| °˛´ďĘĐ| Äţ˛¨ĘĐ| ťłźŻĎŘ| śŤˇ˝ĘĐ| ÁúÓÎĎŘ| Î÷łÇÇř| şĎ×÷ĘĐ| ÇŕÖÝĘĐ| Đ˰˛ĂË| ÁŮĎÄĎŘ|