Oracle对象和编程

  跨用户  查询语句创建表    

create table emp as select * from scott.emp;

视图

创建视图[必须有dba权限]
create view V_emp as select ename,job from emp;

select * from V_emp;         查询视图

update V_emp set job = 'CLERK' where ename = 'ALLEN';      修改视图 原表也会修改

create view V_emp2 as select ename,job from emp with read only;  创建只读视图

  作用    1. 屏蔽一些敏感字段,列如员工工资         2. 保证 主公司 数据变换时, 分公司的数据 也会变化

索引

 索引就是在表的 列上 构建一个二叉树 达到大幅度提高查询 效率的 目地,但是索引会影响增删改的 效率

单列索引     触发规则: 条件必须是索引中的原始值 ,单行函数,模糊查询都不行

create index idx_ename on emp(ename);
select * from emp where ename = 'SCOTT';    触发
  

复合索引     触发条件: 必须包含有优先检索列中的原始值    复合索引中第一列是优先检列,ename 

create index idx_enamejob on emp(ename,job);   
select * from emp where ename = 'SCOTT' and sal ='4000';  --触发复合索引 

当同时存在单列和复合索引时

select * from emp where ename = 'SCOTT';   触发单列

select * from emp where ename = 'SCOTT' and job ='ANALYST';  触发复合索引

select * from emp where ename = 'SCOTT' or job ='ANALYST';  
不触发索引,相当于两个查询语句,一个触发一个不触发,等于不触发

编程语言

  定义变量 

   是一个面向过程的语言      declare 用于

declare
  i number(2) :=10;          赋值用  :=
  s varchar2(10) :='小明';
begin
  dbms_output.put_line(i);
  dbms_output.put_line(s);         输出语句
end;

  sql 使用   | |   连接字符串          emprow 相当于一个对象 存储了一行数据  可以点操作         也可以使用  into 给变量赋值

declare
  ena emp.ename%type;         将emp表中ename的类型给ena变量
  emprow emp%rowtype;         将emp表中一行数据给 emprow 可以存一行数据
begin
  select ename into ena from emp where empno = 7654;
  select * into emprow from emp where empno = 7654;
  dbms_output.put_line(emprow.ename || '的工作为:' || emprow.job);
end;

if判断

declare
  i number(2) :=&age;          &后面随便写  表示输入          
begin
  if i<18 then
    dbms_output.put_line('未成年');
  elsif i<40 then
    dbms_output.put_line('中年');
  else
     dbms_output.put_line('老年人');
     
  end if;
end;

循环

 while循环  输出 1到10

declare
  i number(2):=1;
begin
  while i < 11 loop
    dbms_output.put_line(i);
    i:=i+1;
  end loop;
end;

  exit 循环  更常用

declare
  i number(2):=1;
begin
  loop
    exit when i >10;
    dbms_output.put_line(i);
    i:=i+1;
  end loop;
end;

   for 循环

declare
  i number(2):=1;
begin
  for i in 1..10 loop                用1..10表示   1到10
     dbms_output.put_line(i);
  end loop;
end;

游标

  用来存放数据   多个对象  多行数据 

declare
  cursor c1 is select * from emp;     将emp表存入 游标
  emprow emp%rowtype;                定义一个对象(一行数据)
begin
  open c1;          打开游标
       loop
         fetch c1 into emprow;         循环将每一行给 emprow
         exit when c1%notfound;        判断c1还有下一行
         dbms_output.put_line(emprow.ename);    
       end loop;
  close c1;        关闭游标
end;

含参数游标      参数类型是一个 emp表中的 部门id     当使用游标c2 时传入一个 部门值      就将这个员工的 id全给了c2  再通过  fetch  c2  into  en    en就存了一些员工的id   给这个部门的每个员工 sal+100

declare
  cursor c2(eno  emp.deptno%type) is 
  select empno from emp where deptno = eno;
  en emp.empno%type;
begin
  open c2(10);
       loop
         fetch c2 into en;
         exit when c2%notfound;
         update emp set sal= sal+100 where empno=en;
       end loop;
  close c2;
end;

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值