oracle笔记

◎checkpoint用于只对点的数据进行恢复
◎查看有几张表select * from tab;
◎ed可以进行编辑
◎/重复命令
◎合并字符串||
◎查看表的结构 desc 表名
◎sql 中 字符串用单引号
◎主建可以由多个字段组成
◎外键不能为空,且一定要在另张表中存在
◎左外链接,就是将左边与右边的链接全部显示出来,右边没有的,左边就以空白填空
◎可以使用@加文件名就可以在sqlplus中执行其中的plsql语句
◎使用%type使得定义存储过程中的变量的时候与表中的类型一致
◎如一下语句就取得是stu_tb01中的name字段类型一致
declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
end;
/
◎使用record来定义结构
declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
end;
/

◎由以上使用%rowtype可以将记录与表的类型对应起来,这样表的类型更改之后,那么该类型也会进行更改
◎以上也可以通过my_rec v_rec来声明记录类型的变量
◎另外记录中的数据也可以进行嵌套
◎数据库中的table类型就类似于c++中的map类型
◎创建table的方法
declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
end;
/
◎oracle中的控制语句
declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
  v_cnt number:=0;
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
  loop
     dbms_output.put_line('hello');
     if(v_cnt>=10) then
 exit;
     end if;
      v_cnt:=v_cnt+1;
  end loop;
  while  v_cnt>0 loop
     dbms_output.put_line('hello');
     v_cnt:=v_cnt-1;
  end loop;
  for v in reverse 1..5 loop
    dbms_output.put_line('huangzh');
  end loop;
end;
/
◎空语句
if (true) then
 null;
end if;
◎使用游标的方法
declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
  v_cnt number:=0;
  v_str varchar2(200);
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
   cursor stu_cur is //定义游标
          select id ,name from stu_tb01;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
  loop
     dbms_output.put_line('hello');
     if(v_cnt>=10) then
 exit;
     end if;
      v_cnt:=v_cnt+1;
  end loop;
  while  v_cnt>0 loop
     dbms_output.put_line('hello');
     v_cnt:=v_cnt-1;
  end loop;
  for v in reverse 1..5 loop
    dbms_output.put_line('huangzh');
  end loop;
  if (true) then
     null;
  end if;
  open stu_cur;//打开游标
  loop //循环执行游标读取游标中所指定的变量的值
      fetch stu_cur into v_id,v_name;
          dbms_output.put_line(v_id);
  dbms_output.put_line(v_name);
        exit when stu_cur%notfound;
  end loop;
  close stu_cur;//关闭游标
end;
/
◎注释用--
◎可以使用带参数的游标如:cursor stu_cur(变量名 类型)
如:
declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
  v_cnt number:=0;
  v_str varchar2(200);
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
   cursor stu_cur(vv_id number) is
          select id ,name from stu_tb01 where id <vv_id;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
  loop
     dbms_output.put_line('hello');
     if(v_cnt>=10) then
 exit;
     end if;
      v_cnt:=v_cnt+1;
  end loop;
  while  v_cnt>0 loop
     dbms_output.put_line('hello');
     v_cnt:=v_cnt-1;
  end loop;
  for v in reverse 1..5 loop
    dbms_output.put_line('huangzh');
  end loop;
  if (true) then
     null;
  end if;
  open stu_cur(600);
  loop
      fetch stu_cur into v_id,v_name;
          dbms_output.put_line(v_id);
  dbms_output.put_line(v_name);
        exit when stu_cur%notfound;
  end loop;
  close stu_cur;
end;
/
◎所有的异常都可以通过 others来fetch
如:

declare
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
  v_cnt number:=0;
  v_str varchar2(200);
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
   cursor stu_cur(vv_id number) is
          select id ,name from stu_tb01 where id <vv_id;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
  loop
     dbms_output.put_line('hello');
     if(v_cnt>=10) then
 exit;
     end if;
      v_cnt:=v_cnt+1;
  end loop;
  while  v_cnt>0 loop
     dbms_output.put_line('hello');
     v_cnt:=v_cnt-1;
  end loop;
  for v in reverse 1..5 loop
    dbms_output.put_line('huangzh');
  end loop;
  if (true) then
     null;
  end if;
  open stu_cur(600);
  loop
      fetch stu_cur into v_id,v_name;
          dbms_output.put_line(v_id);
  dbms_output.put_line(v_name);
        exit when stu_cur%notfound;
  end loop;
  close stu_cur;
  exception
 when others then
        dbms_output.put_line('occur exception');
end;
/

◎创建一个存储过程
create or replace procedure firstProc is
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
  v_cnt number:=0;
  v_str varchar2(200);
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
   cursor stu_cur(vv_id number) is
          select id ,name from stu_tb01 where id <vv_id;
begin
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
  loop
     dbms_output.put_line('hello');
     if(v_cnt>=10) then
 exit;
     end if;
      v_cnt:=v_cnt+1;
  end loop;
  while  v_cnt>0 loop
     dbms_output.put_line('hello');
     v_cnt:=v_cnt-1;
  end loop;
  for v in reverse 1..5 loop
    dbms_output.put_line('huangzh');
  end loop;
  if (true) then
     null;
  end if;
  open stu_cur(600);
  loop
      fetch stu_cur into v_id,v_name;
          dbms_output.put_line(v_id);
  dbms_output.put_line(v_name);
        exit when stu_cur%notfound;
  end loop;
  close stu_cur;
  exception
 when others then
        dbms_output.put_line('occur exception');
end firstProc;

◎给一个创建存储过程的权利:grant create procedure to student
◎给一个创建函数的权利:grant create function to student
◎在procedure中的参数in为只能读入的参数out为只能修改的参数inout为即可读入的参数又可修改的参数
◎删除procedure的方法drop procedure 存储过程名
◎创建procedure时参数是不能指定长度的,另外传参数时也可以通过指定参数名来传入,而不需要根据位置传入
如:exec firstproc (p_str=>'huangzh',p_num=>999) --根据名称来指定传入参数
    exec firstproc ('huangzh',999)--根据位置来指定传入参数
◎在procedure中也可以调用其他procedure
◎在procedure中嵌套function并设置其默认值
如:
create or replace procedure firstProc
(
 p_str in varchar2:='call parameter',
 p_num in number:=100
)
is
  v_name varchar2(20):='huangzh';
  v_id number:=999;
  c_id constant number:=1000;
  v_cnt number:=0;
  v_str varchar2(200);
  v_num number;
   type my_rec is record
   (
      v1 number,
      v2 varchar(20)
   );
   v_rec stu_tb01%rowtype;
   type t_StuTable is table of stu_tb01 %rowtype index by binary_integer;
   v_student t_StuTable;
   cursor stu_cur(vv_id number) is
          select id ,name from stu_tb01 where id <vv_id;
   --define function
   function my_func
   return number
   is
      f_num number;
   begin
      f_num :=20;
      return f_num;
   end my_func ;
begin
  v_num:=my_func;
  dbms_output.put_line(v_name);
  dbms_output.put_line(c_id);
  select name into v_name from stu_tb01 where id=v_id;
  dbms_output.put_line(v_name);
  select * into v_rec from stu_tb01 where id=v_id;
  dbms_output.put_line(v_rec.id);
  dbms_output.put_line(v_rec.name);
  select * into v_student (1001) from stu_tb01 where id=v_id;
  dbms_output.put_line(v_student(1001).id);
  dbms_output.put_line(v_student(1001).name);
  loop
     dbms_output.put_line('hello');
     if(v_cnt>=10) then
 exit;
     end if;
      v_cnt:=v_cnt+1;
  end loop;
  while  v_cnt>0 loop
     dbms_output.put_line('hello');
     v_cnt:=v_cnt-1;
  end loop;
  for v in reverse 1..5 loop
    dbms_output.put_line('huangzh');
  end loop;
  if (true) then
     null;
  end if;
  open stu_cur(600);
  loop
      fetch stu_cur into v_id,v_name;
          dbms_output.put_line(v_id);
  dbms_output.put_line(v_name);
        exit when stu_cur%notfound;
  end loop;
  close stu_cur;
  dbms_output.put_line('hello procedure');
  dbms_output.put_line(p_str);
  dbms_output.put_line(v_num);
  exception
 when others then
        dbms_output.put_line('occur exception');
end firstProc;

 

◎包只能存储在数据库中

◎创建一个声明后的包
如:
create or replace package my_pkg
is
   g_num number;
   procedure pkg_proc(p1 number:=0,p2 varchar);
   function pkg_func(f1 number,f2 varchar) return number;
end my_pkg;


◎函数,过程可以重载,首要条件是参数不同,但是varchar和varchar2是不能区分的
◎创建一个包体
create or replace package body huang_pkg
as
  procedure pkg_proc(p1 number:=0,p2 varchar)
  is
  begin
       null;
       --dbms_output.put_line('hello procedure');
  end pkg_proc;

  function pkg_func(f1 number:=0,f2 varchar)
  return number
  is
       v_num number;
  begin
       v_num:=20;
       dbms_output.put_line('hello function');
       return v_num;

  end pkg_func;

begin
  g_num:=200;
end huang_pkg;
◎创建一个触发器
◎一个表最多装12个触发器
◎trigger中不能装有commit或者transaction等事务语句
◎在触发器中不能有修改变化表的内容,所谓变化表就是装有该trigger的表,同时不能有修改限制表的主键以及unique的字段等。所谓限制表就是与变化表有完整性一致的表
*************************************************************
◎proc的预处理过程
proc my_proc.pc=>my_prog.c
gcc my_prog.c -lclntsh -L(lib所在的路径或使用shell变量名称)
◎能够在c语言,sql语言中引用的变量就为sql变量
◎在oracle版本中可以被sql使用的变量必须被一下语句括起来,但在版本8上都无需如此申明
execute sql begin declare section
...//相关c变量

execute sql end declare section
◎要将查询后的结果给c语言变量需在变量前添加“:”
如:
char v_name[20];
int i;
short i_name//指示器变量
exec sql
   select name into :v_name indicator :i_name from students where id=:i
◎执行sql语句用exec sql
◎指示器变量short 变量名,根据指示器变量来判断sql变量是否有值,是否查询到
◎判断操作是否成功,可以使用指示器变量
◎可以使用数组进行循环插入
◎将数据库中的数据赋予给proc的字符串变量,oracle不会给proc变量最后补0,可以使用等价的方法
例如:
char name[41];
exec sql var name is string[41]

exec sql
     create table student
     (
 cname varchar(40),
     )
exec sql
     select cname into :name where cid=id;
则oracle则会在赋予给name时自动补0,否则需要人工补
◎内部数据类型可以直接用在oracle中,外部数据类型则是用在proc中
◎可以通过sql通讯区来查看数据操作之后的
◎编译proc .c文件的方法:
例如:
gcc connDB.c -lclntsh -L$ORACLE_HOME/lib -i$ORACLE_HOME/precomp/public
◎可以使用for:row方法来指定插入多少条记录,而不是数组中所有元素全部插入到表中
◎在静态动态 语句都不可以使用select(不包括动态语句3)

 

 

 


{

◎sys.dbms.execute()执行不起来
如:
v_str:='select * from stu_tb01 where id=v_id';
  sys.dbms_sql.execute(v_str);
◎如何自定义异常?
◎为什么所定义的procedure不能显示出结果,但执行成功
◎什么是sqllib
◎proc中如何执行c++的proc

 


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值