Oracle数据库学习总结

更新时间:2023-08-28 15:32:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Oracle数据库编程学习心得

Oracle数据库学习总结

时间过的还真快,不知不觉中就在这里呆了半个月了。这段时间里都在学习oracle数据库的编程,毕竟这是家软件外包公司。像我们这样的新员工也就只能接触到些CURD的操作。废话不多说,赶紧来梳理下这半月来学习的知识点. 在来公司之前一直都是使用Sql Server数据库,用Sql Server也开发了3个小型项目。所以对Sql语句以及在数据库中扮演重要作用的存储过程,触发器,视图,主键/外键约束都很熟。但Oracle是一个全新的环境,记得刚装上Oracle的时候,我都不知道在哪查看自己已经建立好的表格。还好有师傅的帮忙,要不然我还真没这么快就能入门Oracle。

学习东西就要学习些能改变自己思维的东西,只有这样才能让自己的眼光比别人更独到,思维比别人更深邃,Oracle就是这样的东西。当然做这样的事是要很大的驱动力的呀,如果公司不是都采用Oracle来写程序的话,我估计也就懒得学啦。

对于一位程序员来说并不需要完全掌握Oracle的所有知识,毕竟自己不是DBA。在日常开发中也用不到那些命令和工具,但是有些知识点我们还是必须得熟练的掌握它们。比如:一些基本的DDL和DML语句,存储过程,函数,视图,触发器,序列,游标,自定义类型和包。

下面我就把这段时间里学习Oracle获得的知识点罗列出来,一是为了方便以后查阅,二是为了和搭档交流学习经验。

要适应的一些细节

从Sql Server转到Oracle进行数据库编程,第一道门槛就是语法问题。很多很多的问题都是因为语法而产生的,现将它们统统集合起来并将它们一网打尽之。

PL结构。在Sql Server中,采用的是批处理执行任务的方式,所以可以将多条sql语句选中批量执行,而不用顾忌要在专门的地方声明变量,在专门的地方进行逻辑编码。在Oracle中采用的是PL编程方式,

必须在专门的地方声明变

Oracle数据库编程学习心得

循环结构,要达到循环在Oracle中有3种方式,各有各的好处,你懂的。它们分别如下:

PL结构中的错误处理

就像C#中的Try{} Catch{}语句块 能捕获错误。写几个例子:

HelloWorld级别的错误抛出例子

Oracle数据库编程学习心得

Record类型

Oracle中的Record类型类似于c语言中的结构体,主要用来接收Select

Oracle数据库编程学习心得

语句或游标中返回的数据,下面写个例子:

DDL语句

这里的DDL语言主要是指能完成如下工作的DDL语言:创建表,创建表的主/外 键及级联效果,

Oracle数据库编程学习心得

DML语句

Select语句。Oracle中的Select语句的使用方法与

Sql Server差不多,但还是有些不同之处。

Oracle数据库编程学习心得

Insert语句。Oracle中的Insert语句比Sql Server中的强大很多,废话不多说,看例子:

视图。视图有虚拟视图和物理视图两种,这里不说后者。创建视图的语法如下:

增/删/改 操作而复杂的视图却不能,但如果你实在是要对复杂的视图进行 增/删/改 操作,你可以使用Instead of 类型的Trigger来做。

Oracle数据库编程学习心得

存储过程

废话不多说,看代码:

函数,和存储过程查不多,唯一的区别就是有返回值,而且还能嵌套在DML语句中使用。下面写几个简单的函数:

Oracle数据库编程学习心得

游标

在Sql中使用的比较少,一直觉得它挺神秘的。最近学习了下,但对它有啥好处还是相知甚少。游标分为以下几类:显示游标,隐式游标。显示游标里面又有匿名游标和非匿名游标,隐式游标是Oracle中提供的某些API接口,通过调用它们能获取某些重要的系统信息,在Sql Server中也有类似的功能如‘@@error‘。

Oracle中的隐式游标:

%notfound 游标专用的隐式游标呀,判断游标中是否还有可返回的数据 %isopen 判断游标是否打开

%rowtype 定义行类型的mycontent student%rowtype 表示开辟一个能包含student表中一条元组的变量空间,并将该地址赋予变量mycontent.

%type 定义列类型的 mycolumns http://www.77cn.com.cn%type 概念同上,开辟一个列。 %rowcount 当前返回的数据行数

Oracle数据库编程学习心得

update student set name='new_bank' where current of stu_info; end if; if stu_record.id=10 then delete from class where studentid=stu_record.id; delete from student where current of stu_info; end if; end loop; close stu_info; end; 带参数的游标的定义及使用 declare cursor classInfo(id number) is select * from class where teacherid=id; class_record class%rowtype; begin open classInfo(1); loop fetch classInfo into class_record; exit when classInfo%notfound; dbms_output.put_line('studentid:'||class_record.studentid||' classname:'||class_record.classname); end loop; close classInfo; end; 简写的游标定义及使用 declare cursor info is select name from student; begin for stuName in info loop dbms_output.put_line(' 第 '||info%rowcount||' '||http://www.77cn.com.cn); end loop; end; 匿名游标的定义及使用 declare begin for stuName in (select * from student) loop dbms_output.put_line(http://www.77cn.com.cn); end loop;

Oracle数据库编程学习心得

游标变量

游标变量与C语言中的指针函数类似。游标变量又分为指明返回类型的游标变量和不指明返回类型的游标变量。

声明返回类型的游标变量

还有几种比较高级的游标写法,不知道工作用会不会用到。它们分别是:

嵌套游标:

Oracle数据库编程学习心得

declare cursor info(n number) is select c.classname,cursor(select http://www.77cn.com.cn from student s where s.id=c.studentid ) from class c where c.studentid=&n; type cursor_type is ref cursor; class_cursor cursor_type; classname class.classname%type; tmp varchar2(100); begin open info(1); loop fetch info into classname,class_cursor; exit when info%notfound; dbms_output.put_line(classname||':'); loop fetch class_cursor into tmp; exit when class_cursor%notfound; dbms_output.put_line(tmp); end loop; end loop; close info; end; 批量返回数据的游标: declare cursor student_cursor is select name from student; type name_table_type is table of varchar(20); name_table name_table_type; begin open student_cursor; fetch student_cursor bulk collect into name_table; for i in http://www.77cn.com.cn_table.count loop dbms_output.put_line(name_table(i)); end loop; close student_cursor; end; 批量返回数据的游标但可以限制每次返回数据的行数的游标 declare cursor student_cursor is select name from student; type name_table_type is table of varchar(20); name_table name_table_type;

Oracle数据库编程学习心得

触发器

触发器好东西呀。很多通过常规方法很难解决的问题通过使用它都能简单

的解决,但它们就是难管理。在Oracle中的触发器分两类:在表级别上的触发器和在数据行上的触发器。

DML After触发器

Oracle数据库编程学习心得

create or replace trigger stu_after_

trigger after insert or update or delete on student begin case when inserting then dbms_output.put_line('添加了一条新纪录 2 when updating then dbms_output.put_line('更新了一条新纪录 2 when deleting then dbms_output.put_line('删除了一条新纪录 2 else dbms_output.put_line('不知道你干了啥子 2 end case; end;

By DML After 触发器'); By DML After 触发器'); By DML After 触发器'); By DML After 触发器');

DML before 行触发器 create or replace trigger stu_row_before_trigger before insert or update or delete on student for each row when (old.id between 10 and 20) --约束条件 去掉后就是对所有的行进行触 发操作 begin case when inserting then dbms_output.put_line('您添加了一条新记录: By DML Row Before 触发 器'); dbms_output.new_line(); dbms_output.put_line('ID:'||:new.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:new.age); when updating then dbms_output.put_line('您更新了一条记录:By DML Row Before 触发器 '); dbms_output.new_line(); dbms_output.put_line('更新前的记录:'); dbms_output.put_line('ID:'||:old.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:old.age); dbms_output.put_line('更新后的记录:'); dbms_output.put_line('ID:'||:new.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:new.age); when deleting then dbms_output.put_line('您删除了一条记录:By DML Row Before 触发器 '); dbms_output.new_line(); dbms_output.put_line('ID:'||:old.id||' Name:'||:http://www.77cn.com.cn||'

Oracle数据库编程学习心得

Age:'||:old.age); end case; end; DML after 行触发器 create or replace trigger stu_row_after_trigger after insert or update or delete on student for each row when (old.id between 10 and 20) --约束条件 去掉后就是对所有的行进行触 发操作 begin case when inserting then dbms_output.put_line('您添加了一条新记录: By DML Row After 触发 器'); dbms_output.new_line(); dbms_output.put_line('ID:'||:new.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:new.age); when updating then dbms_output.put_line('您更新了一条记录: By DML Row After 触发器 '); dbms_output.new_line(); dbms_output.put_line('更新前的记录:'); dbms_output.put_line('ID:'||:old.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:old.age); dbms_output.put_line('更新后的记录:'); dbms_output.put_line('ID:'||:new.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:new.age); when deleting then dbms_output.put_line('您删除了一条记录: By DML Row After 触发器 '); dbms_output.new_line(); dbms_output.put_line('ID:'||:old.id||' Name:'||:http://www.77cn.com.cn||' Age:'||:old.age); end case; end; DML instead of 触发器 instead of 触发器只能作用于视图 create view view_student_table as select * from student; create or replace trigger stu_instead_trigger instead of delete on view_student_table begin if :old.id>50 then delete from student where id=:old.id;

Oracle数据库编程学习心得

类似与c#中的命名空间。它分为两个部分:包规范和包体。它还支持重载能力,在包中可以包含相同名称的函数或存储过程,只有它们的参数不同就不妨碍它们各自的调用。下面写几个包,看代码:

Oracle数据库编程学习心得

as begin dbms_output.put_line('Hello World By Procedure!'); end; function HelloWorld return varchar2 as begin return 'Hello World By Function!'; end; end; 学生管理系统的包 包重载 create or replace package mypackage_student as myVersion nvarchar2(250); procedure printfCopyRight; procedure printfCopyRight(message varchar2); procedure addStudent(stuId number,stuName varchar2,stuAge number); function getStuNumbersByTeacher(tName varchar2) return number; function getStuNumbersByTeacherId(tId number) return number; end; --包规范 create or replace package body mypackage_student as procedure printfCopyRight as begin dbms_output.put_line(myVersion); end; procedure printfCopyRight(message varchar2) as begin dbms_output.put_line(message); end; procedure addStudent(stuId number,stuName varchar2,stuAge number) as begin insert into student values(stuId,stuName,stuAge); end; function getStuNumbersByTeacher(tName varchar2) return number

Oracle数据库编程学习心得

索引表,嵌套表,可变长的表

Oracle数据库编程学习心得

select name into stu_name(-1) from student where id=&id; dbms_output.put_line(stu_name(-1)); end; 嵌套表 declare type nameTable is table of varchar2(50); stu_name nameTable:=nameTable('abc','abc');--用前要初始化 begin select name into stu_name(1) from student where id=&id; dbms_output.put_line(stu_name(1)); end; 可变成的表,这里将它们嵌套起来可以做出 2 维数组的形式。 declare type a1_varray_type is varray(10) of int; type na1_varray_type is varray(10) of a1_varray_type; nv1 na1_varray_type:= na1_varray_type( a1_varray_type(1,2,3), a1_varray_type(4,5,6), a1_varray_type(7,8,9) ); begin for i in 1..nv1.count loop for j in 1..nv1(i).count loop dbms_output.put_line(nv1(i)(j)); end loop; end loop; end; 嵌套表的二维数组形式: declare type family is table of varchar2(20); type familyCollection is table of family; --MyFamily family:=family('jim','ann','bank'); myFamilyCollection familyCollection:=familyCollection( family('jim','ann','bank'), family('jim2','ann2','bank2'), family('jim3','ann3','bank3') ); begin for i in 1..myFamilyCollection.count loop for j in 1..myFamilyCollection(i).count loop

Oracle数据库编程学习心得

上面说的这3类表都支持下面这些方法:

Count,limit,first,last,prior,next,extend,trim

本文来源:https://www.bwwdw.com/article/18hi.html

Top