??xml version="1.0" encoding="utf-8" standalone="yes"?>在线看的你懂得,国产精品久久国产精品,北岛玲日韩精品一区二区三区http://www.aygfsteel.com/kevinfriend/zh-cnThu, 03 Jul 2025 00:34:24 GMTThu, 03 Jul 2025 00:34:24 GMT60Manager Userhttp://www.aygfsteel.com/kevinfriend/archive/2006/10/12/74716.htmlhhThu, 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
 a schema is a named collection of objects
 b user is created and a corresponding schema is created
 c user can be associated only with one schema
 d username and schema are often userd interchangely.
2 Checklist for creating users
  a idntfigy tablespaces in which the usr nedds to store objects
  b decide on quotas for each tablespace
  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.
  d create user
  e grant privileges and roles to user
  desc dba_users;
  select * from dba_users;
3 Creating a new user:
Database Authentiacation
 set the initial password
  create user aaron
  identified by soccer
  default tablespace data
  temporary tablespace temp
  guota 15m on data
  password expire;

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

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



h 2006-10-12 09:49 发表评论
]]>
Using SET Operators.http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74565.htmlhhWed, 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
 The union operator returns results form both queries after eliminating duplications.\
 select employee_id,job_id
 from employees
 uniion
 select employ_id ,job_id
 from job_history;
2 the all operator
 The union all opertor reutrn result from both queries,including all duplications
3 interset
 select ster_id,qty from sales where qty>20;
 intersect
 select ster_id,qty from sales where ster_id like '7%';
4 minus
 select ster_id,qty from sales where qty>20
 minus
 select ster_id from sales where ster_id like '7%'

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

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



h 2006-10-11 14:51 发表评论
]]>
开发子q程http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74564.htmlhhWed, 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 1  开发过E?br /> 1) 建立q程Q不带参?br />  create or replace procedure out_time
  is
  begin
    dbms_output.put_line(systimestamp);
  end;
  a 使用execute 命o调用q程
   set  serveroutput on
   exec out_time;
  b 使用call 命o调用q程
   set serveroutput on
   call out_time();
 2) 建立q程Q带有in参数 默认入参敎ͼ另外也可以用in 关键子显C的定义
   create or replace procedure add_employee
   (eno number,name varchar2,sal number,
    job varchar default 'clerk',dno number)
   is
   e_integrity exception;
   pragma exception_init(e_intgegrity,-2291);
   begin
    insert into emp(empno,ename,sal,job.deptno)
      values(eno.name,sal,job,dno);
    exception
      when dup_val_on_index then
        raise_application_error(-20000,'雇员号不能重?);
      when e_integrity then
        raise_application_error(-20001,'部门号不存在');
   
调用
   exec add_employee(111,'clark',200,'manager',10);
   3 建立q程Q带有out 参数
    create or replcace procedure query_employee
    (eno number,name out varchar2,salary out number)
    is
    begin
      select ename,sal,into name,salary from emp where empno=eno;
    exception
      when no_data_found then
        raise_application_error(-20000,'G该雇员不存在');
    end;
   
    调用
    var name varchar2(10)
    var salary number
    exec query_employee(7788,:name,:salary);
    print name,salary;
    4 建立q程Q带有in out 参数
    create or replace procedure compute
    (num1,in out number,num2 in out number)
    is
       v1 number;
       v2 number;
    begin
      v1:=num1/num2;
      v2:=mod(num1,num2);
      num1:=v1;
      num2:=v2;
    end; 
   
    调用
    var n1 number
    var n2 number
    exec :n1:=100;
    exec :n2:=30
    exec compute(:n1,:n2)
    print n1 n2
    5) 为参C递变量和数据 位置传递,名称传递,l合传?br />    create or replace procedure add_dept
    (dno number,dname varchar2 default null,loc varchar default null)
    is
    begin
      insert into dept values(dno.dname,loc);
    end;
    -- 位置传?br />     exec add_dept(50,'sales','new york');
     exec add_dept(60);
     exec add_dept(70,'admin');
    -- 名称传?br />     exec add_dept(50,loc=>'new york');
     exec add_dept(60,dname=>'sales',loc=>'new york');
   6) 昄当前用户的所有程序及其源代码
     select text from user_source where name='add_dept';
   7) 删除q程
     drop procedure add_dept;
2 开发函?br />  可以在sql语句和其他子q程中执行?br />  1 建立函数Q不带Q何参?br />  create or replace function get_user
  return varchar2
  is
    v_user varchar2(100);
  begin
    select username into v_user from user_users;
    return v_user;
    end;
  --使用变量接收函数q回?br />   var v2 varchar2(100)
   exec :v1:=get_user
   print v1;
 -- 在sql 语句中直接调用函?br />   select get_user from dual;
 -- 使用dbms_output 调用函数
   set serveroutput on
   exec dbms_output.put_line('get_user');
 2) 建立函数Q带有in 参数
   create or replace function get_sal(name in varchar2)
   reutnr number
   as
   v_sal emp.sal%type;
   begin
     select sal into v_sal from emp where upper(ename)=upper(name);
     return v_sal;
   exception
     when no_data_found then
       raise_application_error(-20000,'employee does not exist');
   end;
 3) 建立函数Q带有out 参数
   create or replace function get_info
   (name varchar2,title out varchar2)
   return varchar2
   as
     deptname dept.dname%type;
   begin
     select a,job,b,dname into title,deptname from emp a,dept b
     where a.deptno=b.deptno
     and upper(a.ename)=uppder(name);
     return deptname;
   end;
   var job varchar2(20)
   var dname varchar2(20)
   exec :dname:=get_info('scott',job);
   print dname job

 4) 带有 in out 参数
  create or replace function result
  (num1,number,num2 in out nu8mber)
  return number
  as
    v_result number(6);
    v_remainder number;
  begin
    v_result:=num1/num2;
    v_remainder:=mod(num1,num2);
    num2:=v_remainder;
    retrun v_result;
  exception
    when zero_divide then
       raise_application_error(-20000,'zero divied');
  end;
 5) 函数调用的限?br />  -- 在sql 语句中只能调用存储函?br />  -- 在sql 语句中只能调用带有输入参数in Q而不能有输出参数out 和in out 参数的函?br />  -- 在sql 语句中调用的函数的参数必L标准的sql 数据cdQ不能是pl/sql 所Ҏ的数据类?br />  -- 在sql 语句中调用的函数不能包含insertQupdate 和delete
 6) 查看源代?br />  set pagesize 40
  select text form user_source where name='result';
 7) 删除函数
  drop function result;
 3 理子程?br />  1) 列出当前用户的子E序
   select object_name,created,status form user_objects
   where object_type in ('procedure','function');
  2)列出子程序源代码
   select text from user_sorce where name='raise_salary'
  3)cd子程序编译错?br />    -- show errors
    --使用数据字典user_errors  定错误原因和位|?br />     select line||'/'||position as "line/col",text error
     from user_errors where name='raise_salary';
   4) 列出对象的依赖关p?br />    -- 使用 user_dependenciess 定直接依赖关系
    select name,type from user_dependencies
    where referenced_name='emp'
    -- 使用 deptree ?ideptree 定依赖和见解依赖关p?br />    select nested_level,name,type from deptree;
   5) 重新~译子程?br />     当被应用对象的结构被修改以后Q就会将相关依赖对象转变为无效invalid Q需要重新编?br />     1
     alter table emp add remark varchar2(100);
     2
     select object_name,object_type from user_objects
     where status='invalid'
     3
     alter procedure add_employee compile;
     alter view dept10 compile;
     alter function get_info compile;
     
  



h 2006-10-11 14:51 发表评论
]]>
处理例外http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74563.htmlhhWed, 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 /> 1Q?例外分类
  预定义分c,非预定义分类Q自定义例外?br />2 处理预定义例?br />  1) 常用预定义例?br />   a access_into_null;
     create type emp_type as object
     (name varchar2(2),sal number(6,2));
    
     declare
       emp emp_type;
     begin
       emp.name:='scott';
     exception
       when access_into_null then
          dbms_output.put_line('首先初始化对象emp');
    b case_not_found
     undef no
     declare
       v_val emp.sal%type;
     begin
       select sal into v_sal from emp where empno=&no;
       case
         when v_sal<1000 then
           update emp set sal=sal+100 where empno=&no;
         when v_sal<2000 then
           update emp set sal=sal+150 where empno=&no;
         when v_sal<3000 then
          update emp set sal=sal+200 where empno=&no;
       end case;
       exception
         when case_not_found then
           dbms_output.put_line();
      end;
    c collection is null
    在给集合元素Q嵌套表或arraycdQ赋值前Q必首先初始化集合元素
    declare type ename_table_type is table of emp.eanme%type;
    ename_table ename_table_type;
    begin
      select e_name into ename_talbe(2) from emp where empno=$no;
    exception
      when collection_is_null then
        dbms_output.put_lilne('必须使用构造方法初始化集合元素');
    end;
    d currsor_already_open
    reopen curosr 如果用户已经使用了open 命o打开了显C游标,或执行for循环(隐式的打开游标)
       delcare
         cursor emp_surosr is select ename,sal,from emp;
       begin
         open emp_curosr;
         for emp_record in emp_cursor loop
              dbms_output.put_line(emp_record.eanme);
         end loop;
       exception
          when cursor_already_open then
            dbms_output.put_line("游标已经打开");
       end;
     e dup_val_on_index
     begin
     exception
        when dup_val_on_index then
        dbms_output.put_line("列上不能出现重复?);
     end;
     d invalid_curosr
     delcare
       curosr emp_cursor is select ename,sla from emp;
       emp_record emp_crusor%rowtype;
     begin
       fetch emp_cursor into emp_record;
       close emp_crusro;
     exception
       dbms_output.put_line("游标没有打开");
     end;
     f invalid_number  can not convert char to nmuber successfully
       begin
         update mep set sal=sal+1oo;
       exception
         when invalid_number then
     g no_data_found  when select into is executed ,no row is returned     
       declare
         v_sal emp.sal%type;
       begin
          select sal into v_cal form emp where lower(ename)=lower('&name');
       exception
         when no_data_found then
             dbms_output.put_line('没有q回l果');
       end;
      h too_many_row  ora -01422 there are too many are return when "select into" is executed
      i zero_divide ora-01476
      g subscript_beyond_count ora-065533
      declare
        type emp_array_type is varray(20) of varchar2(10);
        emp_array emp_array_type;
      begin
         emp_array:=emp_array_type('scott','mary');
         dbms_output.put_line('emp_array(3)');
      exception
         when subscript_beyone_count then
          dbms_out.put_line('出下标范围');
      end;
      k subscript_outside_limit
       emp_array(-1);
      l value_error the length of variable cannot contain the actual value;
      declare
      begin
      end;
3 处理非预定义例外
  delcare
    e_integrity exception;
    pragma exception_init(e_integrity,-2291);
  begin
    update emp set deptno=dno where empno=&no;
  exception
    when a_integrity then
  end;
4 处理自定义例?br />  与oracle 错误没有M联系Qؓ业务逻辑所定义的例?br />  delcare
    e_no_employee exception;
  begin
    update emp set deptno=&dno where empno=&eno;
  if sql%notfound then
    raise e_no_employee;
  end if;
  exception
    when e_no_emplyee then
     dbms_output.put_line('该雇员不存在');
5 使用错误例外函数
  使用例外函数可以取得错误号以及相关的错误消息Qsqlcode 用于取得oracle 的错误号Q而sqlerrm
则用于取得与之相关的错误信息?br />  1 sqlcode ?sqlerrm
  Z在pl/sql 应用E序中处理其他ؓ预料的到的oracle 错误Q用户可以在例外处理部分?br />when others 自句后,引用两个函数
  declare
    v_ename emp.ename%type;
  begin
    select ename into v_ename form emp where sal='&v_sal';
  exception
    when no_data_found then
    when others then
       dbms_output.put_line(sqlcode);
       dbms_output.put_line(sqlerrm);
  end;
  2 raise_aaplicaition_error
   只能在子E序中用(q程Q函敎ͼ包,触发器)
   raise_application_error(error_number,message,[true|false]);
   error_number 错误P在-20000刎ͼ20999 之间
   message 指定错误消息Q不能超q?048 
   if v_comm is null then
       raise_application_error(-20001,'该雇员无补助');
   end if;



h 2006-10-11 14:46 发表评论
]]>
开发包http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74562.htmlhhWed, 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 1) 建立包规范: 用于定义包的公共l徏Q包括常量,变量Q游标,q程和函数等
 create or replace package emp_package is
  g_deptno number(3):=30;
  procedure add_employee(eno number,name varchar2,salary number dno number default g_deptno);
  procedure fire_empoyee(eno number);
  function get_sal(eno number) return number;
 end emp_package;
  2) 建立包体
   用于实现包规范所定义的过E和函数。在包体中也可以单独定义U有lgQ包括变量,帔RQ过E和函数{。但在包体中所定义的组件只能在包内使用Q而不能由其他子程序引用?br />   create or replace package body emp_package is
    function validate_deptno(v_deptno number)
      return boolean;
    is
      v_temp int;
    begin
      select 1 into v_temp from dept where deptno=v_deptno;
      return true;
    exception
      when no_data_found then
       return false;
    end;
    procedure add_employee(eno number,name varchar2,salary number,dno number default g_deptno)
    is
    begin
      if validate_deptno(dno) then
        insert into emp(empno,ename,sal,deptno)
         values(eno,name,salary,dno);
      esle
        raise_application_error(-20011,'部门不存?);
      end if;
    exception
      when dup_val_on_index then
        raise_application_error(-20011,'该雇员已存在')
    end;
    procedure fire_employee(eno number) is
    begin
      select from emp where empno=eno;
      if sql%notfound then
         raise application_error(-20012,'');
      end if;
    end;
   function get_sal(eno number) return number
   is
     v_sal emp.sal%type;
   begin
     select sal into v_sal from emp where empno=eno;
     return v_sal;
   exception
     when no_data_found then
       raise_application_error(-200012,'');
   end;
  end emp_package;
  3) 调用包组?br />    Q- 在同一个包内调用组?不需要加包名前缀Q直接调?br />  create or replace paclage body emp_package is
    procedure add_employee(eno number,name varchar2,salary number,dno number default g_deptno)
    is
    begin
      validate_deptno(dno) then
    exception
    end;
   -- 调用包公用变?br />   exec emp_package.g_deptno:=20
   -- 调用公用q程
   exec emp_package.add_employee(111,'mary,2000');
   -- 调用q程数据库包的公用组?br />   exec emp_paclage.add_employee@orasrv(1116,'scott',1200);
   4) 查看包源代码
   select text from user_source where name='emp_package' and type='package';
   5) 删除?br />   drop packagee emp_package;
2 使用包重?br />  指多个具有相同名字的子程序,在调用的时候用不同的参数传递?br />  1) 建立包规?br />   同名q程和函数必d有不同的输入参数Q但同名函数q回值的cd数据必须相同
    create or replace package overload is
    function get_sal(eno number) return number;
    function get_sal(name varchar2) return number;
    procedure fire_employee(eno number);
    procedure fire_employee(name varchar2);
   2) 建立包体
    。。?br />3) 使用包构造过E?br /> 初始化全局变量
 1 建立包规?br />   create or replace packiage emp_package
   is
   minsal number(6,2);
   maxsal number(6,2);
   procedure upd_sal(eno number,salary number);
   procedure upd_sal(name varhcar2,salary number);
   end;-- 定义了两全局变量和三个公用过E?br />   2 建立包体
    create or replace package body emp_package is
      procedure add_employee(cno number,name varchar2,salary number,dno number)
      is
      begin
        if salary between minsal and maxsal then
          insert into emp(empno,ename,sal,deptno)
         ........
    -- 构造过E,位于子程序尾部,已begin 开始已end l束
     begin
       select min(sal),max(sal) into minsal,maxsal from emp;
     end;
      end;
3 调用包公用组?br />   在同一ơ会话中W一ơ调用包的公用组建时Q会自动执行其它构造函敎ͼ而将来调用其他组建时则不会再调用其构造过E?br />4 使用U度U别
 1 安包规?br />  create or replcace package purity is
  minsal number(6,2);
  maxsal number(6,2);
  function max_sal return number;
  function min_sal return number;
  pragma restrict_references(max_sal,wnps);--wnps 不能修改包的变量Q不能给包的变量赋|                                            --wnds 不能执行dml
  pragma restrict_references(min_sal,wnps);--rnps 用于限制函数不能d包变?br />  end;

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

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

h 2006-10-11 14:45 发表评论
]]>
使用触发?/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 />  1) 触发事g<br />  2) 触发条g<br />  3) 触发操作<br />     . 触发器代码的大小不能过32kQ如果用大量代码徏立触发器Q应该先建立存储q程Q然后再触发器中使用call语句调用存储q程?br />     . 触发器中正能含有select QinsertQupdate 和delete 语句Q而不能含有ddl 语句Q和事物控制语句?br />2 建立dml 触发?br /> 1) 触发时机<br />  beforeQafter 表示在执行dml操作之后触发?br /> 2)触发事g<br />  insert Qupdate 和delete 操作。也可以使用书法事g<br /> 3) dml 触发器是针对特定表进行的 因此必须制定dml 操作所对应的表<br /> 4) 触发器类?用于指定当触发器事g之后Q需要执行几ơ触发器操作。如果指定语句触发器cd<br />那么会执行一ơ触发器代码Q如果指定行触发器类型,则会在每个被作用行上执行一ơ触发器代码?br /> 5) 触发条g<br /> 用于指定执行行触发器代码的条Ӟ只有为tureӞ才会执行行触发器代码?br />  6) 如果使用pl/sql 存储q程Qjava 存储q程Q或外部处处q程需要在触发器操作部分直接用call<br />  7) dml 触发器触发顺?br />   (1)dml 触发器在单行数据上的触发序?br />    对于单行数据而言Q无论是语句此触发器Q还是行触发器,触发器代码实际只执行一ơ,q且执行<br />序为before 语句触发器,before 行触发器Qdml 操作Qafter 行触发器Qafter 语句触发?br />   (2) dml 触发器在多行数据上的触发序<br />    before 语句触发?<br />    before 行触发器<br />    after 行触发器<br />    before行触发器<br />    after 行触发器<br />    after语句触发?br />   语句触发器只被执行一ơ,而行触发器在每个行上都执行一ơ?br />  2) 语句触发?br />  当审计dml 操作Q或保dml操作安全执行Ӟ可以使用语句触发?br />  1 建立before 语句触发?br />   create or replace trigger tr_sec_emp<br />   before insert or update or delete on emp<br />   begin<br />     if to_char(sysdate,'DY','nls_dtate_language=AMERICAN') in ('sat','sun') then<br />     railse_application_error(-200001,'不能在休息日改变雇员信息');<br />     end if;<br />   end; <br />   2 使用条g谓词<br />   inserting ,updating ,deleting<br />   create or replace trigger tr_sec_emp<br />   before insert or update or delete on emp<br />   begin<br />     if to_char(sysdate,'DY','nls_date_language=american')<br />      in('sat','sun') then<br />     case<br />       when inserting then<br />         raise_application('-20001','inserting');<br />       when updating then<br />         raise_application('-20002','updating');<br />       when deleting then<br />         raise_application('-20003','deleting');<br />     end case;<br />    end if;<br />   end;<br />   3 建立after 语句触发?br />    Zdml 操作Q或者dml 操作后执行汇总运?br />   create table aduit_table(<br />     name varchar2(20),ins int,upd int,del int,<br />     starttime date,endtime date<br />   );<br />   create or replace trigger tr_aduit_emp<br />   after insert or update or delete emp<br />   declare<br />     v_temp int;<br />   begin<br />     select count(*) into v_temp from aduit_table<br />       where name='emp';<br />     if v_temp=0 then<br />       insert into audit_table values<br />       ('emp',0,0,0,sysdate,null);<br />     end if;<br />     case<br />       when  inserting then<br />         update aduit_table set ins=ins+1,endtime=sysdate where name='emp';<br />       when updating then<br />         update audit_table set upd=upd+1,endtime=sysdate where name='emp';<br />       when deleting then<br />         update aduit_table set del=del+1,endtime=sysdate where name='emp';<br />   end;<br />  3) 行触发器<br />   审计数据变化可以使用行触发器<br />   1 建立不before 行触发器<br />    Z取保数据W合商业逻辑或企业规则,对输入的数据q行复杂的约束,可以使用before行触发器<br />     create or replace trigger tr_emp_sal<br />     before update of sal on emp<br />     for each row<br />     begin<br />       if :new.sal<:old.sla then<br />         raisse_application_error(-200010,'工资只涨不降');<br />       end ifQ?br />     end;<br />     2) 建立after 行触发器<br />     Z审计dml 操作Q可以用语句触发器或oracle pȝ提供的审计功能,而ؓ了审计数据变?br />Q则应该使用after 行触发器<br />     create table audit_emp_change(<br />       name varchar2(10),odl number(6,2),<br />       newsal number(6,2),time date);<br />    create or replace trigger tr_sal_change<br />    after update of sal on emp<br />    for each row<br />    declare<br />     v_temp int;<br />    begin<br />     select count(*) into v_temp from audit_emp_change where name=:old.ename;<br />    if v_temp =0 then<br />      insert into audit_emp_change<br />        values(:old,ename,:old.sal,:new,sal,sysdate);<br />    else<br />      update audit_emp_change <br />        set oldsal=:old.sal,newsal=:new.sal,time=sysdate where name=:old.ename; <br />    end if;</p> <p>    end;<br />     )<br />    3) 限制行触发器<br />    当用行触发器,默认情况下会咱每个被作用行上七星一ơ触发器代码Qؓ了时得再特定条g下执行行触发器代码,需要用when 子句<br />    create or replace trigger tr_sal_change<br />    after update of sal on emp<br />    for each row<br />    when(old.job='salesman')<br />    declare<br />       v_temp int..<br />2 dml 触发器用注意事?br />  触发器代码不能从触发器所对应的基表中d数据<br />3 dml 触发?br />  Z保证数据库满特定的商业规则或企业逻辑Q可以用约束,触发器和子程序。约束性能最好,实现最单,所以ؓ售选,如果触发器不盟实玎ͼ可以选择触发器?br />  dml 触发器可以用于实现数据安全保护,数据审计Q数据完整性,参照完整性,数据复制{功能?br /> 1) 控制数据安全<br />  create or replace trigger tr_emp_time<br />  before insert or update or delete on emp<br />  begin<br />    if to_char(sysdate,'hh24') not between '9' and '17' then<br />      raise_application_error(-20101,'not work time');<br />     end if;<br />  end;<br />  2) 实现数据审计<br />  使用数据审计只能审计sql 操作Q而不会记载数据变?br />  audit insert,update,delete on emp by access<br />  3)实现数据完整?br />  首选约?alter table emp add constraint ck_sal check (sal>=800),但是在有些情况下只能使用触发器来实现数据完整?br />   create or replace trigger tr_check sal<br />   before update of sal on emp<br />   for each row<br />   when (new.sla<old.sal or new.sal>1.2* old.sal)<br />   begin<br />      raise_application_error(,,,,,,)<br />   end;<br />  3) 使用引用完整?br />  采用 on delete cascade 可以q行集联删除Q但是却不能q行集联更新。采用触发器实现集联更新<br />   create or replace trigger tr_update<br />   after update of sal on emp<br />   for each row<br />   begin<br />     update emp set depno=:new.deptno where dentno=:old.deptno;<br />   end; <br />4 建立instead of 触发?br />  对于单视囑֏以直接进行insert update 和delete {操作,但是对于复杂视图不允许进行insertQupdate 和delete 操作?br />  满一下条件的为复杂视?br />    h操作集合W?unionQunion all QintersectQminus<br />    h分组函数 minQmaxQavgQsumQcount<br />    hgroup by connect ~译 或start with<br />    hdistinct <br />    hq接<br />  Z在复杂视图上执行dml 操作Q必要Zinstead-of 触发器,建立insteadQof 触发器后Q就可以Z复杂视图执行insertQupdate和delete 语句?br />   instead of 选项只用于视图<br />   Z视图建立触发器时Q不能定义before ?after<br />   在徏立视图时不能指定 with check option <br />   当徏立instead of 触发器时Q必L定for each row 选项<br />  1) 建立复杂视图dept_emp<br />   create or replace view dept_emp as<br />   select a.deptno,a.dname,b,empno,b,ename<br />   from dept a,emp b<br />   where a,deptno=b.deptno;<br />  2) 建立 insteadQof 触发?br />   create of replacee trigger tr_instead_of_dept_emp<br />   instead of insert on dept_emp<br />   for each row<br />   declare<br />     v_temp int;<br />   beegin<br />      select count(*) into v_temp from dept where deptno=:new.deptno;<br />      if v_temp=0 then<br />        insert into dept(deptno,dname) values(:new.deptno,:new.dname);<br />      end if;<br />      select count(*)into v_temp from emp where empno=:new.empno;<br />      if v_temp=0 then<br />         insert into emp(empno,ename,deptno)<br />           values(:new.deptno,:new.ename,:new.deptno);<br />         end if;<br />   end;<br />   <br />   可以对视图执行insert 操作?br />    insert into dept_emp values(50,'admin','1223','mary')<br />5 理触发?br />  1) 昄触发器信?br />    select trigger_name,status from user_triggers<br />    where table_name='emp';<br />   2)止触发?br />    alter trigger tr_check_sal disable;<br />   3) Ȁz触发器<br />    alter trigger tr_check_sal enable;<br />   4) 止或激z表上的所有触发器<br />    alter table emp disable all triggers;<br />    alter table emo eanble all triggers; <br />   5)重新~译触发?br />    alter trigger tr_check_sal compile;<br />   6) 删除触发?br />    drop trigger tr_check_sal;</p> <img src ="http://www.aygfsteel.com/kevinfriend/aggbug/74559.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/kevinfriend/" target="_blank">h</a> 2006-10-11 14:44 <a href="http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74559.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用复合变量http://www.aygfsteel.com/kevinfriend/archive/2006/10/11/74557.htmlhhWed, 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 1Q烦引表
  type ename_table_type is table of emp.ename%type
    index by binary_integer;
  ename_table ename_table_type;
  begin
    select ename into ename_table(-1) from emp
      where empno=&no;
    dbms_output.put_line('雇员名:'||ename_table(-1));
  end;
 
   set serveroutput no
   declare
     type area_table_type is table of number
        index by varchar2(10);
     rea_table area_table_type;
    begin
        area_table('beijing'):=1;
        area_table('shanghai'):=2;
        area_table('guangzhou'):=3;
        dbms_output.put_line(area_table.first);
        dbms_output.put_line(area_table.last);
    end;
     2) 嵌套?br />      索引表类型不能作为篏得数据类型用,但是嵌套表可以作cȝ数据cd使用?br />当用嵌套表元素Ӟ必须先用其构造方法初始化其嵌套表Q?br />       a  在pl/sql 块中使用嵌套?br />        declare
          type ename_table_type is table of emp.ename%type;
          ename_table ename_table_type;
        begin
           ename_table:=eanme_table_type('2','2','3');
           select ename into ename table(2) from emp where empno=&no;
           dbms_ouput.put_line(ename_table(2));
        end;
      b 在表中用嵌套表
        create type phone_type is table of varchar2(20);
        create table employee(
          id number (4),name varchar2(10),sal number(6,2),
          phone phone_type
        )nested table phone store as phone_table;
       -- 为嵌套表插入数据
        insert into employee values(2,'scott',200,phone_type('2222','333333'));
       Q-索嵌套表累得数据
        set serveroutput on
        declare
          phone_table phone_type;
        begin
          select phone into phone_table
          from employee where id=1;
          for i in 1..phone_table.count loop
            dbms_output.put_line(phone_table(i));
          end loop;
        end;
       -- 更新嵌套表列的数?br />         delcare
            phone_table phone_type:=('44444','555555');
         begin
            update employee set phone=phone_table
            where id=1;
         end;
    3) 变长数组
      在用varray 时必L定最大个敎ͼ和数据类?在用其元素时必进行初始化
      type ename_table_type is varray(20) of emp.ename%type;
      ename_table ename_table_type:=ename_table_type('1','2');
     
      -- 在快中用varray
      declare
         type ename_table_type is varray(20) of emp.enameQtypeQ?br />         ename_table ename_table_type:=ename_table_type('mary');
         begin
            select ename into ename_table(1) form emp
               where empno=&no;
          end;
      --在表列中使用varray
       create type phone type is varray(20) of varchar2(20);
       create table employee(
         id number(4),name varchar2(10),
         sal number(6,2),phone phone_type);
      
     3)记录?br />      记录表结合了记录和集合的优点
        declare
          type emp_table_type is table of emp%rowtype
          index by binary_integer;
        emp_table emp_table_type;
        begin
          select * from into emp_table(1) from emp
          where empno=&no;
          dbms_output.put_line(emp_table(1).ename);
        end;
      4)多维集合
       1 多varray
       declare
        --define 一l集?br />          type al_array_type is varray(10) of int;
        --定义二维集合
          type nal_varray_type is varray(10) of a1_varray_type;
        --初始化二l集?br />          nvl nal_varray_type:=nal_varray_type(
             a1_varray_type(1,2),
            a1_varray_type(2,3)
          )
         beign
           for i in 1..nal_varray_type.count loop
              for j in 1..a1_array_type.count loop
                dbms_out.putline(nvl(i)(j));
              end loop;
           end loop;
        end;
       2 使用多嵌套?br />        table a1_table_type is table of int;
        table nvl_table_type is table of a1_table_type;
        nvl nvl_table_type:=nvl_table_type(
          a1_table_type(1,2),
          a1_table_type(2,3)
        );
2 集合Ҏ
  1) exist
   if ename_table.exists(1) then
    ename_table(1):='scott';
   2) count q回当前集合变量中的元素M?br />    ename_table.count
   3) limit q回集合元素的最大个敊W?只有varray ?br />   4)first and last
       ename_table.first
       ename_table.last
   5) prior 和next
    ename_table.prior(5); Q-q回元素5的前一?br />    ename_table.next(5);  --  后一?br />   6) extend
    使用于varray ?嵌套表?br />    extend add a null value
    extend (n) add n null value
    extend (n,i)add n i value
    declare
      type ename_table_type is varray(20) of varchar2(20);
      ename_table ename_table_type;
    begin
      ename_table:=ename_table_type('mary');
      ename_table.extend(5,1);
      dbms_output.put_line(ename_table.count);
    end;
   7) trim
   trim remove one element from the tail of the collection.
   trim(n) remove n element from the tail of the colleciton.
   8)delete
    delete: delete all the elements
    delete(n) :delete the nth elements
    delete(m,n): delete the elements from m to n
3 集合赋?br />  1)一个集合的数据赋值给另一个集?clear the destination collectins and set the original collection
   delcare
    type name_varray_type is varray(4) of varchar2(10);
    name_array1 name_varray_type;
    name_array2 name_varray_type;
   begin
     name_array1:=name_varray_type('scott','smith');
     name_array2:=name_array_type('a','b','c');
     name_array1:=name_array2;  
   end;
  
 
  type name_array1_type is varray(4) of varchar2(10);
  type name_array2_type is varray(4) of varchar2(10);
  name_array1 name_array1_type;
  name_array2 name_array2_type;
  h相同的数据类型,单具有不同的集合cd不能构赋?br />  2) l集合赋城null ?br />    可以使用delete ?trim
    也可以?I集合赋l目表集?br />    type name_varray_type is varray(4) of varchar2(10);
    name_array name_varray_type;
    name_empty name_varray_type;
   
    name_array:=name_varray_type('1','2');
    name_array:=name_empty;
  3) 使用集合操作赋和比较集合都是10g 的内容,p176 先略q?br />4 扚wl定
  执行单词sql 操作能传递所有集合元素的数据?br />  1 forall 语句
  用于insert update 和delete操作。在oracle9i 中forall 语句必须hq箋的元?br />    1) using forall on insert
     declare
        type id_table_type is table of number(6)
        index by binary_integer;
        type name_table_type is table of varchar2(2)
        index by binary integer;
        id_table id_table_type;
        name_table name_table_type;
      begin
         for i in 1..10 loop
           id_table(i):=i;
           name_table(i):='Name'||to_char(i);
         end loop;
         forall i in 1..id_table.count
           insert into demo values(id_table(i),name_table(i));
      end;
     2)using forall on using update
       forall i in 1..id_table.count
           upate demo set name:=name_table(i)
              where id:=id_table(i);
     3)using forall on using delete
        forall i in 1..id_table.count
            delete from demo where id:=id_table(i);
     4) using forall on part of the collection
        for i in1..10 loop
          id_table(i):=i;
          name_table(i):="name"||to_char(i);
        end loop;
        forall i in 8..10 l
           insert into demo values(id_table(i),name_table(i));
   2 bulk collect
     is fit for select into ,fetch into and dml clause
     1) using bulk collect
      declares  
        type emp_table_type is table of emp%rowtype
         index by binary_integer;
        emp_table emp_table_type;
      begin
         select * bulk collect into emp_table
          from emp where deptno=&no;
         for i in 1..emp_tablee.count loop
            dbms_output.put_line(emp_table(i).ename);
         end loop;
      2) 在dml 的返回字句用bulk collect 字句
         declare
          type ename_table_type is table of emp.ename%type;
           ename_table ename_table_type;
          begin
             deletee from emp where deptno=&no
             returning ename bulk_collect into ename_table;
          for i in 1..ename_table.count loop
            dbms_output.put(ename_table(i));
          end loop;
        end;
          end;
      end;

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

h 2006-10-11 14:43 发表评论
]]>
վ֩ģ壺 | ŷ| ̨| | ɳ| ƽ̶| ¹| | н| ʶ| | | | | | ԭ| ƽ| ɽ| | ͬ| ̺| | | | | ï| | Ǧɽ| | ϳ| ̨| | | | | ɽ| ɽ| | | | ˮ|