Oracle数据库基础知识
对于一位程序员来说并不需要完全掌握Oracle的所有知识,毕竟自己不是DBA。在日常开发中也用不到那些命令和工具,但是有些知识点我们还是必须得熟练的掌握它们。比如:一些基本的DDL和DML语句,存储过程,函数,视图,触发器,序列,游标,自定义类型和包。下面罗列下oracle的基础知识。 参数赋值 Sql Server Set @parameter=5 --加不加分号无所谓,这里是用‘=‘号进行赋值的 Oracle Parameter:=5;--必须加分号,并使用加‘:‘的’=‘号进行赋值的 PL结构。在Sql Server中,采用的是批处理执行任务的方式,所以可以将多条sql语句选中批量执行,而不用顾忌要在专门的地方声明变量,在专门的地方进行逻辑编码。在Oracle中采用的是PL编程方式,必须在专门的地方声明变量,在专门的地方进行流程编码,经典的PL如下: Declare --这里是专门用来定义变量的 Begin --这里是专门用来进行程序编码的 End;--这里必须加分号 If语句 If ***条件then Else if ***条件then Else End if;--这里要加分号 Case语句 Case When ***条件then When ***条件then When ***条件then Else End Case;--这里要加分号 还可以写成: Case常量--一个字符变量 When‘A‘then When‘B‘then When‘C‘then Else End Case;--这里要加分号 循环结构,要达到循环在Oracle中有3种方式,各有各的好处,你懂的。它们分别如下: 第一种 Loop **** Exit when退出条件; End loop;--要加分号 第二种 While条件loop **** End loop;--要加分号 第三种 For I in 1..100 loop *** End loop; --要加分号 PL结构中的错误处理 就像C#中的Try{} Catch{}语句块能捕获错误。写几个例子: HelloWorld级别的错误抛出例子 declare stu_info student%rowtype; cursor stu_cursor is select * from student; begin select * into stu_info from student; exception when TOO_MANY_ROWS then dbms_output.put_line('行太多'); when others then dbms_output.put_line('未知错误 错误号:'||sqlcode||'错误信息'||sqlerrm); end; 手动抛出错误,类似于c#中的throw declare stu_info student%rowtype; cursor stu_cursor is select * from student; begin Raise_Application_Error(-20001,'打酱油的错误');--显示抛出错误 exception when TOO_MANY_ROWS then dbms_output.put_line('行太多'); when others then dbms_output.put_line('未知错误 错误号:'||sqlcode||'错误信息'||sqlerrm); end; 自定义一个错误,并把它手动抛出 declare my_error Exception; pragma Exception_Init(my_error,-29999);--这里很重要哦 begin raise my_error;--抛出错误 exception when others then dbms_output.put_line('未知错误 错误号:'||sqlcode||'错误信息'||sqlerrm); end; Record类型 Oracle中的Record类型类似于c语言中的结构体,主要用来接收Select语句或游标中返回的数据,下面写个例子: declare type student_record_type is record( stu_name student.name%type, stu_age student.age%type ); student_recordstudent_record_type;--这里很重要,不能直接在类型上操作 begin select name,age into student_record from student where id=&id; dbms_output.put_line(student_record.stu_name||' '||student_record.stu_age); end; DDL语句 这里的DDL语言主要是指能完成如下工作的DDL语言:创建表,创建表的主/外 键及级联效果, 建表: Create Table student( StuId number(5), StuName varchar2(20), StuAge number(2) ) Create Table class( StudentId number(5), TeacherId number(5), ClassName varchar2(30) ) Create Table teacher ( tId number(5), tName varchar2(30), tSalary number(5,2) ) Alter Table class Add Constraint p_k Primary Key (StudentId,TeacherId) Alter table student Add Constraint p_k Primary Key (StuId) Alter Table class Add Constraint f_k_1 Foreign Key (StudentId) references student(id) on delete cascade Alter Table class Add Constraint f_k_2 Foreign Key (TeacherId) references student(tId) on delete cascade DML语句 Select语句。Oracle中的Select语句的使用方法与Sql Server差不多,但还是有些不同之处。 赋值方式不同: Sql Server: Select @peopleNumber=count(*) from people Oracle: Select count(*) into peopleNumber from people 内连接 Sql Server Select s.id,s.name from student s inner join class c on s.id=c.studentid where c.classname=’***’ Oracle: Select s.id,s.name from student s inner join class c on s.id=c.studentid where c.classname=’***’ 左连接 右连接
全连接
Insert语句。Oracle中的Insert语句比Sql Server中的强大很多,废话不多说,看例子:
单条数据的插入 Insert into student(id,name,age) values(1,’张三’,22); 插入的数据源来自select语句 Insert into studentfrom select id,age from tmp_student; 根据不同的条件,将数据插入到不同的表中 Insert all when id between 1 and 3 then into x_testtable when id between 4 and 6 then into x_testtable2 select id,name from studentDelete语句 Update语句。 Update student set name=’new’||name where id=1; Delete语句。和标准的sql标准一致,没多大变化。 Delete from student where id=1 视图。视图有虚拟视图和物理视图两种,这里不说后者。创建视图的语法如下: 简单的视图: Create View View_Student as Select * from Student 复杂的视图: Create View Teacher_Student as Select t.name,count(s.id)学生数from student s inner join class c on s.id=c.id inner join teacher t on c.teacherid=t.id Group by t.name 简单视图与复杂视图的区别在于:简单的视图能在视图上对实际存储的数据进行增/删/改 操作而复杂的视图却不能,但如果你实在是要对复杂的视图进行 增/删/改 操作,你可以使用Instead of类型的Trigger来做。 存储过程 废话不多说,看代码: HelloWorld级别的存储过程 create or replace procedure x_print_helloworld as begin dbms_output.put_line('Hello World'); end;--分号是重要滴 带输入参数的存储过程,而且还支持默认值 create or replace procedure x_print_something(msg varchar2 default 'helloworld') as begin dbms_output.put_line(msg); end; 带输出参数的存储过程 create or replace procedure x_getSum(n out number) as begin for i in 1..n loop n:=n+i; end loop; end; 定义了存储过程你得调用呀,看代码: declare begin x_print_helloworld; x_print_something; x_print_something('abc'); x_jc(10); (编辑:台州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |