子查询join .. on ..:select ename,sal from emp join (select deptno,max(sal) max_sal from emp group by deptno) em on (emp.sal=em.max_sal and emp.deptno = em.deptno)
自身表连接:1.select emp.ename,t.ename from emp join (select ename,empno from emp) t on emp.mgr = t.empno 2.select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno
select ename,dname from emp join dept on (emp.deptno = dept.deptno) <--->select ename,dname from emp join dept using(deptno)
dba登陆:conn sys/密码 as sysdba; 授权:grant create table,create view to scott;
create view v$_dept_avg_sal_grade as select deptno,grade,avg_sal from (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s on (t.avg_sal between losal and hisal);
备份数据库,使用dba帐号登陆:使用exp导出,在此过程中要输入要备份的用户
创建新用户并授予权限:create user 用户名 identified by 密码 default tablespace users quota 10M on users; grant create session,create table,create view to 用户
导入数据:imp 事务回滚:rollback
创建一个新表并把指定表的数据copy到新表中:create table emp2 as select * from emp; (insert into emp2 select * from emp)
rownum: select empno,ename from emp where rownum <= 5;
分页查询:select ename,sal,r from (select ename,sal,rownow r from (select ename,sal from emp order by sal)) where r between 6 and 10;
if... elsif...else end if:declare v_sal emp.sal%type; begin select sal into v_sal from emp where empno = 7369; if(v_sal <1200) then dbms_output.put_line('low'); elsif(v_sal<2000) then dbms_output.put_line('middle'); else dbms_output.put_line('high'); end if; end;
循环:declare i binary_integer := 1; begin loop dbms_output.put_line(i); i := i+1; exit when (i>=11); end loop; end;
declare j binary_integer := 1; begin while j<11 loop dbms_output.put_line(j); j := j + 1; end loop; end;
begin for k in 1..10 loop dbms_output.put_line(k); end loop; for k in reverse 1..10 loop dbms_output.put_line(k); end loop; end;
创建序列: create sequence seq_errorlog_id start with 1 increment by 1;
游标(cursor): declare cursor c is select * from emp; v_emp c%rowtype; begin open c; fetch c into v_emp; dbms_output.put_line(v_emp.ename); close c; end;
declare cursor c is select * from emp; v_emp c%rowtype; begin open c; loop fetch c into v_emp; exit when (c%notfound); dbms_output.put_line(v_emp.ename); end loop; close c; end;
declare cursor c is select * from emp; v_emp c%rowtype; begin open c; fetch c into v_emp; while (c%found) loop dbms_output.put_line(v_emp.ename); fetch c into v_emp; end loop; close c; end;
declare cursor c is select * from emp; begin for v_emp in c loop dbms_output.put_line(v_emp.ename); end loop; end;
procedure(存储过程):创建--> create or replace procedure p is cursor c is select * from emp2 for update; begin for v_emp in c loop if(v_emp.deptno = 10) then update emp2 set sal = sal + 10 where current of c; elsif(v_emp.deptno =20) then update emp2 set sal = sal + 20 where current of c; else update emp2 set sal = sal + 50 where current of c; end if; end loop; commit; end; 调用-->: exec p; 或者 begin p; end;
带参数的存储过程:创建--> create or replace procedure p (v_a in number, v_b number, v_ret out number,v_temp in out number)--in传入参数,out返回值,in out传入和返回参数,默认为in is begin if(v_a > v_b) then v_ret := v_b; else v_ret := v_b; end if; v_temp := v_temp + 1; end; 调用--> declare v_a number := 3; v_b number := 4; v_ret number; v_temp number := 5; begin p(v_a,v_b,v_ret,v_temp); dbms_output.put_line(v_ret); dbms_output.put_line(v_temp); end;
函数: 创建--> create or replace function sal_tax (v_sal number) return number is begin if(v_sal <2000) then return 0.10; elsif (v_sal <2750) then return 0.15; else return 0.20; end if; end; 调用-->与其他内置函数一样 select lower(ename),sal_tax(sal) from emp;
trigger(触发器): 创建一张log表--> create table emp2_log( uname varchar2(20), action varchar2(10), atime date ) 创建触发器-->(级联更新—— update emp2 set deptno = :NEW.deptno where deptno = :OLD.deptno;) create or replace trigger trig after insert or delete or update on emp2 for each row begin if inserting then insert into emp2_log values( USER, 'insert',sysdate); elsif updating then insert into emp2_log values (USER,'update',sysdate); elsif deleting then insert into emp2_log values (USER,'detele',sysdate); end if; end;
posted on
2013-06-30 23:36zcjava
阅读(270)
评论(0)
编辑收藏举报