ďť??xml version="1.0" encoding="utf-8" standalone="yes"?>精品无吗乱吗av国产爱色,99视频日韩,国产精品欧美一区二区三区不卡http://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)



ĺşäh–‡ 2006-10-12 09:49 发表评论
]]>
开发子˜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)



ĺşäh–‡ 2006-10-11 14:51 发表评论
]]>
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;



ĺşäh–‡ 2006-10-11 14:51 发表评论
]]>
开发包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;

ĺşäh–‡ 2006-10-11 14:46 发表评论
]]>
处理例外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;



ĺşäh–‡ 2006-10-11 14:46 发表评论
]]>
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)

ĺşäh–‡ 2006-10-11 14:45 发表评论
]]>
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'

ĺşäh–‡ 2006-10-11 14:45 发表评论
]]>
使用复合变量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žçŽ‹(hu¨¤)的元ç´?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;

ĺşäh–‡ 2006-10-11 14:44 发表评论
]]>
使用触发ĺ™?/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>开发动态sqlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74555.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:43:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74555.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74555.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74555.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74555.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74555.html1 动态sql ˝Ž€äť?br />2
 (t¨Šng) 1 使用execute immediate 处理ddl 操作
 (t¨Šng) (t¨Šng) (t¨Šng) create or replacee procedure drop_table(table_name varchar2)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_statement varchar2(100);
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_statement:='drop table'||table_name;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_statement;
 (t¨Šng) (t¨Šng) 调用
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exec drop_table('worker');
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) 2) 使用 execute immediate 处理dcl 操作
 (t¨Šng) (t¨Šng) (t¨Šng) create or replace procedure grant_sys_priv
 (t¨Šng) (t¨Šng) (t¨Šng) (priv varchar2,username varchar2)
 (t¨Šng) (t¨Šng) (t¨Šng) is
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat:='gruant'||priv||'to'||username;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_stat;
 (t¨Šng) (t¨Šng) (t¨Šng) end;

 (t¨Šng) (t¨Šng) exec grant_sys_priv('create session','scott');
 (t¨Šng) (t¨Šng) 3 使用execute immediate 处理dml 操作
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 1) 处理无占位符和returning 子句的dml 语句
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) delcare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat varchar2(100);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat:='update emp set sal=sal*1.1 where deptno=44';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_stat;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2) 处理包含占位˝WŚçš„dml语句
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) delare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat varchar2(100);
 (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) sql_stat:='update emp set sql=sql*(1+:percent/100)'
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (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 deptno=:dno';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_stat using &1,&2;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 3) 处理包含returning 子句的dml语句
 (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) salary number(6,2);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat varchar2(200);
 (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) sql_stat:='update emp set sal=sal*(1:percent/100)'
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ||'where empno=:eno returning sal into :salary';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_stat using &1,&2;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) returning into salary;
 (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) 输入1的ĺ€?15
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 输入2的ĺ€?2222
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 新工资;2223
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 4) 使用execute immediate 处理单行查询
 (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) sql_stat varcchar2(100);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_record emp%rowtype;
 (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) sql_stat:='select * from emp where empno=:eno';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_stat into emp_record using &1;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng)3 处理多行查询语句
 (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type empcurtyp is ref cursor;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_cv empcurtyp;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp record emp%rowtype;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat varchar2(100);
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat:='select * from em where deptno=:dno';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) open emp_cv for sql_stat using &dno;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) fetch emp_cu into emp_record;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exit when emp_cv%notfound;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) close emp_cv;
 (t¨Šng) (t¨Šng) end;
4 在动态sql 中ä‹É用bulk语句
 (t¨Šng) (t¨Šng) 1) ĺœ?execute immediate 语句中ä‹É用动态bulk 语句
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) declare
 (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) index by binary_integer;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type sal_table_type is table of emp.sal%type
 (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) ename_table ename_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sa_table sal_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sal_stat varchar2(100);
 (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) sql_stat:='update emp set sal=sal*(1+:percent/100)'
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) || 'where deptno=:dno'
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ||'returning ename,sal into :name,:salary';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execut immediate sql_stat using &percent,&dno
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) returning bulk collect into ename_table,sal_table;
 (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) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) (t¨Šng) (t¨Šng) 2) 使用bulk 子句处理多行查询
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat:='select ename from emp where deptno=:dno';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute immediate sql_stat bulk collect into ename_table using &dno;
 (t¨Šng) (t¨Šng) (t¨Šng) 3) 在fetch 语句中ä‹É用bulk 子句
 (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 empcurtyp is ref cursor;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) emp_cv empcurtyp;
 (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) index by binary_integer;
 (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) sql_stat varchar2(100);
 (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) sql_stat:='select ename from emp where job:=title';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) open emp_cv for sql_stat using '&job';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) fetch emp_cv bulk collect into ename_table;
 (t¨Šng) (t¨Šng) (t¨Šng) 4) 在forall 语句中ä‹É用bulk 子句
 (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 ename_table_type is table of emp.ename%type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) type sla_table_type is table of emp.sal%type;
 (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) sal_table sal_table_type;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat varchar2(100);
 (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) ename_table:=ename_table_type('scott','smith','clark');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) sql_stat:='update emp set sal=sal*1.1 where ename=:1'
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ||'returning sal into :2';
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) forall i in 1..ename_table.count
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execite immediate sql_stat using ename_table(i)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) returning bulk collect into sal_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) end;



ĺşäh–‡ 2006-10-11 14:43 发表评论
]]>
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;

ĺşäh–‡ 2006-10-11 14:43 发表评论
]]>
archetecture queryhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74553.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:42:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74553.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74553.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74553.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74553.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74553.html1 hierachical Queries
 (t¨Šng)select [level],colun,expr......
 (t¨Šng)from table
 (t¨Šng)[where condition(s)]
 (t¨Šng)[start with condition(s)]
 (t¨Šng)[connect by prior condition(s)]
 (t¨Šng)
 (t¨Šng)where condition
 (t¨Šng) exprcompparison_operator expr

2 Starting Point
 (t¨Šng).Specifies the condition that must be met
 (t¨Šng).Accepts any valid condition
3 Waling the Tree
 (t¨Šng) connect by prior column1=column2
 (t¨Šng) walk from the top donw ,using the employees table
 (t¨Šng) (t¨Šng) top donw
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) column1=parentkey
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) column2=childkey
 (t¨Šng) (t¨Šng) bottom up
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) column1=child key
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) column2=parent key

 (t¨Šng) select level,last_name
 (t¨Šng) from employees
 (t¨Šng) start with last_name='king'
 (t¨Šng) connect by prior employee_id=manager_id;



ĺşäh–‡ 2006-10-11 14:42 发表评论
]]>
managing an oracle instancehttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74554.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:42:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74554.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74554.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74554.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74554.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74554.html (t¨Šng) 1)system parameter file -- spfile<sid>,spfile (oracle 9i or later) is a binary file ,it cann't been edited by the text editor. you can change it through "alter system XXX";
 (t¨Šng) in oracle 9.2.0,spfile is stored in the directory of ora92\datatase\spfile<sid>.ora.
 (t¨Šng) 2)init parameter file(pfile)--init<sid>.ora (t¨Šng) (early version) is a text file.it is stored in admin\<sid>\pfile.
 (t¨Šng)2
 (t¨Šng) (t¨Šng) Entries are specific to the instance being accessed;
 (t¨Šng) (t¨Šng) there are two kinds of parameters:
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) explicit: having an entry in the file
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) implicit: no entry within the file,but assuming the oracle default values;
 (t¨Šng) (t¨Šng) Multiple files can be used for a single datasbase to optimize performance in different situation.
 (t¨Šng) (t¨Šng) (t¨Šng) static parameter file,pfile (init.ora)
 (t¨Šng) (t¨Šng) (t¨Šng) persistent parameter file,spfile(spfile<sid>.ora);
3 order being used
 (t¨Šng)first spfile<sid> then int<sid>.ora
 (t¨Šng)select name,value from v$system_parameter
4 spfile spfilesid.ora
 (t¨Šng) 1)Binary file with the abiility to make changes persistent across shutdown and startup
 (t¨Šng) 2)Maintained by oracle server
 (t¨Šng) 3)Record parameter value change made with the alter system command
 (t¨Šng) 4)Can specify wether the change being made is tempory or persistent
 (t¨Šng) 5)value can be delete or reset to allow an instance to revert to the default value;
 (t¨Šng) (t¨Šng) (t¨Šng) e.g
 (t¨Šng) (t¨Šng) (t¨Šng) alter system set timed_statistics=false scope=memory;
 (t¨Šng) (t¨Šng) alter system set timed_statistics=false scope=spfile;
 (t¨Šng) (t¨Šng) startup force-- restart oracle
 (t¨Šng) (t¨Šng) select name,value from v$system_parameter where name like 'time%';
 (t¨Šng) (t¨Šng) alter system set timed_statistics=false scope=both;
 (t¨Šng) 6) not all entries can be modified.to see more ,you can lool up v$system_parameter.in this table the isses_modifiable and issys_modifiable will tell which one (t¨Šng) can be modified in session,and which one can be modified in sys, and which one cann't be modified.
 (t¨Šng) (t¨Šng) e.g
 (t¨Šng) (t¨Šng) select name,isses_modifiable from v$system_parameter where isses_modifiable='true';
 (t¨Šng) (t¨Šng) alter session set timed_statistics=false;
 (t¨Šng) (t¨Šng) e.g
 (t¨Šng) (t¨Šng) select name,issys_modifiable from v$system_parameter where issys_modifiable<>'false';
 (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) .issys_modifiable may have two values immediate or deffered.
 (t¨Šng) (t¨Šng) (t¨Šng) immediate means alter this entry will take effect on the current session ,while derrered will take effect on a new session.
 (t¨Šng)
5 pfile initsid.ora
 (t¨Šng) .the pfile is a text file than can be modigied with an operation system editor.
 (t¨Šng) .modifications to the file are made manually.
 (t¨Šng) .changes to the file take effect on the next startup
6 Creating an spfile
 (t¨Šng) (t¨Šng) spfile can be created from initsid.ora file using thee create spfile command,whick can be executed before or after instance startup.
 (t¨Šng) (t¨Šng) create spfile from pfile. -- you must shutdown the instance befofe or you can use the fellow statement:
 (t¨Šng) (t¨Šng) (t¨Šng) create spfile='d:\oracle\oracle\ora92\database\spfile.ora' from pfile;
 (t¨Šng)yo caan backup the
7 Oracle Managed files(OMF)
 (t¨Šng) 1) OMF are created and deleted by the oracle server as directed by sql commands
 (t¨Šng) 2) OMF are established by parameters:
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) DB_CREATE_DEST: set to give the default location for data files
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) DB__CREATE_OMLINE_LOG_DEST_N: set to gieve the default locations for online redo logs and control files,up to a mazimum of 5 locations.
 (t¨Šng) (t¨Šng) (t¨Šng) e.g 1
 (t¨Šng) (t¨Šng) (t¨Šng) show parameter db_create
 (t¨Šng) (t¨Šng) (t¨Šng) e.g 2
 (t¨Šng) (t¨Šng) (t¨Šng) create tablespace test1;--report error,Do not konw where create the data file
 (t¨Šng) (t¨Šng) (t¨Šng) alter system set db_create_file_dest='d:\oracle\oradb';
 (t¨Šng) (t¨Šng) (t¨Šng) create tablespace test1; -- is ok;
 (t¨Šng) (t¨Šng) (t¨Šng) e.g 3
 (t¨Šng) (t¨Šng) (t¨Šng) alter database add logilfe group 6;
 (t¨Šng) (t¨Šng) (t¨Šng) -- if you do not specify the db_create_online_log_dest_N ,the log file will create at (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --the location which is specified by the db_create_file_dest.
 (t¨Šng) (t¨Šng) (t¨Šng) e.g 4
 (t¨Šng) (t¨Šng) (t¨Šng) drop table test1; -- oracle alse remove it's physicial file;
 (t¨Šng) (t¨Šng) (t¨Šng) alter database drop logfile group 6-- also romove the physical file.
 (t¨Šng) (t¨Šng) (t¨Šng) e.g 5
 (t¨Šng) (t¨Šng) (t¨Šng) create tablespace test1
 (t¨Šng) (t¨Šng) (t¨Šng) datafile 'd:\oracle\oradb\test1.dbf' size 2m;
 (t¨Šng) (t¨Šng) (t¨Šng) drop tablespace test1 -- if you create tablespace or others object in this way ,you (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --will cann't remove the physical file.
 (t¨Šng) (t¨Šng) (t¨Šng) drop table test1 including comtents and datafiles;
8 Starting up a database mount
 (t¨Šng)startup nomomount :instance started for this instance;create db.create controlfule
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 1) create alert_sid.log (t¨Šng) startup log which is stored in <oracle_home>\admin\<sid>\bdump
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 2) start instance include allocating memory and start bpprocess
 (t¨Šng) (t¨Šng) (t¨Šng) select * from v$instance;
 (t¨Šng) (t¨Šng) (t¨Šng) select * from v$bgprocess;
 (t¨Šng) (t¨Šng) (t¨Šng) select * from V$sga;
 (t¨Šng) alter database mount;
 (t¨Šng) (t¨Šng) (t¨Šng) open control files for the instance;
 (t¨Šng) (t¨Šng) (t¨Šng) select* from v$database;
 (t¨Šng) (t¨Šng) (t¨Šng) select* from v$tablespace;
 (t¨Šng) (t¨Šng) (t¨Šng) select* from datafile;
 (t¨Šng) (t¨Šng) (t¨Šng) select* from log;
 (t¨Šng)alter database open;
 (t¨Šng) (t¨Šng) (t¨Šng) all files opened as describled by the control file for this instance;
9 start up
 (t¨Šng) start pfile=...\initsid.ora; (t¨Šng) (t¨Šng) (alter session set nls_language=american)
 (t¨Šng) alter database open read only; you can not change database to read only after the database has been created.
 (t¨Šng) startup database restrict;
 (t¨Šng) alter system enable restricted session;-- only people with restricted privilege can (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --access the database;
 (t¨Šng) (t¨Šng) alter restricted session to kkk;
 (t¨Šng) (t¨Šng) alter disable restricted session
10 opening a database in read only mode
 (t¨Šng) (t¨Šng) a databse can be opened as read only database alter database open read only;
 (t¨Šng) (t¨Šng) a read-only database can be used to :
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute queries
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) execute disk sorts using locally managed
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) take data files offline and online ,not tableespaces
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) perform recovery of offline data files and tablespace;
11Shutting down teh database
 (t¨Šng) normal: wait until current sessions end,wait until current trasactions end ,force a checkpoint and close files.
 (t¨Šng) tansactional:wail until current transaction end.force a checkpoint and close files
 (t¨Šng) immediate :force a checkpoint and close the files
 (t¨Šng) abort (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) : do nothing ,just close the files. when startup ,need recovery.
12 Managing an instance by monitoring Diagnostic Files
 (t¨Šng) Diagnostic files contain information about significant events encounted while the instance is operational.
 (t¨Šng) (t¨Šng) .user to resolve problem or to better manager the database.
 (t¨Šng) (t¨Šng) .server types of dignostic files exist:
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) alertsid.log (t¨Šng) --which location is specified by the background_dump_dest (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --entry in the initsid.ora.tje default value is (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) --<ora_home>\admin\sid\bdump
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) background trace files -- the same as above;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) user_tace files
13 alert log file
 (t¨Šng) (t¨Šng) the alertsid.log file recored the command and result of major event while the database is operational.
 (t¨Šng) (t¨Šng) .it is userd for day-to-day operational information or dignosing database errors
 (t¨Šng) (t¨Šng) .ench entry has a time stamp associated with it.
 (t¨Šng) (t¨Šng) .the dba manager the alertsid.log file.
 (t¨Šng) (t¨Šng) .its location is defined by background_dump_dest.
14 enabling or disabling user tracing
 (t¨Šng) (t¨Šng) (t¨Šng) . sessin level using the alter session
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) alter session set sql_trace=true;
 (t¨Šng) (t¨Šng) (t¨Šng) session level by execcuting dbms
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)

ĺşäh–‡ 2006-10-11 14:42 发表评论
]]>
Managing Passswordd Security and Resourceshttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74551.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:41:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74551.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74551.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74551.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74551.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74551.html1 Profiles
 (t¨Šng) 1)a profile is a named set of password and resource limits
 (t¨Šng) 2)Profiles are assigned to user by the create user or alter user command
 (t¨Šng) 3)can be enable or disable
 (t¨Šng) 4)can relate to the default profile.
2 Password Management
 (t¨Šng) Password history,account locking,password expiration and aging ,password verificcation.
3Enabling Password Management
 (t¨Šng) 1)set up passwordd management by using profiles and assign them to users
 (t¨Šng) 2)lock unlock and expire accounts usign the create user or alter user
 (t¨Šng) 3)alwarys enforcing
 (t¨Šng) (t¨Šng) e.g
 (t¨Šng) (t¨Šng) create user test identified by test;
 (t¨Šng) (t¨Šng) alter user test account lock;
 (t¨Šng) (t¨Šng) alter user test account unlock;
 (t¨Šng) (t¨Šng) alteer user test password expire;
4 Passowrd Account locking
 (t¨Šng) FAIKED_LOGIN_ATTEMPS: number of failed login attemts before lockout of the account
 (t¨Šng) PASSWORD_LOCK_TIME : number of days the account is locked after the specified number of (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) failed login attemps
 (t¨Šng) e.g
 (t¨Šng) create profile profile1 limit
 (t¨Šng) password_lock_time 1/1440 (t¨Šng) -- one muinuts
 (t¨Šng) failed_login_attempts 3;
 (t¨Šng)
 (t¨Šng) alter user test
 (t¨Šng) profile profile1;

 (t¨Šng) alter profile profile1 limit
 (t¨Šng) passowrd_lock_time 1/24 (t¨Šng) --one hour
5 passowrd expiration and aging
 (t¨Šng)passwowd_life_time lifetime of the passowrd in days (t¨Šng) after which the password expires(有效ćœ?
 (t¨Šng)password_grace_time grace period in days for changing the password after the first (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) successful login afteer the password has expired(锁定ćœ?
 (t¨Šng)e.g
 (t¨Šng)alter profile profile1 limit
 (t¨Šng)password_life_time 2
 (t¨Šng)password_grace_time 3;
6 password history
 (t¨Šng)password_reuse_time:number of days before a passowrd and be resued
 (t¨Šng)password _reuse_max:maxum number of times password can bee reused
 (t¨Šng)e.g
 (t¨Šng)alter profile profile1 limit
 (t¨Šng)password_reuse_time 10
 (t¨Šng)password_reuse_max 3;
7passowrd Verification(study latter)
8drop a profile
 (t¨Šng)drop profile profile1 (cascade);
the user will use the default profile.it will take effect on the new session.
9 Resource Management
 (t¨Šng)Resource mangement limits can be enforced at the session level,the call level or both
 (t¨Šng)limits can be defined by profiles using the create profile command
 (t¨Šng)enable resource limints with the
 (t¨Šng) .resource_limit initialization parameter
 (t¨Šng) (t¨Šng) alter system command
 (t¨Šng)e.g
 (t¨Šng)alter system set resource_limit=true;
10 setting a Resdource limits at session level
 (t¨Šng) cup_per_session : total cpu time measured in hundredths of seconds (癑ֈ†äš‹ä¸€żU?
 (t¨Šng) sessions_per_user: number of coucurrent sessions allowed for each username
 (t¨Šng) connect_time:elapsed connect time measured in minutes
 (t¨Šng) idle_time :periods of inactive time measured in minutes
 (t¨Šng) logical_reads_per_session: number of data blocks
 (t¨Šng) private_sga :measure in reads
 (t¨Šng) e.g
 (t¨Šng) alter profile profile1 limit
 (t¨Šng) cpu_per_session 100000
 (t¨Šng) connect_time 60
 (t¨Šng) idle_time 5;
 (t¨Šng)
 (t¨Šng) alter user test profile profile1

11 Setting Resource limits at call level (t¨Šng)
 (t¨Šng)e.g
 (t¨Šng)alter profile profile1
 (t¨Šng)cpu_per_call 1000 (t¨Šng) -- cup time per call in
 (t¨Šng)logical_reads_per_call --number of data balock that can be read per call
 (t¨Šng)
 (t¨Šng) create profile develper_prof limit
 (t¨Šng) (t¨Šng) session_per_user2
 (t¨Šng) (t¨Šng) cpu_per_session 10000
 (t¨Šng) (t¨Šng) idle_time 60
 (t¨Šng) (t¨Šng) connect_time 480
12 Managing Resources using database resource manager
 (t¨Šng) (t¨Šng) 1)Provides the oracle server with more control over resorce management decisions
 (t¨Šng) (t¨Šng) 2)elements of database resorcee manager
 (t¨Šng) (t¨Šng) (t¨Šng) resource consumer group
 (t¨Šng) (t¨Šng) (t¨Šng) resourcee plan
 (t¨Šng) (t¨Šng) (t¨Šng) resource allocation method
 (t¨Šng) (t¨Šng) (t¨Šng) resource plan directives
 (t¨Šng) (t¨Šng) 3)dbms_resource_manager package is uerd to create and maintain elements
 (t¨Šng) (t¨Šng) 4)requires administer_resource_manager privilege
 (t¨Šng) (t¨Šng) (t¨Šng) desc dbms_resoource_manager
13 0btaining password and resource limits informaiton
 (t¨Šng) information about password and resource limits can be obtained by querying the data dictonary
 (t¨Šng) (t¨Šng) dba_users
 (t¨Šng) (t¨Šng) select * from users;
 (t¨Šng) (t¨Šng) dba_profiles
 (t¨Šng) (t¨Šng) select * from dba_profiles where profile='PROFILE1'



ĺşäh–‡ 2006-10-11 14:41 发表评论
]]>
extension to dml in oraclehttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74552.htmlĺşäh–‡ĺşäh–‡Wed, 11 Oct 2006 06:41:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74552.htmlhttp://www.aygfsteel.com/kevinfriend/comments/74552.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74552.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/74552.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/74552.html (t¨Šng)1)the insert...select statement can be userd to insert row into multiple table as part of a single dml statement.
 (t¨Šng)2) multitable insert statements can be used in data warehousing systems to transfer data from one or more operational sources to source to a set of target table.
 (t¨Šng)3) they providde significant performance improvement over
 (t¨Šng) (t¨Šng) (t¨Šng) single dml versuls multiple insert...select statement
 (t¨Šng) (t¨Šng) (t¨Šng) single dml versus a proceedduree to do mutiple inserts using if ,,, then syntax.
2
 (t¨Šng) unconditional insert
 (t¨Šng) insert all
 (t¨Šng) (t¨Šng) (t¨Šng) into sal_history values (EMPID,HIREDATE,SAL)
 (t¨Šng) (t¨Šng) (t¨Šng) into mgr_history values (EMPID,MGR,SAL)
 (t¨Šng) select employee_id EMPID,hire_date JIREDATE,
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) salary SAL,manager_id MGR
 (t¨Šng) from employees
 (t¨Šng) where employee_id>200;
3 Conditional insert all
 (t¨Šng) insert all
 (t¨Šng) (t¨Šng) when sal>1000 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) into sal_history values(empid,hiredate,sal)
 (t¨Šng) (t¨Šng) when mgr>200 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) into mgr_history values(empid,mgr,sal)
 (t¨Šng) (t¨Šng) select emp_id empid,hire_date hiredate,salary sal,manager_id mgr,
 (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) where employee_id>200;
4 Conditional first insert
 (t¨Šng) insert first
 (t¨Šng) (t¨Šng) (t¨Šng) when sal >25000 (t¨Šng) then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) into special_sal values(deptid,sal)
 (t¨Šng) (t¨Šng) (t¨Šng) when hiredate like ('%00%') then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) into hiredate_history_00 values(deptid,hiredate)
 (t¨Šng) (t¨Šng) (t¨Šng) when hiredate like ('%99%') then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert hiredate_history_99 values(ddeptid,hiredate)
 (t¨Šng) (t¨Šng) (t¨Šng) else
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) into hiredate_history values(deptid,hiredate)
 (t¨Šng) (t¨Šng) (t¨Šng) select ddepartmeent_id deptid,sum(salary) sal,
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) max(hire_date) hiredate
 (t¨Šng) (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) (t¨Šng) group by department_id;
5 Pivoting insert
 (t¨Šng)insert all
 (t¨Šng) into sales_info values (employee_id,week_id,sales_mon)
 (t¨Šng) into sales_info values (employee_id,week_id,sales_tue)
 (t¨Šng) into sales_info values (employee_id,week_id,sales_wed)
 (t¨Šng) into sales_info values (employee_id,week_id,sales_thur)
 (t¨Šng) into sales_info values (employee_id,week_id,sales_fri)
 (t¨Šng) select employee_id,weekid,sales_mon,sales_tue,sales_wed,sales_thur,sales_fri
 (t¨Šng) from sales_source_data;
6 create index with create table statement
 (t¨Šng) create table new_emp
 (t¨Šng) (employee_id number(6)
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) primary key using index
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (create index emp_id_idx on new_emp(employee_id)),
 (t¨Šng) (t¨Šng) first_name varchar2(20),
 (t¨Šng) (t¨Šng) last_name varchar2(25)
 (t¨Šng) )

ĺşäh–‡ 2006-10-11 14:41 发表评论
]]>
access controll http://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72839.htmlĺşäh–‡ĺşäh–‡Fri, 29 Sep 2006 07:40:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72839.htmlhttp://www.aygfsteel.com/kevinfriend/comments/72839.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72839.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/72839.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/72839.html1Privilgeges
 (t¨Šng)1) Database security:
 (t¨Šng) --System security
 (t¨Šng) --Data security
 (t¨Šng)2)System privileges:Caining access to the database
 (t¨Šng)3)Object privileges:manipulationg thee content of the database objects
 (t¨Šng)4)Schemas:Collections of objects ,such as tables,views,and sequences
2System Privileges
 (t¨Šng) . More than 100 privileges are available;
 (t¨Šng) . The database administrator has high-levle system privileges for tasks such as:
 (t¨Šng) (t¨Šng) (t¨Šng) creating new user,removing user,removing tables,backing up tables
3 Creating user
 (t¨Šng)the dba creates users by using the create user statement
 (t¨Šng)create user user
 (t¨Šng)identified by password;
 (t¨Šng)e.g create user object scott
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) identified by tiger;

 (t¨Šng)SQL> create user testuser
 (t¨Šng) 2 (t¨Šng) identified by test;

User created

SQL> conn testuser/test@orcl2000
Not logged on

SQL> grant access session to testuser;

grant access session to testuser

Not logged on

SQL> conn digit_cc/digit_cc@orcl2000
Connected to Oracle9i Enterprise Edition Release 9.2.0.1.0
Connected as digit_cc

SQL> grant create session to testuser;

Grant succeeded

SQL> conn testuser/test@orcl2000;
Connected to Oracle9i Enterprise Edition Release 9.2.0.1.0
Connected as testuser
4 user System privileges
 (t¨Šng)once a user is created,the dba can grant specific system privileges to a user
 (t¨Šng)grant privilege[,privilege...]
 (t¨Šng)to user [,user|role,public...];

 (t¨Šng)DBA can grant a user specific system privileges
 (t¨Šng)grant create session,create table,create sequence,create view (t¨Šng) to scott;
5 creating and granting privileges to role
' Create a role
 (t¨Šng) create role manager;
 (t¨Šng).grant privileges to a role
 (t¨Šng) grant create table,create view to manager
 (t¨Šng).Grant a role to user
 (t¨Šng) grant manager to kochar;
 (t¨Šng)
SQL> create role testrole;

Role created

SQL> grant create table,create view,create sequence to testrole;

Grant succeeded

SQL> grant testrole to testuser;
6 change your password
 (t¨Šng)you can change your password by using the alter user statement;
 (t¨Šng)alter user scott
 (t¨Šng)indetified by lion;
7 object privileges
 (t¨Šng)object privileges vary from object to object
 (t¨Šng)an owner has all the privilege to the object
 (t¨Šng)an owner can give specific privilege on that owner object
 (t¨Šng) grant select on auther to testuser;
 (t¨Šng) grant select on outher to testuser with grant option -- testuser also can grant it to

other user;
 (t¨Šng) grant update(department_name,location_id)
 (t¨Šng) on departments
 (t¨Šng) to scott,manager;
8 how to revoke object privileges
 (t¨Šng)--you use the revoke statement to revoke privileges granted to other users
 (t¨Šng)--privileges granted to other users through the with grant option clause are also revoked.
 (t¨Šng) revoke privilege {[,privilege...]|all} on object
 (t¨Šng) from {user[,user....]|role|public}
 (t¨Šng) [cascade constraints]
 (t¨Šng) revoke select on author from user;
9 Database Links
 (t¨Šng)Database link allow user to access data in the remote database;
SQL> create database link kjw1
 (t¨Šng) 2 (t¨Šng) connect to digit_cc identified by digit_cc
 (t¨Šng) 3 (t¨Šng) using 'orcl2000';

Database link created

SQL> select * from digit_cc.table_action@kjw1;



ĺşäh–‡ 2006-09-29 15:40 发表评论
]]>
database link 使用 转蝲http://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72838.htmlĺşäh–‡ĺşäh–‡Fri, 29 Sep 2006 07:35:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72838.htmlhttp://www.aygfsteel.com/kevinfriend/comments/72838.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72838.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/72838.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/72838.html链接到远˝E‹ć•°ćŽĺş“

在一个分布式的环境里ĺQŒć•°ćŽĺş“é“žćŽĽć˜ŻĺŽšäš‰ĺˆ°ĺ…śĺŽƒć•°ćŽĺş“çš„čˇŻĺž„çš„ä¸€ä¸Şé‡čŚć–šćł•ďźŒä˝żĺž—˜qœç¨‹ĺ¤„理夊čĄŁć— çźă€?/p>

要获得数据库链接的更深奥的知识,查看Oracle8i SQL ReferenceĺQˆOracle8i SQL参考)(j¨Ş)和Oracle8i Concepts ĺQˆOracle8i概念手册ĺQ‰ă€‚诌žl†čľ„ć–™çš„ĺŚä¸€ä¸ŞćžĺĽ˝çš„ćĽćşć˜ŻOracle8i Distributed Database SystemsĺQˆOracle8i分布式数据库žpȝťŸć‰‹ĺ†ŒĺQ‰ă€?/p>

今天许多˜qčĄŒOracle的机构有不止一个Oracle数据库。有时不˝ŽĄĺŽŸčŽĄĺˆ’ć˜ŻĺŚ˜q™ć ˇĺQŒä¸€ä¸Şć•°ćŽĺş“ä¸­çš„ć•°ćŽĺŻčƒ˝ä¸ŽĺŚä¸€ć•°ćŽĺş“ä¸­çš„ć•°ćŽĺ…łč”ă€‚ĺ‡şçŽ°čż™żUćƒ…冉|—śĺQŒä˝ ĺŻäťĽé“žćŽĽ˜q™ä¸¤ä¸Şć•°ćŽĺş“ä˝żĺž—ç”¨ćˆˇćˆ–ĺş”ç”¨ç¨‹ĺşĺŻäťĽčŽżé—Žć‰€ćœ‰ć•°ćŽďźŒžŽąĺĽ˝čąĄĺŽƒäťŹĺœ¨ä¸€ä¸Şć•°ćŽĺş“ä¸­ă€‚ĺ˝“ä˝ čż™äšˆĺšć—ÓžźŒä˝ ĺ°ąćœ‰äş†(ji¨Łn)一个分布式数据库系žlŸă€?

如何žŽ†ä¸¤ä¸Şć•°ćŽĺş“é“žćŽĽĺœ¨ä¸€čľˇĺ‘˘ĺQŸä‹É用一个数据库链接来完成。数据库链接是定义一个数据库到另一个数据库的čľ\径的对象。数据库链接允许你查询远˝E‹čĄ¨ĺ?qi¨˘ng)执行远˝E‹ç¨‹ĺşă€‚ĺœ¨äťÖM˝•ĺˆ†ĺ¸ƒĺźçŽŻĺ˘ƒé‡ŒĺQŒć•°ćŽĺş“é“žćŽĽéƒ˝ć˜Żĺż…čŚçš„ă€?

˝Ž€ĺ•ćĄˆäž?/p>

数据库链接的目的是定义一条到˜qœç¨‹ć•°ćŽĺş“的衯垄ĺQŒä‹É你可以通过在本地执行一条SQL语句来ä‹É用那个数据库中的表和其它的对象。例如,你在一个远˝E‹ć•°ćŽĺş“ä¸Šćœ‰ä¸€ä¸Şç§°äš‹äؓ(f¨´)"geographic feature name"的表ĺQŒč€Œä˝ ćƒ›_œ¨ĺˇ˛čżžćŽĽĺˆ°ä˝ ćœŹĺœ°ć•°ćŽĺş“çš„ćƒ…ĺ†ľä¸‹čމK—Žé‚Łäş›ć•°ćŽă€‚ć•°ćŽĺş“é“žćŽĽć­Łć˜Żä˝ ć‰€éœ€čŚçš„ă€‚ĺœ¨ĺťşçŤ‹ĺŽƒäš‹ĺ‰ďźŒä˝ ĺż…™ĺŔLœé›†ĺŚ‚ä¸‹äżĄćŻďźš(x¨Ź)

一个网žlœćœĺŠĄĺżUŽÍźŒä˝ çš„ćœŹĺœ°ć•°ćŽĺş“äş‹äž‹čƒ˝ĺ¤Ÿä‹É用它来与˜qœç¨‹äş‹äž‹ç›¸čżžćŽĽčżœ˝E‹ć•°ćŽĺş“ä¸Šçš„ćœ‰ć•ˆç”¨ćˆˇĺĺ’ŒĺŁä×o(h¨´)ž|‘çťœćœĺŠĄĺç§°ć˜ŻćŻä¸€ä¸Şć•°ćŽĺş“é“žćŽĽĺż…éœ€çš„ă€‚ćŻä¸€‹ĆĄä˝ äťŽĺŽ˘ćˆähœşPC使用SQL*Plus˜qžćŽĽĺˆîC˝ çš„ć•°ćŽĺş“ć—‰™ƒ˝čځä‹É用服务名żU°ă€‚ĺœ¨é‚Łäş›ćƒ…ĺ†ľä¸‹ďźŒä˝ ćäž›çť™SQL*Plus的网žlœćœĺŠĄĺżU°ć˜Żé€ščż‡ĺœ¨ä˝ çš„厢ćˆähœşä¸Šçš„nsnames.ora文äšg中查扑֮ƒäťŹćĽč§Łćžçš„ă€‚ĺœ¨ć•°ćŽĺş“é“žćŽĽä¸­ä˝żç”¨çš„ç˝‘žlœćœĺŠĄĺżUîCšŸć˜ŻĺŚ‚ć­¤ďźŒé™¤éžć˜Żé‚Łäş›ĺĺ­—ć˜Żä˝żç”¨éŠČ•™ĺœ¨ćœĺŠĄĺ™¨ä¸Šçš„tnsnames.ora文äšg来解析ă€?

在你定义数据库链接时指定的用户名和口令,用于建立与远˝E‹äş‹äž‹çš„˜qžćŽĽă€‚ä¸éœ€ź‹Źçź–ç ç”¨ćˆˇĺĺ’ŒĺŁäť¤ďźŒĺťşçŤ‹ć•°ćŽĺş“é“žćŽĽäšŸć˜ŻĺŻčƒ˝çš„ç”šč‡łć˜Żĺ€źĺž—é€‰ĺ–çš„ă€‚ć—˘ç„śčż™ć řPźŒçŽ°ĺœ¨ćˆ‘äťŹćł¨ć„˜q™ä¸Şćœ€ç›´ćŽĽçš„äž‹ĺ­ă€?

下列语句建立äş?ji¨Łn)一个数据库链接ĺQŒĺŽƒĺ…čŽ¸čŽ‰K—ŽĺŽ˘ćˆˇĺ¸ćˆˇĺQŒčż™ä¸Şĺ¸ćˆäh˜Żäş‹ĺ…ˆĺœ¨GNIS数据库徏好的ĺQ?/p>

CREATE DATABASE LINK GNIS
CONNECT TO GUEST IDENTIFIED BY WELCOME
USING 'GNIS';

链接名称GNIS紧随LINK关键字。当˜qžćŽĽĺˆ°čżœ˝E‹äş‹äž‹ć—śĺQŒCONNECT TO...IDENTIFIED子句指定UEST/WELCOME作äؓ(f¨´)用户名和口ä×o(h¨´)使用 。USING子句指定通过ž|‘çťœćœĺŠĄĺç§°GNIS建立˜qžćŽĽă€‚ä‹É用这一链接ĺQŒçŽ°ĺœ¨ä˝ ĺŻäťĽĺœ¨čżœ˝E‹ć•°ćŽĺş“ä¸ŠćŸĽčŻ˘ć•°ćŽă€‚äž‹ĺŚ‚ďźš(x¨Ź)

SQL> SELECT GFN_FEATURE_NAME
2 FROM GNIS.FEATURE_NAMES@GNIS
3 WHERE GFN_FEATURE_TYPE='falls'
4 AND GFN_STATE_ABBR='MI'
5 AND GFN_COUNTY_NAME='Alger';

GFN_FEATURE_NAME
_________________
Alger Falls
Au Train Falls
Chapel Falls
Miners Falls
Mosquito Falls
Tannery Falls
..

在SELECT语句中@GNIS紧随表名żUŽÍźŒčŻ´ć˜ŽGNIS.FEATURE_NAMES表是在远˝E‹ć•°ćŽĺş“ĺQŒĺş”诼通过GNIS链接讉K—ŽĺQŒé“žćŽĽçąťĺž‹Oracle支持几种不同žcŐdž‹çš„é“žćŽĽă€‚čż™äş›çąťĺž‹ç›¸äş’é‡ĺ ďźŒćœ‰ć—śéšžäťĽé€ščż‡é€‰éĄš˜q›čĄŒĺˆ†çąťă€‚ĺ˝“ä˝ ĺžçŤ‹ć•°ćŽĺş“é“žćŽĽć—ÓžźŒä˝ éœ€čŚäťŽä¸‹é˘é€‰ĺ–ĺQ?

PublicĺQˆĺ…Źç”¨ďź‰(j¨Ş)或Private ĺQˆç§ćœ‰ďź‰(j¨Ş)链接

权限žc? Fixed UserĺQˆĺ›şĺŽšç”¨ćˆřPź‰(j¨Ş), Connected UserĺQˆčżžćŽĽç”¨ćˆřPź‰(j¨Ş)ćˆ?Current UserĺQˆĺ˝“ĺ‰ç”¨ćˆřPź‰(j¨Ş)
Shared LinkĺQˆĺ…ąäşŤé“žćŽĽďź‰(j¨Ş)ćˆ?Not Shared LinkĺQˆéžĺ…׃ín链接ĺQ?
每次创徏数据库链接时ĺQŒä˝ čŚč‡Şč§‰ä¸č‡Şč§‰ĺœ°ĺš˜q™ä¸‰żUé€‰ć‹Šă€?


公用链接与私有链接相ĺŻŇŽ(gu¨Š)Ż”

公用数据库链接对所有的数据库用户开放访问权。前面显˝Cşçš„ć˜Żç§ćœ‰ć•°ćŽĺş“é“žćŽĽĺQŒĺŽƒĺŞĺŻšĺťşçŤ‹ĺŽƒçš„ç”¨ćˆˇćŽˆćƒă€‚ĺ…Źç”¨ć•°ćŽĺş“é“žćŽĽć›´äؓ(f¨´)有用ĺQŒĺ› ä¸şĺŽƒä˝żä˝ ä¸ĺż…ä¸şćŻä¸€ä¸Şć˝œĺœ¨ç”¨ćˆˇĺˆ›ĺťşĺ•ç‹Źçš„é“žćŽĽă€‚äؓ(f¨´)äş?ji¨Łn)徏立一个公用数据库链接ĺQŒä‹É用如下显˝Cşçš„PUBLIC关键字:(x¨Ź)

CREATE PUBLIC DATABASE LINK GNIS
CONNECT TO GUEST IDENTIFIED BY WELCOME
USING 'GNIS';

即ä‹É˜q™ć˜Żä¸€ä¸Şĺ…Źç”¨é“žćŽĽďźŒç”¨ćˆˇĺäťć—§ĺ›şĺŽšă€‚ć‰€ćœ‰ä‹É用这个链接的用户都作为用户GUEST˜qžćŽĽĺˆ°čżœ˝E‹ć•°ćŽĺş“ă€?


使用数据库链接访问远˝E‹čĄ¨

ĺ›? 数据库链接GNISĺQŒćŒ‡ć˜Žç˝‘žlœćœĺŠĄĺżUŽÍźŒé“žćŽĽPROD事例到GNIS事例中的FEATURE_NAMES襨ă€?


权限žc?/p>

当你建立一个数据库链接ć—ÓžźŒĺ…łäşŽä˝ ĺŚ‚ä˝•ćŽˆćƒĺŻš˜qœç¨‹ć•°ćŽĺş“čż›čĄŒčŽżé—ŽďźŒćœ‰ä¸‰żUé€‰ć‹Šă€‚čż™ä¸‰ç§é€‰ć‹ŠäťŁčĄ¨äş?ji¨Łn)数据库链接的另一żUĺˆ†žcŔL–šćł•ă€‚čż™ä¸‰ç§žcŐdˆŤĺڂ䏋ĺQ?

固定用户。äؓ(f¨´)˜qœç¨‹ć•°ćŽĺş“é“žćŽĽćŒ‡ĺŽšç”¨ćˆˇĺĺ’ŒĺŁäť¤ďźŒä˝œäؓ(f¨´)数据库链接定义的一部分ă€?
˜qžćŽĽç”¨ćˆˇă€‚ĺœ¨ä¸ćŒ‡ĺŽšç”¨ćˆˇĺĺ’ŒĺŁäť¤ć—śĺˆ›ĺžçš„ć•°ćŽĺş“é“žćŽĽă€?
当前用户。徏立数据库链接ňqśćŒ‡ĺޚCURRENT_USER关键字ă€?
固定用户数据库链接是指在创徏链接时äؓ(f¨´)˜qœç¨‹ć•°ćŽĺş“ćŒ‡ĺŽšç”¨ćˆˇĺĺ’ŒĺŁäť¤ă€‚čż™ä¸€é“žćŽĽä¸çŽĄäť€äšˆć—śĺ€™ä‹É用,也无čŽř™°ä˝żç”¨ĺQŒéƒ˝ä˝żç”¨ç›¸ĺŒçš„ç”¨ćˆˇĺĺ’ŒĺŁäť¤ç™ťé™†ĺˆ°˜qœç¨‹ć•°ćŽĺş“ă€‚ĺˆ°ç›Žĺ‰ä¸şć­˘ä˝ ĺœ¨ćœŹć–‡ä¸­ć‰€çœ‹ĺˆ°çš„éƒ˝ć˜Żĺ›şĺŽšç”¨ćˆˇé“žćŽĽă€?

固定用户链接ĺQŒĺ°¤ĺ…ść˜Żĺ…Źç”¨ĺ›şĺŽšç”¨ćˆˇé“žćŽĽçš„ä¸€ä¸Şć˝œĺœ¨é—Žćć˜Żäť–äťŹćŠŠčżœ˝E‹çłťžlŸä¸Šçš„ĺŒä¸€ĺ¸ćˆˇžl™äş†(ji¨Łn)许多本地用户。从安全角度来说ĺQŒĺŚ‚ćžœć‰€ćœ‰çš„ćœŹĺœ°ç”¨ćˆˇĺœ¨čżœ˝E‹çłťžlŸä¸Šć‹Ľćœ‰ĺŒä¸€ä¸Şĺ¸ćˆřPźŒč´Łä“QžŽŢpځ㊘ä(sh¨´)¸­ĺQŒčż™ĺ–决于用ćˆďL(f¨Ľng)š„ć•°é‡ 。如果数据丢失,几乎不可能确定破坏是如何发生的。另一个潜在问题是公用固定用户链接žŽ†ĺŻš˜qœç¨‹ć•°ćŽĺş“çš„čމK—Žćƒçť™äş?ji¨Łn)所有的本地数据库用戗÷€?

如果你不惛_œ¨ć•°ćŽĺş“é“žćŽĽä¸­ĺľŒĺ…Ľç”¨ćˆˇĺĺ’ŒĺŁä×o(h¨´)ĺQŒOracle提供žl™ä˝ ĺŚä¸€ä¸Şéžĺ¸¸ćœ‰ç”¨çš„é€‰ć‹Šă€‚ä˝ ĺŻäťĽĺťşçŤ‹ä¸€ä¸ŞčżžćŽĽç”¨ćˆˇé“žćŽĽă€‚čżžćŽĽç”¨ćˆˇé“žćŽĽć˜Ż˜q™ć ˇçš„é“žćŽĽďźŒĺŽƒé€ščż‡äťÖM¸€ä¸Şć­Łĺœ¨ä‹É用该链接的本地数据库的用ćˆďL(f¨Ľng)š„ç”¨ćˆˇĺĺ’ŒĺŁä×o(h¨´)登陆到远˝E‹ć•°ćŽĺş“ă€‚ä˝ ĺŻäťĽé€ščż‡˝Ž€ĺ•ĺœ°˝Işĺ‡şç”¨ćˆˇĺĺ’ŒĺŁä×o(h¨´)来徏立一个连接用户链接。考虑如下定义ĺQ?

CREATE PUBLIC DATABASE LINK GNIS

USING 'GNIS';

链接名是GNIS。它˜qžćŽĽĺˆ°čżœ˝E‹ć•°ćŽĺş“˜qžćŽĽć—śä‹É用的ž|‘çťœćœĺŠĄĺç§°ć˜ŻGNISĺQŒä˝†ć˜Żć˛Ąćœ‰ćŒ‡ĺŽšç”¨ćˆˇĺĺ’ŒĺŁäť¤ă€‚ĺ˝“ä˝ ĺœ¨ćŸĽčŻ˘ä¸­ä‹É用这个链接时ĺQŒĺރ?y¨­u)Ž†ĺ‘˜qœç¨‹ć•°ćŽĺş“ĺ‘é€ä˝ ĺ˝“ĺ‰çš„ç”¨ćˆˇĺĺ’ŒĺŁäť¤ă€‚äž‹ĺŚ‚ďźŒĺŚ‚ćžœä˝ ä‹É用AHMAD/SECRET 登陆ĺˆîC˝ çš„ćœŹĺœ°ć•°ćŽĺş“ĺQŒé‚ŁäšˆAHMAD/SECRETžŽ†ć˜Żä˝ ç™ťé™†ĺˆ°˜qœç¨‹ć•°ćŽĺş“ć—śä˝żç”¨çš„ç”¨ćˆˇĺĺ’ŒĺŁäť¤ă€?

ä¸ŢZş†(ji¨Łn)使用一个连接用户链接,你必™ĺŐdœ¨˜qœç¨‹ć•°ćŽĺş“ä¸Šćœ‰ä¸€ä¸Şĺ¸ĺřPźŒäş?ji¨Łn)č§Ł˜q™ä¸€ç‚ŇŽ(gu¨Š)˜Żĺžˆé‡čŚçš„ă€‚ä¸ä˝†čż™ć řPźŒč€Œä¸”ä˝ ĺœ¨ä¸¤ä¸Şć•°ćŽĺş“ä¸Šĺş”ä‹É用同ć ďL(f¨Ľng)š„ç”¨ćˆˇĺ’ŒĺŁäť¤ă€‚ĺŚ‚ćžœćœŹĺœ°ç™ťé™†ä‹É用AHMAD/SECRETĺQŒé‚Łäšˆç™ťé™†ĺˆ°˜qœç¨‹ć•°ćŽĺş“ć—śäšŸĺż…™ĺÖM‹É用同ć ďL(f¨Ľng)š„ç”¨ćˆˇĺĺ’ŒĺŁä×o(h¨´)。ä‹É用连接用户链接时ĺQŒĺŚ‚ćžœä˝ çš„ĺŁäť¤ä¸ĺŒďźŒä˝ ĺ°ąć— ćƒç™ťé™†ă€?

公用˜qžćŽĽç”¨ćˆˇć•°ćŽĺş“é“žćŽĽĺ°¤ĺ…śćœ‰ç”¨ďźŒĺ› äؓ(f¨´)你可以徏立一个可被所有用戯‚Žżé—Žçš„é“žćŽĽĺQŒĺƈ且所有用戯‚˘Ťĺˆ†ĺˆŤä˝żç”¨äť–ćˆ–ĺĽšč‡ŞĺˇŢqš„ç”¨ćˆˇĺĺ’ŒĺŁä×o(h¨´)授权。你获得责ä“Q斚w˘çš„ĺˆŠç›ŠďźŒć˛Ąćœ‰žŽ†čżœ˝E‹ć•°ćŽĺş“ĺ‘ä˝ çš„ćœŹĺœ°ć•°ćŽĺş“ä¸Šçš„ćŻä¸€ä˝ç”¨ćˆˇĺź€ć”žă€‚äťŁäťäh˜Żä˝ ĺż…™ĺŐdœ¨ä¸¤ä¸Şć•°ćŽĺş“ä¸ŠĺťşçŤ‹ç”¨ćˆˇĺ¸ćˆˇĺQŒĺĆˆä¸”ä˝ ĺż…éœ€ź‹ŽäżĄĺŁä×o(h¨´)保持一致ă€?

当前用户链接通过使用CURRENT_USER关键字徏立ĺƈ且与˜qžćŽĽç”¨ćˆˇé“žćŽĽç›ć€źźă€‚ĺŞćœ‰ĺ˝“ä˝żç”¨Oracle Advanced Security OptionĺQˆOracle鍘çń”厉全选饚ĺQ‰ć—śĺQŒä˝ ć‰čƒ˝ä˝żç”¨ĺ˝“ĺ‰ç”¨ćˆˇé“žćŽĽĺQŒčż™ä¸Şé“žćŽĽĺŞĺŻŇŽ(gu¨Š)Žˆćƒä‹É用X.509认证的用ćˆähœ‰ç”¨ă€?


ĺ…׃ín链接

ĺ…׃ín数据库链接是指该链接的多个用户可以共享同一个底层网žlœčżžćŽĽă€‚äž‹ĺŚ‚ďźŒĺœ¨ćœ‰ĺ››ä˝ç”¨ćˆˇçš„MTSĺQˆĺ¤šžUżç¨‹ćœĺŠĄĺ™¨ďź‰(j¨Ş)环境下,每一个共享服务器˜q›ç¨‹éƒ˝ĺ°†ä¸Žčżœ˝E‹ćœĺŠĄĺ™¨ćœ‰ä¸€ä¸Şç‰Šç†é“žćŽĽďꌘq™ĺ››ä˝ç”¨ćˆˇĺ…ąäşŤčż™ä¸¤ä¸Şé“žćŽĽă€?
表面上,ĺ…׃ín链接乍一听č“vćĽĺƒć˜Żä¸€äťśĺĽ˝äş‹ă€‚ĺœ¨ćŸäş›çŽŻĺ˘ƒä¸‹çš„ź‹ŽĺŚ‚ć­¤ďźŒä˝†ć˜ŻĺQŒĺ˝“ä˝ č€ƒč™‘ä˝żç”¨ĺ…׃ín链接ć—ÓžźŒĺş”ĺ˝“ć„čŻ†ĺˆ°čż™ćœ‰čŽ¸ĺ¤šĺą€é™ć€§ĺ’Œč­Śĺ‘ŠĺQ?


如果你ä‹É用一个专用的服务器连接来˜qžćŽĽĺˆîC˝ çš„ćœŹĺœ°ć•°ćŽĺş“ĺQŒé“žćŽĽĺŞčƒ˝ĺœ¨ä˝ äťŽé‚Łäş›˜qžćŽĽä¸­ĺˆ›ĺťşçš„ĺ¤šé‡äź?x¨Ź)话间共享ă€?在MTS环境里,每一个共享服务器˜q›ç¨‹ć˝œĺœ¨ĺœ°ć‰“ĺź€ä¸€ä¸Şé“žćŽĽă€‚ć‰€ćœ‰çš„äź?x¨Ź)话被同一ĺ…׃ín服务器进˝E‹ćäž›ĺƈ且分享被那个˜q›ç¨‹ć‰“埀的ä“Q意共享链接。因为在MTS环境里的一个共享服务器˜q›ç¨‹čƒ˝ĺ¤ŸćœĺŠĄäşŽčŽ¸ĺ¤šç”¨ćˆŻ‚żžćŽĽďźŒĺ…׃ín链接的ä‹É用可能导致打开的链接远多于所必须的链接。用SHARED关键字徏立共享数据库链接。还必须使用AUTHENTICATED BY 子句在远˝E‹çłťžlŸä¸ŠćŒ‡ĺŽšä¸€ćœ‰ć•ˆçš„ç”¨ćˆˇĺĺ’ŒĺŁäť¤ă€‚ĺŚ‚ä¸‹ĺ‘˝äť¤ĺžçŤ‹ä¸€ä¸Şĺ…ąäşŤçš„ă€ĺ…Źç”¨çš„ă€čżžćŽĽç”¨ćˆäh•°ćŽĺş“铞掼ĺQ?


CREATE SHARED PUBLIC DATABASE LINK GNIS
AUTHENTICATED BY DUMMY_USER IDENTIFIED BY SECRET
USING 'GNIS';

要获得创建链接和˝ŽĄç†ĺˆ†ĺ¸ƒĺźçłťžlŸçš„更多辄料ĺQŒčŻˇćŸĽé˜…Oracle Technology Network (http://otn.oracle.com/)ă€?
使用AUTHENTICATED BY子句˝EĺžŽćœ‰äş›ĺ›°ć‰°ĺQŒä˝†ć˜Żç”ąäşŽĺŽžçŽ°ĺ…ąäşŤé“žćŽĽçš„ć–šĺźĺŽ‰ĺ…¨ć€§ĺ†łĺŽšĺŽƒć˜Żĺż…™ĺȝš„ă€‚čż™ä¸Şäž‹ĺ­ä¸­çš„ç”¨ćˆˇĺĺ’ŒĺŁäť¤DUMMY_USER/SECRET必须在远˝E‹çłťžlŸä¸Šćœ‰ć•ˆă€‚ç„śč€Œďꌘqœç¨‹žpȝťŸä¸Šä‹É用的帐户仍就是连接用ćˆďL(f¨Ľng)š„ĺ¸ćˆˇă€‚ĺŚ‚ćžœćˆ‘äťĽJEFF/SECRET登陆到我的本地数据库ňqśä‹É用我刚徏好的ĺ…׃ín链接ĺQŒĺ°†äź?x¨Ź)发生以下一žpŐdˆ—äş‹äšgĺQ?


ä¸ŢZş†(ji¨Łn)打开链接ĺQŒOracle使用DUMMY_USER/SECRET向远˝E‹ć•°ćŽĺş“ćŽˆćƒă€?然后ĺQŒOracle试图使用HMAD/SECRET使我登陆到远˝E‹ć•°ćŽĺş“ă€‚ĺ…ąäşŤé“žćŽĽçš„ä¸ťčŚç›Žçš„ć˜Żĺ‡žŽ‘ä¸¤ä¸Şć•°ćŽĺş“ćœĺŠĄĺ™¨äš‹é—´çš„ĺş•ĺą‚ž|‘çťœ˜qžćŽĽć•°é‡ă€‚ĺŽƒäťŹćœ€é€‚ĺˆäşŽMTS环境ĺQŒĺœ¨é‚Łä˝ ć‹Ľćœ‰ĺ¤§é‡çš„通过˜q™ä¸€é“žćŽĽčމK—Ž˜qœç¨‹ć•°ćŽĺş“çš„ç”¨ćˆˇă€‚č§‚ĺżľä¸ŠĺQŒä˝ ćƒŒ™ŽŠç”¨ćˆˇć•°é‡­‘…过ĺ…׃ín服务器进˝E‹çš„ć•°é‡ă€‚é‚Łäšˆä˝ ĺŻäťĽé€ščż‡ä¸şćŻä¸€ĺ…׃ín服务器进˝E‹ć‰“ĺź€ä¸€ä¸Şé“žćŽĽč€Œä¸ć˜ŻćŻä˝ç”¨ćˆäh‰“埀一个铞掼的ć–ŇŽ(gu¨Š)ł•ĺQŒčŠ‚çœčľ„ćşă€?


查找关于数据库链接的资料

你可以从几个数据字典视图中获得徏立好的数据库链接的资料。DBA_DB_LINKS视图为每一定义的链接返回一行。OWNER 列和DB_LINK列分别显˝CŢZş†(ji¨Łn)˜q™ä¸€é“žćŽĽçš„ć‰€ćœ‰č€…ĺŠ(qi¨˘ng)名称。对公用数据库链接,OWNER列将包含'PUBLIC'。如果你建立固定用户链接ĺQŒç”¨ćˆˇĺĺş”ĺœ¨DBA_DB_LINKS视图的USERNAME列里ĺQŒä˝†ć˜ŻĺŁäť¤ĺŞčƒ˝äťŽSYS.LINK$视图中看到。默认情况下ĺQŒĺŞćœ‰ĺ…ˇćœ‰SELECT ANY TABLEžpȝťŸćƒé™çš„DBA能够讉K—ŽSYS.LINK$视图查看口ä×o(h¨´)。你应该保护讉K—Žé‚Łä¸Şč§†ĺ›žçš„ćƒé™ă€‚ALL_DB_LINKS 视图ĺ’?USER_DB_LINKS视图ä¸?DBA_DB_LINKS视图相类äź?它们分别昄Ą¤şäş?ji¨Łn)你能够讉K—Žçš„ć‰€ćœ‰é“žćŽĽĺŠ(qi¨˘ng)你所拥有的全部链接。最后,V$DBLINK动态性能视图向你昄Ą¤şĺ‡ŢZ“Q意给定时间你-当前用户ĺQŒć‰“ĺź€çš„ĺ…¨éƒ¨ć•°ćŽĺş“é“žćŽĽă€?


全局性的数据库名żU?

在分布式环境里,Oracleĺťř™ŽŽä˝ çš„ć•°ćŽĺş“é“žćŽĽĺĺş”ä¸ŽĺŽƒäťŹ˜qžćŽĽĺˆ°çš„ć•°ćŽĺş“çš„ĺ…¨ĺą€ć€§ĺżU°ç›¸ĺŒšw…ă€‚ĺ› ć­¤ĺŚ‚ćžœä˝ ć­Łĺœ¨˜qžćŽĽĺˆ°ĺżUîCؓ(f¨´)GNIS.GENNICK.ORG的数据库ĺQŒä˝ ĺş”当žŽ†ä˝ çš„ć•°ćŽĺş“é“žćŽĽĺ‘˝ĺä¸şGNIS.GENNICK.ORG
为确定数据库的全局性名żUŽÍźŒäťĽSYSTEM登陆ňqśćŸĽčŻ˘GLOBAL_NAME视图ĺQ?


SQL> SELECT * FROM GLOBAL_NAME;

GLOBAL_NAME
_______________
GNIS.GENNICK.ORG

ç”׃şŽĺŽ†ĺ˛çš„ĺŽŸĺ› ďźŒéť˜čŽ¤ćƒ…ĺ†ľä¸‹ďźŒĺ…¨ĺą€ć€§ĺżUîC¸Žć•°ćŽĺş“é“žćŽĽĺżU°çš„äš‹é—´çš„é“žćŽĽä¸ć˜Żĺźşĺˆść€§çš„ă€‚ä¸˜q‡ďźŒä˝ ĺŻäťĽé€ščż‡čŽ„Ą˝ŽGLOBAL_NAMES的初始化参数为TRUE来改变这一行äؓ(f¨´)。例如:(x¨Ź)


SQL> SHOW PARAMETER GLOBAL_NAMES

NAME TYPE VALUE
________________________________________________________
global_names boolean TRUE

用于产生˜q™ä¸ŞčŒƒäž‹çš„äş‹äž‹čŚćą‚ä˝ ä˝żç”¨çš„ć•°ćŽĺş“é“žćŽĽĺďźŒĺż…éĄťä¸Žç›Žć ‡ć•°ćŽĺş“çš„ĺ…¨ĺą€ć€§ć•°ćŽĺş“ĺç§°ç›¸ĺŒšé…ă€‚ćł¨ć„ä¸Žä¸€äş›Oracle文ćĄŁä¸­čŻ´çš„ç›¸ĺďźŒĺ…łé”Žć˜Żä˝ çš„ćœŹĺœîCş‹äž‹çš„GLOBAL_NAMES讄Ą˝Žă€‚ĺŚ‚ćžœä˝ çš„ćœŹĺœîCş‹äž‹ä¸­GLOBAL_NAMES=FALSEĺQŒä˝ žŽŢpƒ˝ĺ¤Ÿä‹É用数据库链接ĺQŒč€Œä¸ç”¨çŽĄĺŽƒäťŹć˜ŻĺŚä¸Žčżœ˝E‹ć•°ćŽĺş“çš„ĺ…¨ĺą€ć€§ĺżU°ç›¸ĺŒšw…ă€‚ć€Čš„来说ĺQŒĺŚ‚ćžœä˝ čŽ„Ą˝ŽGLOBAL_NAMES=TRUEĺQŒä˝ ĺş”čŻĽĺœ¨ä˝ çš„ć‰€ćœ‰äş‹äž‹ä¸­ä¸€ĺž‹čż™äšˆĺšă€?br />



ĺşäh–‡ 2006-09-29 15:35 发表评论
]]>
some database objecthttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72757.htmlĺşäh–‡ĺşäh–‡Fri, 29 Sep 2006 03:31:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72757.htmlhttp://www.aygfsteel.com/kevinfriend/comments/72757.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/29/72757.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/72757.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/72757.html1 sequence
 (t¨Šng) 1) (t¨Šng) (t¨Šng) automatically generatess unique numbers
 (t¨Šng) (t¨Šng) is a sharable object
 (t¨Šng) (t¨Šng) is typically used to create a primary key value
 (t¨Šng) (t¨Šng) replaces applicaition code
 (t¨Šng) (t¨Šng) speeds up the efficiency of accessing sequence
 (t¨Šng) (t¨Šng) create sequence sequence
 (t¨Šng) (t¨Šng) [increment by n]
 (t¨Šng) (t¨Šng) [start with n]
 (t¨Šng) (t¨Šng) [{maxvalue n |nomaxvalue}]
 (t¨Šng) (t¨Šng) [{minvalue n |nominvalue}]
 (t¨Šng) (t¨Šng) [{cycle|nocycle}]
 (t¨Šng) (t¨Šng) [{cache n |nocache}]

 (t¨Šng) (t¨Šng) create sequence dept_deptin_seq
 (t¨Šng) (t¨Šng) increment by 10
 (t¨Šng) (t¨Šng) start with 120
 (t¨Šng) (t¨Šng) maxvalue 9999
 (t¨Šng) (t¨Šng) nocache
 (t¨Šng) (t¨Šng) nocycle
 (t¨Šng) 2) Confirming Sequences
 (t¨Šng) (t¨Šng) verify your sequence values in the user_sequences data dictionary table
 (t¨Šng) (t¨Šng) select sequence_name,min_value,max_value,increment_by,last_number
 (t¨Šng) (t¨Šng) from user_sequences;
 (t¨Šng) (t¨Šng) the last_number display the next available sequence number if nocache is specified
 (t¨Šng) 3)nextval and currval Pseudocolumns
 (t¨Šng) (t¨Šng) (t¨Šng) --nextval return thee next available sequence value,it return a unique value every time
it si referenced,even for different ueer;
 (t¨Šng) (t¨Šng) (t¨Šng) --currval obtains the current sequence value;
 (t¨Šng) (t¨Šng) (t¨Šng) --nextval must be issued for that sequence before curval contains a value;
 (t¨Šng) 4) Using a Sequence
 (t¨Šng) (t¨Šng) (t¨Šng) -- Caching sequence values in the memory give faster access to these values;
 (t¨Šng) (t¨Šng) (t¨Šng) -- Gaps in sequence value can occur when
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) a rollback occurs
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) b the system crashes
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) c A sequence us used in another table;
 (t¨Šng) (t¨Šng) 5) alter sequence test increment by 10;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) you can change all properties of the sequence except the start with .
 (t¨Šng) (t¨Šng) 6) remove sequence
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) drop sequence test;
2 index
 (t¨Šng) 1) how are indexes created
 (t¨Šng) (t¨Šng) Automatically : a unique index is created automatically when you create primary key or

unique constraint in a table definition,
 (t¨Šng) (t¨Šng) Manually: user can create nounique index on column to speed up access to the rows.
 (t¨Šng) (t¨Šng) create index testindex on autoer(lanme);
 (t¨Šng) 2) When to Create an index
 (t¨Šng) (t¨Šng) ypu should create an index if:
 (t¨Šng) (t¨Šng) . a column contains a wide range of values
 (t¨Šng) (t¨Šng) . a column contains a large number of null values
 (t¨Šng) (t¨Šng) . one or more columns are frequently used together in where clause or a join condition;
 (t¨Šng) (t¨Šng) . The table is large and most queries are expected to retrieve less than 2 to 4 percent

of the rows;
 (t¨Šng) (t¨Šng) 3) When not to create an index
 (t¨Šng) (t¨Šng) this usually not worth creating an index if:
 (t¨Šng) (t¨Šng) . the table is small
 (t¨Šng) (t¨Šng) . The columns are not often used as a condition in the query.
 (t¨Šng) (t¨Šng) . Most queries are expected to retrieve more than 2 to 4 percent of the rows in the

table
 (t¨Šng) (t¨Šng) . the indexed columns are referenced as part of an expression.
 (t¨Šng) (t¨Šng) 4)Confirming indexes
 (t¨Šng) (t¨Šng) (t¨Šng) . The user_indexes data dictionary view contains the name of the index and tis uniquess
 (t¨Šng) (t¨Šng) (t¨Šng) . the user_ind_columns view contains the index name,the table name,and the column name.
 (t¨Šng) (t¨Šng) (t¨Šng) select ic.index_name,ic_column_name,ic.column_position,ic_col_pos,ix.uniqueness
 (t¨Šng) (t¨Šng) (t¨Šng) from user_indexed ix,user_ind_columns ic
 (t¨Šng) (t¨Šng) (t¨Šng) where ic.index_name=ix.index_name
 (t¨Šng) (t¨Šng) (t¨Šng) and ic.table_name='employees';
 (t¨Šng) 5)ĺŸŢZşŽĺ‡˝ć•°çš„çƒŚ(ch¨ł)ĺź?br /> (t¨Šng) . a function-based index is an index based on expressions
 (t¨Šng) . The index expression is built form table columns,constraints,SQL functions and user-

defined functions
 (t¨Šng) (t¨Šng) create index testindex2
 (t¨Šng) (t¨Šng) on autors (upper(au_fname));
 (t¨Šng) (t¨Šng)
 (t¨Šng) (t¨Šng) select * from authors
 (t¨Šng) (t¨Šng) where upper(au_fname) like 'B%';
 (t¨Šng) 6) remoe index
 (t¨Šng) (t¨Šng) drop index index_name;
3 synonyms
 (t¨Šng) Simplify access to objects by creating a synonym
 (t¨Šng) (t¨Šng) . Ease referring to a table ownerd by anther user
 (t¨Šng) (t¨Šng) . Shorten lengthy object names;
 (t¨Šng) (t¨Šng) create [publi] synonym synonym for object;



ĺşäh–‡ 2006-09-29 11:31 发表评论
]]>
使用游标http://www.aygfsteel.com/kevinfriend/archive/2006/09/28/72593.htmlĺşäh–‡ĺşäh–‡Thu, 28 Sep 2006 07:32:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/28/72593.htmlhttp://www.aygfsteel.com/kevinfriend/comments/72593.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/28/72593.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/72593.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/72593.html (t¨Šng)1ĺQ?索引čĄ?br /> (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 erveroutput 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žçŽ‹(hu¨¤)的元ç´?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 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;

ĺşäh–‡ 2006-09-28 15:32 发表评论
]]>
createing viewhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/27/72367.htmlĺşäh–‡ĺşäh–‡Wed, 27 Sep 2006 10:30:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/27/72367.htmlhttp://www.aygfsteel.com/kevinfriend/comments/72367.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/27/72367.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/72367.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/72367.html1Why Use Views
 (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 distince 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) creaate 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)



ĺşäh–‡ 2006-09-27 18:30 发表评论
]]>
使用复合变量.http://www.aygfsteel.com/kevinfriend/archive/2006/09/27/72317.htmlĺşäh–‡ĺşäh–‡Wed, 27 Sep 2006 07:46:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/27/72317.htmlhttp://www.aygfsteel.com/kevinfriend/comments/72317.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/27/72317.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/72317.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/72317.html1 pl/sql 集合 处理单列多行数据库,使用的类型äؓ(f¨´)标量žcŐdž‹
 (t¨Šng)1ĺQ?索引čĄ?br /> (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 erveroutput 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žçŽ‹(hu¨¤)的元ç´?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 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;



ĺşäh–‡ 2006-09-27 15:46 发表评论
]]>
including constrainthttp://www.aygfsteel.com/kevinfriend/archive/2006/09/26/71973.htmlĺşäh–‡ĺşäh–‡Tue, 26 Sep 2006 06:44:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/26/71973.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71973.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/26/71973.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71973.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71973.html1 What are Constrains
 (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 satify
 (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'



ĺşäh–‡ 2006-09-26 14:44 发表评论
]]>
creating and manipulation tablehttp://www.aygfsteel.com/kevinfriend/archive/2006/09/25/71804.htmlĺşäh–‡ĺşäh–‡Mon, 25 Sep 2006 09:59:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/25/71804.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71804.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/25/71804.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71804.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71804.html1) User Tables:
 (t¨Šng) a Are a collection of tables created and maintained by the user
 (t¨Šng) b Contain user information
 (t¨Šng)2) Data Dictionary
 (t¨Šng) a is a collection of table created and maintained by the Oracle Server
 (t¨Šng) b Contain database information
2 Querying the Data Dictionary
 (t¨Šng)1)see the names of the table owned by the user
 (t¨Šng) (t¨Šng) select table_name from user_tables;
 (t¨Šng)2) view distinct object types ownered by the user
 (t¨Šng) (t¨Šng) select distinct object_type from user_object;
 (t¨Šng)3) view tables ,view ,synonyms and sequences owned by the user
 (t¨Šng) (t¨Šng) select * from user_catalog
3 Creating a Table by Ussing a Subquery Syntax
 (t¨Šng)create table tt3
 (t¨Šng)as
 (t¨Šng)select * from authors
4 Teh alter table Statement
 (t¨Šng)1) Add a new column
 (t¨Šng) alter table tt2
 (t¨Šng) add(fname varchar2(20) default 'unkonown',
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) address varchar2(30) null);
 (t¨Šng)2)Modigying a Column's data type size and default value
 (t¨Šng) alter table dept80
 (t¨Šng) modigy (last_name varchr2(30))
 (t¨Šng) A change to thee default value affects onlly subsequent insertion to the table
 (t¨Šng) 3) drop a column
 (t¨Šng) alter table dept80
 (t¨Šng) drop column job_id;
 (t¨Šng) The set unseed Option
 (t¨Šng) (t¨Šng) a you use the set unused optoin to mark one or more columns as unused
 (t¨Šng) (t¨Šng) b you use the drop unused colimns options to remove the columns that are marked as
 (t¨Šng) (t¨Šng) as unused
 (t¨Šng) (t¨Šng) alter table tt2
 (t¨Šng) (t¨Šng) set unused colun fnamel;
 (t¨Šng) (t¨Šng) alter table table
 (t¨Šng) (t¨Šng) drop unused columns
5 Dropping a Table
 (t¨Šng)1) All data and structure in the table is deleted
 (t¨Šng)2) Any pending transaction are committed
 (t¨Šng)3) All indexes are dropped
 (t¨Šng)4) You cannot roll back the drop table statement
6 Changing the Name of an Object
 (t¨Šng) rename dept to detail_dept;
 (t¨Šng) you must be the owner of the object
7 Truncate a Table
 (t¨Šng) Remove all rows from the table
 (t¨Šng) release the storage space used by that table
 (t¨Šng) you cannot rollback row when using truncate
 (t¨Šng) alternatly ,you can remove row by using delete statement


ĺşäh–‡ 2006-09-25 17:59 发表评论
]]>
Data manipulationhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/25/71781.htmlĺşäh–‡ĺşäh–‡Mon, 25 Sep 2006 08:25:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/25/71781.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71781.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/25/71781.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71781.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71781.html (t¨Šng) 1) A DML statement is executed when you:
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) add new rows to a table
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) modify existing row in a table
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) remove existing rows from a table
 (t¨Šng) 2) A transaction consist a collection dml statements form a logic unit of work
2 Using Explicit Default Values
 (t¨Šng)1) default with insert
 (t¨Šng)insert into departments
 (t¨Šng)values(200,'ddd',default)
 (t¨Šng)2) default with update
 (t¨Šng)update departments
 (t¨Šng)set manager_id=default where department_id=10
3 The Merge Statement
 (t¨Šng)1)Provide the ability to conditionaly update or insert data into database
 (t¨Šng)2)Perform a update if the row exists and an insert if it is a new row
 (t¨Šng) (t¨Šng) a Avoid update separatly
 (t¨Šng) (t¨Šng) b increase performance and ease of use
 (t¨Šng) (t¨Šng) c is useful in data warehousing application
 (t¨Šng) (t¨Šng) example
 (t¨Šng) (t¨Šng) (t¨Šng) merge into copy_emp c
 (t¨Šng) (t¨Šng) (t¨Šng) using employees e
 (t¨Šng) (t¨Šng) (t¨Šng) on (c.employee_id=e.employee_id)
 (t¨Šng) (t¨Šng) (t¨Šng) when mathched then
 (t¨Šng) (t¨Šng) (t¨Šng) update set
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) c.first_name=e.first_name
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) c.last_name=e.last_name
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ..............
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) c.department_id=e.department_id
 (t¨Šng) (t¨Šng) (t¨Šng) when not matched then
 (t¨Šng) (t¨Šng) (t¨Šng) insert values(e.employeeid,e.first_name,......e.department_id);
4 Database Transactions
 (t¨Šng)1)Begin when the first dml statement is executed
 (t¨Šng)2)end with one of the following events
 (t¨Šng) (t¨Šng) a a commit or rollback statement is issued;
 (t¨Šng) (t¨Šng) b a ddl or dcl statement execute (commit automatically)
 (t¨Šng) (t¨Šng) c the user exist isqllplus
 (t¨Šng) (t¨Šng) d the system crashes
 (t¨Šng)3) advantage of commit and rollback statemnt
 (t¨Šng) (t¨Šng) With commit and rollback statement ,you can
 (t¨Šng) (t¨Šng) (t¨Šng) a ensure data consistence
 (t¨Šng) (t¨Šng) (t¨Šng) b Preview data change before making change permant
 (t¨Šng) (t¨Šng) (t¨Šng) c group logic relate operatons
5 State of The Data Before commit or rollback
 (t¨Šng)1) the previous state of the data can be recovered
 (t¨Šng)2) The current user can review the result of the dml operation by using the select statment
 (t¨Šng)3) other user can not view the result of the dml
 (t¨Šng)4) the affected was locked ,other user cannot change the data within the affecteed row
6 Read Consistency
 (t¨Šng)1) Read consistency guarantees a consistent view of the data at all times
 (t¨Šng)2) Changes made by one user do not confilict with changes made by another user
 (t¨Šng)3) Read consistency ensures that on the same data
 (t¨Šng) (t¨Šng) (t¨Šng) a Readers do not wait for writers
 (t¨Šng) (t¨Šng) (t¨Šng) b Writers do not wait for readers
7Locking
 (t¨Šng) 1) Prevent destructive interaction between concurrent transactions
 (t¨Šng) 2) Reqire no user action
 (t¨Šng) 3) Automatically use the lowest level of restrictiveness
 (t¨Šng) 4) Are held for the duration of the transaction
 (t¨Šng) 5) Are of two types:explicit locking an implicit locking
8 Implicit Locking
 (t¨Šng) 1)Two lock modes
 (t¨Šng) (t¨Šng) (t¨Šng) a Exclusive :Locks out other users
 (t¨Šng) (t¨Šng) (t¨Šng) b Share: Allows other users to accesss
 (t¨Šng) 2)High level of data concurrency
 (t¨Šng) (t¨Šng) (t¨Šng) a DML:Table share,row exclusive
 (t¨Šng) (t¨Šng) (t¨Šng) b Queries: No locks required
 (t¨Šng) (t¨Šng) (t¨Šng) c DDL:Protects object definitions
 (t¨Šng) 3)Locks held until commit or rollback


ĺşäh–‡ 2006-09-25 16:25 发表评论
]]>
sequence 的用ćł?/title><link>http://www.aygfsteel.com/kevinfriend/archive/2006/09/24/71573.html</link><dc:creator>ĺşäh–‡</dc:creator><author>ĺşäh–‡</author><pubDate>Sun, 24 Sep 2006 08:08:00 GMT</pubDate><guid>http://www.aygfsteel.com/kevinfriend/archive/2006/09/24/71573.html</guid><wfw:comment>http://www.aygfsteel.com/kevinfriend/comments/71573.html</wfw:comment><comments>http://www.aygfsteel.com/kevinfriend/archive/2006/09/24/71573.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/kevinfriend/comments/commentRss/71573.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/kevinfriend/services/trackbacks/71573.html</trackback:ping><description><![CDATA[刚刚用sequence ĺQŒĺˆĺż˜ä(sh¨´)ş†(ji¨Łn)ĺQŒĺ‘ľĺ‘ľďźŒäťŽç˝‘上扞äş?ji¨Łn)一˝‹‡ć–‡çŤ ďźŒĺ†™çš„不错ĺQŒcopy 在这é‡?br />1、Create (t¨Šng)Sequence (t¨Šng) <br />你首先要有CREATE (t¨Šng)SEQUENCE或者CREATE (t¨Šng)ANY (t¨Šng)SEQUENCE权限ĺQŒÂ?<br />CREATE (t¨Šng)SEQUENCE (t¨Šng)emp_sequence (t¨Šng) <br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)I(y¨Şng)NCREMENT (t¨Šng)BY (t¨Šng)1 (t¨Šng) (t¨Šng)-- (t¨Šng)每次加几个Â?<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)START (t¨Šng)WITH (t¨Šng)1 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)-- (t¨Šng)äť?开始计敊W?<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)NOMAXVALUE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)-- (t¨Šng)不设ž|Žćœ€ĺ¤§ĺ€źÂ?<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)NOCYCLE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)-- (t¨Šng)一直篏加,不ĺó@环Â?<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)CACHE (t¨Šng)10; (t¨Šng) <br /><br />一旦定义了(ji¨Łn)emp_sequenceĺQŒä˝ žŽąĺŻäťĽç”¨CURRVALĺQŒNEXTVAL (t¨Šng) <br /> (t¨Šng)CURRVAL=˜q”回 (t¨Šng)sequence的当前值Â?<br /> (t¨Šng)NEXTVAL=增加sequence的ĺ€ű|źŒç„śĺŽ˜q”回 (t¨Šng)sequence (t¨Šng)值Â?<br />比如ĺQšÂ?<br /> (t¨Šng) (t¨Šng)emp_sequence.CURRVAL (t¨Šng) <br /> (t¨Šng) (t¨Šng)emp_sequence.NEXTVAL (t¨Šng) <br /><br />可以使用sequence的地方:(x¨Ź) (t¨Šng) <br />- (t¨Šng)不包含子查询、snapshot、VIEW的Â?t¨Šng)SELECT (t¨Šng)语句 (t¨Šng) <br />- (t¨Šng)I(y¨Şng)NSERT语句的子查询中Â?<br />- (t¨Šng)NSERT语句的VALUES中Â?<br />- (t¨Šng)UPDATE (t¨Šng)的Â?t¨Šng)SET中Â?t¨Šng) (t¨Šng)?<br /><br />可以看如下例子:(x¨Ź) (t¨Šng) <br />INSERT (t¨Šng)I(y¨Şng)NTO (t¨Šng)emp (t¨Šng)VALUES (t¨Šng) (t¨Šng) <br />(empseq.nextval, (t¨Šng)'LEWIS', (t¨Šng)'CLERK',7902, (t¨Šng)SYSDATE, (t¨Šng)1200, (t¨Šng)NULL, (t¨Šng)20); (t¨Šng) <br /><br />SELECT (t¨Šng)empseq.currval (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)FROM (t¨Šng)DUAL; (t¨Šng) <br /><br />但是要注意的是:(x¨Ź) (t¨Šng) <br />- (t¨Šng)˝WŹä¸€‹ĆĄNEXTVAL˜q”ĺ›žçš„ć˜Żĺˆĺ§‹ĺ€ű|ź›éšĺŽçš„NEXTVALäź?x¨Ź)自动增加你定义的INCREMENT (t¨Šng)BYĺ€ű|źŒç„śĺŽ˜q”回增加后的倚{€‚CURRVAL (t¨Šng)ć€ŔL˜Ż˜q”ĺ›žĺ˝“ĺ‰SEQUENCE的ĺ€ű|źŒä˝†ć˜Żĺœ¨çŹŹä¸€‹ĆĄNEXTVAL初始化之后才能ä‹É用CURRVALĺQŒĺŚĺˆ™äźš(x¨Ź)出错。一‹ĆĄNEXTVALäź?x¨Ź)增加一‹ĆĄSEQUENCE的ĺ€ű|źŒć‰€äťĽĺŚ‚ćžœä˝ ĺœ¨ĺŒä¸€ä¸ŞčŻ­ĺĽé‡Œé˘ä‹É用多个NEXTVALĺQŒĺ…śĺ€źĺ°ąć˜Żä¸ä¸€ć ďL(f¨Ľng)š„ă€‚ć˜Žç™˝ďźŸÂ (t¨Šng) <br /><br />- (t¨Šng)如果指定CACHEĺ€ű|źŒORACLEžŽąĺŻäťĽé˘„ĺ…ˆĺœ¨ĺ†…ĺ­˜é‡Œé˘ć”„Ą˝Žä¸€äş›sequenceĺQŒčż™ć ˇĺ­˜ĺ–的忍些。cache里面的取完后ĺQ?a class="channel_keylink" >oracle</a>自动再取一žl„ĺˆ°cache。Â?t¨Šng)ä‹É用cache或许äź?x¨Ź)蟩ĺřPźŒÂ (t¨Šng)比如数据库突然不正常down掉(shutdown (t¨Šng)abort),cache中的sequencežŽ×ƒźš(x¨Ź)丢夹. (t¨Šng)所以可以在create (t¨Šng)sequence的时候用nocache防止˜q™ç§ćƒ…冾。Â?<br /><br />2、Alter (t¨Šng)Sequence (t¨Šng) <br />你或者是该sequence的ownerĺQŒćˆ–č€…ćœ‰ALTER (t¨Šng)ANY (t¨Šng)SEQUENCE (t¨Šng)权限才能改动sequence. (t¨Šng)可以alter除start至以外的所有sequence参数.如果惌™Ść”šĺ˜startĺ€ű|źŒĺż…饝 (t¨Šng)drop (t¨Šng) (t¨Šng)sequence (t¨Šng)再Â?t¨Šng)re-create (t¨Šng). (t¨Šng) <br />Alter (t¨Šng)sequence (t¨Šng)的例子Â?<br />ALTER (t¨Šng)SEQUENCE (t¨Šng)emp_sequence (t¨Šng) <br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)I(y¨Şng)NCREMENT (t¨Šng)BY (t¨Šng)10 (t¨Šng) <br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)MAXVALUE (t¨Šng)10000 (t¨Šng) <br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)CYCLE (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)-- (t¨Šng)ĺˆ?0000后从头开始Â?<br /> (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)NOCACHE (t¨Šng); (t¨Šng) <br /><br /><br />影响Sequence的初始化参数ĺQšÂ?<br />SEQUENCE_CACHE_ENTRIES (t¨Šng)=讄Ą˝Žčƒ˝ĺŒć—śč˘Ťcache的sequence数目。Â?t¨Šng)?<br /><br />可以很简单的Drop (t¨Šng)Sequence (t¨Šng) <br />DROP (t¨Šng)SEQUENCE (t¨Šng)order_seq; (t¨Šng) <br /><img src ="http://www.aygfsteel.com/kevinfriend/aggbug/71573.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-09-24 16:08 <a href="http://www.aygfsteel.com/kevinfriend/archive/2006/09/24/71573.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>subquerieshttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71344.htmlĺşäh–‡ĺşäh–‡Fri, 22 Sep 2006 08:25:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71344.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71344.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71344.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71344.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71344.html1 Guidelines for Using Subqueries
 (t¨Šng)a Enclose subqueries in parenttheses
 (t¨Šng)b placce subqueries on the right side of the comparision condition
 (t¨Šng)c the order by clause in the subquery is not needed
 (t¨Šng)d using single-row operators with single-row subqueries and use multiple -row operator with multiple-row subqueries .
 (t¨Šng) single-row subqueries can work as a expression,and muitiple-row subqueries can only be used with in all any ,i will talk it later
 (t¨Šng) select last_name where job_idd=(select job_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) (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) (t¨Šng) (t¨Šng) (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 imployee_id=141)
2 The HAVING CLause with Subqueries
 (t¨Šng)a The Oracle server execute subqueries first
 (t¨Šng)b The Oracle return result into the HAVING clause of the main query
 (t¨Šng) select department_id,min(salary)
 (t¨Šng) from employee
 (t¨Šng) group by department_id
 (t¨Šng) having min(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) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (select min(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) (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) (t¨Šng) (t¨Šng) where department_id=50);
3 Multiple-Row Subqueries
 (t¨Šng) a Return (t¨Šng) more than one row
 (t¨Šng) Using mutiple-row comparsion operator
 (t¨Šng) select employee_id
 (t¨Šng) from employees
 (t¨Šng) where salary<any
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (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 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) (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 job_id='ddd')

 (t¨Šng) select employee_id
 (t¨Šng) from employees
 (t¨Šng) where salary<all
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (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 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) (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 job_id='ddd')
 (t¨Šng) (t¨Šng) select emp.last_name
 (t¨Šng) (t¨Šng) from employees emp
 (t¨Šng) (t¨Šng) where emp.employee_id not 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) (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 mgr.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) (t¨Šng) (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 mgr)



ĺşäh–‡ 2006-09-22 16:25 发表评论
]]>
jointablehttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71323.htmlĺşäh–‡ĺşäh–‡Fri, 22 Sep 2006 06:55:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71323.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71323.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71323.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71323.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71323.htmlUsing a join to query data form more than one table
select table1.column,table2,column
from table1,table2
where table1.column1=table2.column2 .
2 outjoin
 (t¨Šng)1)You use an outer join to also see rows that do not meet the join condition
 (t¨Šng)2)The Outer join operator is the plus sign(+)
 (t¨Šng) (t¨Šng) a left join
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select tabl1.column,table2,column
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from table1,table2
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where table1.column(+)=table2.column
 (t¨Šng) b (t¨Šng) right join
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select table1.column,table2.column
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from table1,table2
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) wheretable1.coulmn=table2.column(+)
 (t¨Šng) 3) self join
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select worker.last_name||'works for'||manager.last_name
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from (t¨Šng) employees owrker,employees manager
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) where worker.manager_id=manager.employee_id;
 (t¨Šng)
3 Joining Tables Using SQL:1999 Syntax
 (t¨Šng) Use a join to query data from more than one table
 (t¨Šng) 1) Creationg Cross Joins
 (t¨Šng) (t¨Šng) a The cross join clause produces thee cross product of two tables
 (t¨Šng) (t¨Šng) b This is the same as Cartesian product between the two tables
 (t¨Šng) (t¨Šng) select last_name,department_name
 (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng) ccross join departments
 (t¨Šng) 2) Creating Natual Joins
 (t¨Šng) (t¨Šng) a The Natual join clause is bassed on all columns in the two tables that have the same name
 (t¨Šng) (t¨Šng) b it select rows from the two tables that have the equal values in all matched columns
 (t¨Šng) (t¨Šng) c if the columns having the same name and have the different data types in an error is returned.
 (t¨Šng) (t¨Šng) select department_id,department_name,location_id,city
 (t¨Šng) (t¨Šng) from departments
 (t¨Šng) (t¨Šng) natual join locations
 (t¨Šng)3) using clause
 (t¨Šng) (t¨Šng) select e.employee_id,e.last_name
 (t¨Šng) (t¨Šng) from employees e join departments d
 (t¨Šng) (t¨Šng) using (department_id);
 (t¨Šng)4) Creating joins with thee on clause
 (t¨Šng) a The join condition for thee natual join is basically an equaljoin of all column with the same name.
 (t¨Šng) b To specify arbitrary condition or specify columns to join, the on clause is userd
 (t¨Šng) c The join condition is separated from other search conditions
 (t¨Šng) d The on claus make code easy to understand.
 (t¨Šng) select e.employee_id,e.last_name,e.department_id,
 (t¨Šng) from employees e join departments d
 (t¨Šng) on (e.department_id=d.department_id);
 (t¨Šng)
 (t¨Šng) from employe
 (t¨Šng) join departments d
 (t¨Šng) on d.department_id=e.department_id
 (t¨Šng) join locations l
 (t¨Šng) on d.location_id=l.location_id
 (t¨Šng)5) INNER Versus OuTER Joins
 (t¨Šng) a In SQL:1999,the join of two tables returning only matched rows is an inner join
 (t¨Šng)6) FULL OUTER JOIN
 (t¨Šng) select e.last_name,e,department_id,d.department_name
 (t¨Šng) from employees e
 (t¨Šng) full outer join departments d
 (t¨Šng) on (e.department_id=d.department_id);
 (t¨Šng)

ĺşäh–‡ 2006-09-22 14:55 发表评论
]]>
sql functionhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71279.htmlĺşäh–‡ĺşäh–‡Fri, 22 Sep 2006 03:50:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71279.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71279.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71279.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71279.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71279.html1 "'
2 ||
3 isql*plus (t¨Šng) http://127.0.00.1/isqlplus
4 desc author
5 initcap('SQL Course')
 (t¨Šng) INSERT('JellwWord','W') (t¨Šng) 6
 (t¨Šng) (t¨Šng) LPAD (salary,10,'*') (t¨Šng) *****24000
 (t¨Šng) (t¨Šng) RPAD (salary,10,'*') (t¨Šng) 24000*****
 (t¨Šng) (t¨Šng) TRIM ('H' from 'HolloWorld') olloWord
 (t¨Šng) (t¨Šng) substr('helloword',1,5) (t¨Šng) hello
 (t¨Šng) (t¨Šng) substr('helloword',-1,5) oword
6 (t¨Šng) Number Functions
 (t¨Šng) (t¨Šng) round(45.926,2) (t¨Šng) (t¨Šng) 45.93
 (t¨Šng) (t¨Šng) round(45.926,-2) 0
 (t¨Šng) (t¨Šng) round(55.926,-2) 100
 (t¨Šng) (t¨Šng) trunc(45.926,2) (t¨Šng) (t¨Šng) 45.92
 (t¨Šng) (t¨Šng) mod(1600,300) (t¨Šng) 100
7 data function
 (t¨Šng) (t¨Šng) systdate
 (t¨Šng) (t¨Šng) (sysdate-hire_date)/7 as weeks
 (t¨Šng) (t¨Šng) months_between (t¨Šng) number of months between two dates
 (t¨Šng) (t¨Šng) months_between ('01-sep-95','11,jan-94') (t¨Šng) 19.6774194
 (t¨Šng) (t¨Šng) add_months (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) add calendar months to date
 (t¨Šng) (t¨Šng) add_months('11-JAN-94',6) (t¨Šng) '11-JUL-94'
 (t¨Šng) (t¨Šng) next_day (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) next day of the date specified
 (t¨Šng) (t¨Šng) next_day('01-SEP-95','FRIDAY') '08-SEP-95'
 (t¨Šng) (t¨Šng) last_day (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) last day of the month
 (t¨Šng) (t¨Šng) last_day('01-feb-95') (t¨Šng) '28-feb-95'
 (t¨Šng) (t¨Šng) round (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) round date
 (t¨Šng) (t¨Šng) assume sysdate='25-jul-95'
 (t¨Šng) (t¨Šng) round(sysdate,'month') 01-aug-95
 (t¨Šng) (t¨Šng) round(sysdate,'year') (t¨Šng) 01-JAN-96
 (t¨Šng) (t¨Šng) trunc (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) truncate date
 (t¨Šng) (t¨Šng) trunc(sysdate,'month') 01-Jul-95
 (t¨Šng) (t¨Šng) trunc(sysdate,'month') 01-JAN-95
8 (t¨Šng) Conversion Functions (t¨Šng)
 (t¨Šng) 1) implicit data typ conversion
 (t¨Šng) (t¨Šng) varchar2 or char (t¨Šng) ---number
 (t¨Šng) (t¨Šng) varchar2 or char (t¨Šng) ---date
 (t¨Šng) (t¨Šng) numbeer (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ---varchar2
 (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) ---varchar2
 (t¨Šng) 2) to_char(date,'format')
 (t¨Šng) format:
 (t¨Šng) (t¨Šng) (t¨Šng) YYYY Full year in numbers
 (t¨Šng) (t¨Šng) (t¨Šng) YEAR Year spelled out
 (t¨Šng) (t¨Šng) (t¨Šng) MM (t¨Šng) (t¨Šng) Two-digit value for month
 (t¨Šng) (t¨Šng) (t¨Šng) MONTH Full name of the month
 (t¨Šng) (t¨Šng) (t¨Šng) MON (t¨Šng) THree-letter abbreviation of the month
 (t¨Šng) (t¨Šng) (t¨Šng) DY (t¨Šng) (t¨Šng) Three-letter abbreviation of the day of the week
 (t¨Šng) (t¨Šng) (t¨Šng) DAY (t¨Šng) Full name of the day of hte week
 (t¨Šng) (t¨Šng) (t¨Šng) DD (t¨Šng) (t¨Šng) Numberic day of the month
 (t¨Šng) (t¨Šng) (t¨Šng) HH24:MI:SS AM (t¨Šng) 15:45:32:PM
 (t¨Šng) (t¨Šng) (t¨Šng) DD "of" (t¨Šng) MONTH 12 of october
 (t¨Šng) 3) to_char function with number (t¨Šng)
 (t¨Šng) (t¨Šng) TO_CAHR(number,'format_model')
 (t¨Šng) (t¨Šng) These are some of the format elements you can use with the to_char function to display number as a character.
 (t¨Šng) (t¨Šng) (t¨Šng) 9 Reqresents a number
 (t¨Šng) (t¨Šng) (t¨Šng) 0 Forces a zero to be displayed
 (t¨Šng) (t¨Šng) (t¨Šng) $ Places a flationg dollar sign
 (t¨Šng) (t¨Šng) (t¨Šng) L Uses the floating local currency symbol
 (t¨Šng) (t¨Šng) (t¨Šng) . Prints a decimal point
 (t¨Šng) (t¨Šng) (t¨Šng) , Print a thousand director
 (t¨Šng) select to_char(qtym,"$999.99")
 (t¨Šng) 4) Using t_number and to _date functions
 (t¨Šng) (t¨Šng) a converting a character string to a number format using to_number function
 (t¨Šng) (t¨Šng) to_number(char,"format")l
 (t¨Šng) (t¨Šng) b converting a character string to a date format
 (t¨Šng) (t¨Šng) to_date(char,"format")
5 Nesting Functions
.Single-row function can be nested to many level
.Nested function can be evaluated from deepest level
6General Function
These function work with any data type and pertain to using nulls
nvl(expr1,expr2);
nvl2(expr1,expr2,expr3)
nullif(expr1,expr2)
coalesce(expr1,expr2,,,,exprn)
 (t¨Šng)1) nvl function
 (t¨Šng)convert a null to an actual function
 (t¨Šng)a Data type can be used are data character and number
 (t¨Šng)b Data types must match (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng)
 (t¨Šng)(set wrap off
 (t¨Šng) set line 1000
 (t¨Šng))
 (t¨Šng)2)Using the COALESCE Function
 (t¨Šng)a The advantage of the coalesce function over nal function is that coalesce function can take multiple alternative value
 (t¨Šng)b If the first value is not null, it return that expression,otherwise,it does a coalesce of remaining expressions
6 Conditional Expressions
 (t¨Šng)a Provide the use of if-then-else logic
 (t¨Šng)b use two methods: case expression decode function
 (t¨Šng) select last_name,job_id,salary,
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) case job_id when 'it' then 1*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) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when 'manager' then 1.2*salary
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) else salary end;
 (t¨Šng) from employee.

 (t¨Šng) select last_namek,job_id,salary,
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) decode(job_id,'it' ,1*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) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) 'manager',1.2*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) salary)
 (t¨Šng) (t¨Šng) from employees
 (t¨Šng) (t¨Šng)



ĺşäh–‡ 2006-09-22 11:50 发表评论
]]>
Aggregating Datas Using Group Functionbs.http://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71278.htmlĺşäh–‡ĺşäh–‡Fri, 22 Sep 2006 03:49:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71278.htmlhttp://www.aygfsteel.com/kevinfriend/comments/71278.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/09/22/71278.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/71278.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/71278.html1 What Are Group Functions
Group functions operatee on sets of rows to give one result per group
 (t¨Šng)1)agg,count,max,min,stddev,sum,variance
 (t¨Šng)select avg(salary),max(salary),min(salary),sum(salary)
 (t¨Šng)from employees
 (t¨Šng)where job_id like '%REP%'

 (t¨Šng)select count(*) from
 (t¨Šng)select count(address) from authors
 (t¨Šng)count the valid count of the address (exclude the null value)
 (t¨Šng)2) Using theDISTINCT Keyword
 (t¨Šng) count(distinct expr) return thee number of the distinct non-null value of the expr
 (t¨Šng) select count(distincee department_id) from employees
 (t¨Šng)3)Group functions and null values
 (t¨Šng) group functions ignore null values in the clumn
 (t¨Šng)4) Using thee NVL Function with Group Functions
 (t¨Šng) The nul function force group funtion to include null values
 (t¨Šng) select avg(nvl(commission_pct,0)) from employees
2 Creating Groups of Data
 (t¨Šng) 1)
 (t¨Šng) a Divide rows in a table into smaller groups by using the group by clause
 (t¨Šng) b All coulmns in the select list that are not in group function must be in the group by clause
 (t¨Šng) select department_id,avg(salary)
 (t¨Šng) from employees
 (t¨Šng) group by department_id;
 (t¨Šng) 2) Grouping by More Than One Column
 (t¨Šng) 3) Ilegal Queries Using Group Functions
 (t¨Šng) (t¨Šng) a You cannot use thee where clause to restrict groups
 (t¨Šng) (t¨Šng) b You use thee having clause to restrict groups
 (t¨Šng) (t¨Šng) c you cannot use group functions in the where clause
 (t¨Šng) 4)Excluding Group Resdults:The Having Clause
 (t¨Šng) (t¨Šng) Use the HAVING clause to restrict groups
 (t¨Šng) (t¨Šng) a Rows are grouped
 (t¨Šng) (t¨Šng) b The group functions is applied
 (t¨Šng) (t¨Šng) c Groups matcching the Having clause are display
 (t¨Šng) select department_id,max(salary)
 (t¨Šng) from employees
 (t¨Šng) group by department_id
 (t¨Šng) having max(salary)>10000
 (t¨Šng)5) Nesting Group function
 (t¨Šng)select max(avg(salary))
 (t¨Šng)from employees
 (t¨Šng)group by department_id;



ĺşäh–‡ 2006-09-22 11:49 发表评论
]]>
oracle žl“构莞莥http://www.aygfsteel.com/kevinfriend/archive/2006/08/11/62932.htmlĺşäh–‡ĺşäh–‡Fri, 11 Aug 2006 02:47:00 GMThttp://www.aygfsteel.com/kevinfriend/archive/2006/08/11/62932.htmlhttp://www.aygfsteel.com/kevinfriend/comments/62932.htmlhttp://www.aygfsteel.com/kevinfriend/archive/2006/08/11/62932.html#Feedback0http://www.aygfsteel.com/kevinfriend/comments/commentRss/62932.htmlhttp://www.aygfsteel.com/kevinfriend/services/trackbacks/62932.html (t¨Šng) 1 ˝Ž€ĺ•ćĄäťśĺˆ¤ć–?br /> (t¨Šng) declare
 (t¨Šng) (t¨Šng) v_sal number(6,2);
 (t¨Šng) begin
 (t¨Šng) (t¨Šng) select sal into v_sal from emp
 (t¨Šng) (t¨Šng) where lower(ename)=lower('&&name');
 (t¨Šng) (t¨Šng) if v_sal<2000 then
 (t¨Šng) (t¨Šng) (t¨Šng) update emp set sal=v_val+200
 (t¨Šng) (t¨Šng) (t¨Šng) where lower(ename)=lower('&name');
 (t¨Šng) (t¨Šng) end if;
 (t¨Šng) end;
 (t¨Šng)2 二重条äšg分支
 (t¨Šng) if v_comm<>0 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) .....
 (t¨Šng) else
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ........
 (t¨Šng) end if;
 (t¨Šng)3 多重条äšg分支
 (t¨Šng) IF (t¨Šng) (t¨Šng) THEN
 (t¨Šng) ELSIF (t¨Šng) (t¨Šng) THEN
 (t¨Šng) ELSIF (t¨Šng) (t¨Šng) THEN
 (t¨Šng) ELSE
 (t¨Šng) END IF;
2 case 语句
 (t¨Šng) 1 在case 语句中ä‹É用单一选择˝WŚčż›čĄŒç­‰ĺ€źćŻ”čž?br /> (t¨Šng) (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_deptno emp.deptno%type
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) v_deptno:=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) case v_deptno
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when 10 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update emp...
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) when 20 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update ......
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) else
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dems_out.put_line('不存在该部门');
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end case;
 (t¨Šng) (t¨Šng) (t¨Šng) end;
 (t¨Šng) 2 在case 语句中ä‹É用多żUćŻ”čžƒćĄäť?br /> (t¨Šng) (t¨Šng) (t¨Šng) declare
 (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) v_ename emp.ename%type
 (t¨Šng) (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) select ename ,sal into vv_ename,v_sal
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) from emp where empno=&no;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) case
 (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) update emp set ...
 (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) end case;
3 循环语句
 (t¨Šng) 1 基本循环
 (t¨Šng) (t¨Šng) declare
 (t¨Šng) (t¨Šng) (t¨Šng) i INT:=1;
 (t¨Šng) (t¨Šng) begin
 (t¨Šng) (t¨Šng) (t¨Šng) loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into temp valuse(1);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) exit when i=10;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) i:=i+1;
 (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) (t¨Šng) end;
 (t¨Šng) 2 while 循环
 (t¨Šng) (t¨Šng) (t¨Šng) while i<=10 loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into tem valuse(i);
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) i:=i+1;
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) end loop;
 (t¨Šng) 3for 循环
 (t¨Šng) (t¨Šng) (t¨Šng) for i in reverse 1..10 loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) insert into temp values(1);
 (t¨Šng) (t¨Šng) (t¨Šng) end loop;
4 ™ĺşĺşćŽ§ĺˆśčŻ­ĺĽ
 (t¨Šng) 1 goto
 (t¨Šng) (t¨Šng) (t¨Šng) goto label_name;
 (t¨Šng) (t¨Šng) (t¨Šng) loop
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) ...
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) goto end_loop;
 (t¨Šng) (t¨Šng) (t¨Šng) <<end loop>>
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) dbms_output
 (t¨Šng) (t¨Šng) 2 null 语句不会(x¨Ź)执行äťÖM˝•ć“ä˝œĺQŒĺƈ且会(x¨Ź)直接žŽ†ćŽ§ĺˆśäź é€’é“ä¸‹ä¸€ćĄčŻ­ĺ?br /> (t¨Šng) (t¨Šng) (t¨Šng) if v_sal<3000 then
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) update emp set .....
 (t¨Šng) (t¨Šng) (t¨Šng) else
 (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) (t¨Šng) null;
 (t¨Šng) (t¨Šng) (t¨Šng) end if;

ĺşäh–‡ 2006-08-11 10:47 发表评论
]]>
Ö÷ŐžÖŠÖëłŘÄŁ°ĺŁş ˝çĘ×ĘĐ| Ô­ŃôĎŘ| ÁŮşŁĘĐ| ÍŠ°ŘĎŘ| ŃôˡĎŘ| ÍňÄţĘĐ| żŚĘ˛ĘĐ| ÓńťˇĎŘ| ÍňʢÇř| ĚĆÉ˝ĘĐ| źĂÄţĘĐ| ÂŢś¨ĘĐ| żŞ˝­ĎŘ| Ěěף| É˝ľ¤ĎŘ| šţ°ÍşÓĎŘ| ͨľŔ| ÂŢś¨ĘĐ| ËÉĎŞĎŘ| šÝĚŐĎŘ| ¸ŁČŞĘĐ| ťŞČÝĎŘ| ÁéĘŻĎŘ| °˘ÂłżĆśűÇßĆě| ̨ǰĎŘ| ÉŰÎäĘĐ| şÓÎ÷Çř| ŃÎͤĎŘ| ן¸ńśűĆě| ÂŢ˝­ĎŘ| ťëÔ´ĎŘ| É˝śŤĘĄ| żľÂíĎŘ| ÓŔżľĘĐ| ̨ąąĎŘ| śÁĘé| ¸ŁÖÝĘĐ| ĎÉžÓĎŘ| ťĆĘŻĘĐ| ˇöˇçĎŘ| а͜űť˘ÓŇĆě|