05.oracle基本用法

-------高级部分

1.序列(oracle特有,相当于mysql的auto_increment)(解决了mysql拿到自增长值难,或者不一致的问题)

create sequence my_seq
   start with 1
   increment by 1
   nomaxvalue 
   minvalue 1
   nocycle    #cycle相当于遇到最大/小值就循环
   cache 1000;
#下一个数
select my_seq.nextval from dual;
#当前的数
select my_seq.currval from dual;

2.PLSQL编程(判断null时,要用 is)

   --定义变量 并且赋值的区域
  DECLARE
    vnum number;
  vage number := 28;
  vabc number := &abc;--输入一个数值,从一个文本框输入
  vsal emp.sal%type;  --引用型的变量,代表emp.sal的类型
  vrow emp%rowtype;   --记录型的变量,代表emp一行的类型     

  BEGIN
   --将查询到的sal内容存入vsal并输出
   select sal into vsal from emp where empno = 7654; 
  
  	dbms_output.put_line(vsal);
     IF 10<i THEN
     ELSE IF xxx THEN
     ELSE 
     END IF;
     WHEN  10<i  LOOP

      END LOOP;

       while i<10 loop
       end loop;

      for i in reverse 1 .. 10 loop    #10开始
      end loop;
       
        loop
             exit when i>10
        end loop;

  END;

3.索引

#创建索引
create index user2_index on user2(id,name);
#改索引的名字
alter index user2_index rename to user2_index1
#就是这个表需要 建立多个索引的条件在语句中
alter index user2_index1 coalesce
#重建索引,一般不使用,会导致变慢
alter index user2_index1 rebuild
#删除索引
drop index user2_index1;

#语句可以使用索引

4.视图(不推荐,需要system权限)

  #创建视图
  create view aaa as select * from user2
  #查询视图,和表一样
   select * from aaa;
  #删除
  drop view aaa
  #修改视图,存在视图就replace
  create or replace view view_emp as
   select id from user2;

5.同义词(也与视图一样,区别在于视图是使用语句,同义词是整张表名)

   create synonym view_emp1  for user2;
   create or replace synonym view_emp1 for user2

   drop synonym view_emp1

6.游标

--输出指定部门下的员工姓名和工资
declare
  --1.声明游标
  cursor vrows(dno number) is select * from emp where deptno = dno;
  --声明变量
  vrow emp%rowtype;
begin
  --2.打开游标 传入deptno=10
  open vrows(10); 
  --3.循环遍历
  loop
    --拿到一条数据
    fetch vrows into vrow; 
    --相当于break,但是有加条件
    exit when vrows%notfound;
    dbms_output.put_line('姓名:' || vrow.ename || ' 工资: ' || vrow.sal);
  end loop;
  --4.关闭游标
  close vrows;
end;

   ----系统游标遍历
  --输出员工表中所有的员工姓名和工资
declare
  --1.声明系统引用游标
  vrows sys_refcursor;
  --声明变量
  vrow emp%rowtype;
begin
  --2.打开游标
  open vrows for select * from emp;
  --3.循环遍历
  loop
    fetch vrows into vrow;
    exit when vrows%notfound;
    dbms_output.put_line('姓名:' || vrow.ename || ' 工资: ' || vrow.sal);
  end loop;
  --4.关闭游标
  close vrows;
end;

7.存储过程(创建存储过程不知道是否语法有错误)

--给指定员工涨薪并打印涨薪前和涨薪后的工资,指定传入参数类型
--in代表输入, out代表输出
create procedure proc_update_sal(vempno in number,vnum in number) 
is  
  --is后面是声明变量部分
  vsal number;
  aa number;
begin
  --查询当前的工资
  select sal into vsal from emp where empno = vempno;
  --输出涨薪前的工资
  dbms_output.put_line('涨薪前:' || vsal);
  --更新工资
  update emp set sal = vsal + vnum where empno = vempno;
  --输出涨薪后的工资
  dbms_output.put_line('涨薪后:' || (vsal + vnum));
  --提交事物
  commit;
end;

--给员工编号为7521的员工涨工资10元
call proc_update_sal(7521, 10);

   ---修改存储过程
   create or replace procedure proc_update_sal(vempno in number,vnum in number)
is
  --声明变量
  vsal number;
begin
  --查询当前的工资
  select sal into vsal from emp where empno = vempno;
  --输出涨薪前的工资
  dbms_output.put_line('涨薪前:' || vsal);
  --更新工资
  update emp set sal = vsal + vnum where empno = vempno;
  --输出涨薪后的工资
  dbms_output.put_line('涨薪后:' || (vsal + vnum));
  --提交事务
  commit;
end;

 ----删除存储过程
   drop procedure proc_update_sal;

8.函数(和存储过程一样,但是他有返回值 return xxx;)

create function func_getsal(vempno number) return number  --返回的数据类型
is
  vtotalsal number;
begin
  select sal * 12 + nvl(comm, 0) into vtotalsal from emp where empno = vempno;
  return vtotalsal;
end;

--查询员工编号为7788的年薪
declare
  vsal number;
begin
  vsal := func_getsal(7788);
  dbms_output.put_line(vsal);
end;

   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值