-----创建一个序列,再创建一个表(主键是数字),通过序列生成该表的主键值。
create table mytb1(
m_id number primary key,
m_name varchar2(20) not null
)
create sequence myseq2
start with 1001
increment by 2
nomaxvalue
nocycle
cache 20; declare
i integer;
begin
i :=1;
while i<=10 loop
insert into mytb1 values(myseq2.nextval,'德玛西亚');
i :=i+1;
end loop; end;
select * from mytb;
---创建表Student,其主键为数值类型:
drop table student;
create table student(
Stu_id number(6) primary key,
Stu_name varchar2(20) not null,
Stu_score number(3,1)
);
---编写一个pl/sql语句块将100条记录插入表中
select * from user_sequences; --查询当前用户下的所有序列 begin
for i in 1..100 loop
insert into student values(myseq2.nextval,'德玛西亚',92.5);
end loop;
end;
select * from student; ---编写一个pl/sql语句块计算表student的平均成绩,并打印
declare
rs number;
begin
select avg(Stu_score) into rs from student;
dbms_output.put_line(rs);
end; ---编写一个pl/sql语句块,打印所有学生信息,如果成绩字段为null,显示为“无”
---方法一:
begin
for stu in (select stu_id,stu_name, nvl(to_char(Stu_score),'无') stu_score from student) loop
dbms_output.put_line(stu.stu_id||','||stu.stu_name||','||stu.stu_score);
end loop;
end;
---方法二
select stu_id, stu_name,
(
case
when stu_score is null then '无'
else to_char(stu_score)
end
) stu_score
from student; ---编写一个pl/sql语句块,打印成绩最高的20名学生信息
delete from student where stu_score not like 'null'; --删除有成绩的学生记录 begin
for i in 1..50 loop
insert into student values(myseq2.nextval,'Frank_Lei',trunc(DBMS_RANDOM.value(30,100),1));--插入30~100之间保留一位小数的随机成绩
end loop;
end;
select * from student; declare
cursor cur
is
select * from student where rownum<=20 order by stu_score desc;
begin
for stu in cur loop
dbms_output.put_line(stu.stu_id||'...'||stu.stu_name||'...'||stu.stu_score);
end loop; end; ---编写一个pl/sql语句块,打印所有学生信息,成绩显示为“合格”、“不合格”和“无”三种
declare
cursor cur
is
select * from student;
begin
for stu in cur loop
case
when stu.stu_score<=0 then dbms_output.put_line(stu.stu_id||'...'||stu.stu_name||'...无');
when stu.stu_score>0 and stu.stu_score<60 then dbms_output.put_line(stu.stu_id||'...'||stu.stu_name||'...不合格');
else dbms_output.put_line(stu.stu_id||'...'||stu.stu_name||'...合格');
end case; end loop;
end; ---利用一条sql语句实现上题功能
select stu_id,stu_name,
(
case
when stu_score<=0 then '无'
when stu_score<60 and stu_score >0 then '不合格'
else '合格'
end
) stu_score
from student; ---编写一个pl/sql语句块,求阶乘
declare
temp number;
rst number;
begin
temp :=1;
rst :=1;
while temp<=4 loop
rst := rst*temp;
temp :=temp+1;
end loop;
dbms_output.put_line(rst);
end;

PL/SQL学习笔记(三)的更多相关文章

  1. ORALCE PL/SQL学习笔记

    ORALCE  PL/SQL学习笔记 详情见自己电脑的备份数据资料

  2. PL/SQL学习笔记之集合

    一:PL/SQL集合 集合是一个有序且存有相同的类型数据的数据结构. PL/SQL提供了三种集合类型: 索引表(关联数组) 嵌套表 数组 二:索引表:一个索引表(也叫关联数组)是一组键 - 值对.每个 ...

  3. PL/SQL学习笔记之变量、常量、字面量、字符串

    一:变量 1:变量声明与初始化 variable_name datatype(约束) [:= | DEFAULT 初始值] 如: sales , ); name ); a ; greetings ) ...

  4. PL/SQL学习笔记之基本块格式与语法

    一:PL/SQL程序块 PL/SQL是一种块结构的语言,一个PL/SQL程序就是一个 代码逻辑块. PL/SQL程序由三部分构成: 1 声明 部分 使用关键字DECLARE开头,它是一个可选的部分,用 ...

  5. Oracle之PL/SQL学习笔记

    自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了一下,给大家分享,有不足之处还望大家 ...

  6. [Oracle] PL/SQL学习笔记

    -- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...

  7. PL/SQL学习笔记程序单元

    一:程序单元组成 一个PL/SQL程序单元主要包括三部分: 声明与定义部分:声明变量.常量.类型等:定义过程.函数等: 执行部分:执行PL/SQL语句:调用过程.参数:处理游标等: 异常处理部分:处理 ...

  8. PL/SQL学习笔记之日期时间

    一:PL/SQL时间相关类型 PL/SQL提供两个和日期时间相关的数据类型: 日期时间(Datetime)数据类型 时间间隔类型 二:日期时间类型 datetime数据类型有: DATE TIMEST ...

  9. PL/SQL学习笔记之包

    一:包 包是由一组相关的函数,过程,变量,游标等PL/SQL程序设计元素的组合而成的一个PL/SQL程序单元,相当于Java中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...

随机推荐

  1. Opencv中视频播放与进度控制

    视频画面本质上是由一帧一帧的连续图像组成的,播放视频其实就是在播放窗口把一系列连续图像按一定的时间间隔一幅幅贴上去实现的. 人眼在连续图像的刷新最少达到每秒24帧的时候,就分辨不出来图像间的闪动了,使 ...

  2. 网易短信接口集成 nodejs 版

    /* name:网易短信服务集成nodejs版: author:zeq time:20180607 test: // checkValidCode('157****6954','284561').th ...

  3. MyBatis学习 之 三、SQL语句映射文件(2)增删改查、参数、缓存

    2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id="getStudent" paramet ...

  4. bzoj 2083 Intelligence test —— 思路+vector

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2083 先把所有子序列都存下来,总长度应该有限制,所以用 vector 存: 要做到 O(n) ...

  5. 865C

    二分+期望dp 好神奇啊...出题人太神了! 我们发现dp之间的关系不满足是一个dag,那么我们只能用高斯消元,但是由于这里是取最小值,需要取min,也不能用高斯消元,于是我们想出了一个奇妙的方法 我 ...

  6. Bootstrap Notify

    https://github.com/mouse0270/bootstrap-notify $.notify('Hello World', { offset: { x: 50, y: 100 } }) ...

  7. vim 退出命令(保存、放弃保存)

    在命令模式中,连按两次大写字母Z,若当前编辑的文件曾被修改过,则Vi保存该文件后退出,返回到shell:若当前编辑的文件没被修改过,则Vi直接退出,   返回到shell. 在末行模式下,输入命令 : ...

  8. JavaScript面向对象轻松入门之综合

    javascrpit面向对象之综合   这一章是对前几章的一个总结,通过一个案例来综合认识javascript面向对象的基本用法   需求: 几乎所有的web应用都需要保存数据一些到本地,那么我们就来 ...

  9. Codeforces - 773A - Success Rate - 二分 - 简单数论

    https://codeforces.com/problemset/problem/773/A 一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<= ...

  10. XTU1266:Parentheses(贪心+优先队列)

    传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...