/*
此文章可以直接作为sql脚本运行,相关的表和数据可以参考前面的一些文章的内容 ![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
*/
/*视图 view */
--使用视图的好处:
---①.数据的安全管理
create view myperson --语法: create view 视图名 as select语句
as
select * from person where personid < 10; --这样 stid >= 6 的数据就被保护起来了,在视图中查询不到了
select * from myperson; --只能查到 personid < 10 的数据
---②.方便多表连接时的查询
--项目中的多表连接查询一般不直接写sql语句,而是先建立视图,再查询视图
create view kscj
as
select sname as 姓名,coursename as 课程名,score as 分数
from studentinfo s inner join exam e
on s.stuid = e.stuid;
select * from kscj; --创建视图后,进行多表连接查询时就不用写一长串语句了,直接查询视图
/*变量定义,语句执行,输出结果,异常处理*/
declarefname varchar2(10) ;
begin
select stname into fname from stuinfo where stid=&n;
dbms_output.put_line('结果是----->'||fname);
exception when NO_DATA_FOUND
THEN dbms_output.put_line('木有查到结果!');
end;
declare --申明变量的部分
fname varchar2(10); --申明的变量 形式为: 变量名 数据类型
begin --语句开始
select pname into fname --将查询到的 pname 的值赋给 fname
from person where personid = 10; --也可以直接给变量赋值 fname := '傅红雪'
dbms_output.put_line('查询到的名字--->'||fname); --输出语句 dbms_output.put_line(字符串)
--这里特别要注意,oracle中字符串连接是用 || 而不是 +
end; --语句结束
--再来看看下面这些语句
declare
fname varchar2(10);
begin
select pname into fname
from person where personid = &n; --&n 表示这个值由自己输入 写成 &a,&b都行 关键是有&
dbms_output.put_line('查询到的名字--->'||fname);
end;
--继续看下面这些语句
declare
fname varchar2(10);
begin
select pname into fname
from person where personid = &n;
dbms_output.put_line('查询到的名字--->'||fname);
exception --异常的处理
when no_data_found then dbms_output.put_line('无结果集!'); --当查询不到数据时,显示 "无结果集!"
end;
/*类型变量 --->类似于c/c++中的结构体变量*/
declaretype mytype is record ( --语法: type 类型名(自己建) is record (子变量)
pname person.pname%type, --这里pname的数据类型是参照的person表的pname字段的数据类型
psex person.psex%type --语法: 变量名 表名.字段名%type
);
recorda mytype; --声明一个上面建立的类型的一个变量
begin
select pname , psex into recorda from person where personid = &n; --将查询的结果放到 recorda 中
dbms_output.put_line('结果--->'||recorda.pname||'---'||recorda.psex);
exception
when no_data_found then dbms_output.put_line('无结果集!');
end;
/*PL/SQL程序语句的结构*/
-- if else 结构
----三种结构 1.if() then 语句; 2. if() then 语句; else 语句; 3.if() then 语句; elsif() then 语句; else 语句 ;declare
money number ;
begin
money := &n;
if (money < 2000)
then money := money + 100 ; --要加 then
elsif (money <5000) --这里不是 else if 而是 elsif
then money := money + 500 ; --要加 then
else
money := money + 1000 ; --不要加 then
end if ; --不管是什么形式的 if else 结构 最后都要加 end if ;
dbms_output.put_line(money);
end;
--case 选择结构
----case 等值比较
declare
deg char(1);
chengji char(4);
begin
deg := '&c'; --字符串手动赋值时的形式 '&c' c可以是任意字符或者字符串
case deg --这里要放变量名
when 'a' then chengji := '优秀'; --语句含义解析: 当上面的变量 deg='a' chengji = '优秀'
when 'b' then chengji := '良好';
when 'c' then chengji := '一般';
end case; --case 结构结束
dbms_output.put_line(chengji);
end;
----case 范围比较
declare
score number;
chengji char(4);
begin
score := &c;
case --这里不要放变量名
when score >= 90 then chengji := '优秀'; --这里变量的值不是一个固定的值,而是一个范围
when score >= 80 then chengji := '良好';
when score >= 70 then chengji := '一般';
when score >= 60 then chengji := '及格';
else chengji := '补考'; --用else简化语句
end case; --case 结构结束
dbms_output.put_line(chengji);
end;
--case在查询中的运用
select k.*,( --运用case结构和加常量列的方法,按条件给表或试图加一列数据
case
when 分数 = 100 then '满分' --这里木有分号隔开
when 分数 >= 90 then '优秀'
when 分数 >= 80 then '良好'
when 分数 >= 60 then '及格'
else '不及格'
end --只有 end 而不是 end case
) as 等级
from kscj k;
/*PL/SQL程序语句的循环结构*/
--循环结构的运用主要是在一次性的插入某些有特殊关联的数据,下面以插入班级编号,和班级名称为例
--用一条sql语句向表classes中插入10条记录,班级号为 1~10,班级名为 计科09+班级号
--形式1: loop 循环
declare
i int := 1;
begin
loop --循环开始
insert into classes values (i,'计科090'||i); --循环内部的语句
exit when i = 9; --退出循环的条件 当i=9时,退出循环
i := i + 1; --变量的自增
end loop; --循环结束
commit; --提交数据
end;
--形式2: while 循环
declare
i int := 10;
begin
while(i < 20) loop --循环条件写在这里 和java语法一样 不要忘记 loop 关键字
insert into classes values(i,'计科09'||i);
i := i + 1;
end loop; --end loop 结束循环
commit;
end;
--形式3: for 循环
begin
for i in 20..30 loop --变量不用申明,直接使用,这里表示 i的取值为[20,21,...29,30],关键字是 in 而不是 =
insert into classes values (i,'计科09'||i);
end loop;
commit;
end;