-----创建一个序列,再创建一个表(主键是数字),通过序列生成该表的主键值。
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. html5--6-9 CSS选择器6--伪类选择器

    html5--6-9 CSS选择器6--伪类选择器 实例 @charset="UTF-8"; /*:root{background: green}*/ /*li:first-chi ...

  2. 一步一步学Silverlight 2系列(8):使用样式封装控件观感

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  3. 深入浅出Oracle学习笔记:Undo

    undo的作用是:解决oracle多用户读写一致性,以及操作可撤销或者回滚. 1.undo表空间是从10g开始进行自动管理的,几个参数如下: undo_management:回滚段手动管理还是自动管理 ...

  4. nginx最简单的网站配置:单主机+静态文件

    1.域名绑定主机:新建一个A记录,将www的子域名绑定到主机的ip上:2.主机的nginx设置监听端口,绑定域名,修改网站根目录路径和默认首选文件:/etc/nginx/conf.d 这个目录中新建一 ...

  5. RecyclerView 局部刷新(获取viewHolder 去刷新)

    RecyclerView.ViewHolder viewHolder = mRecyclerView.findViewHolderForAdapterPosition(i); if (viewHold ...

  6. ubuntu安装wine+plsql

    1.在ubuntu下装了win7的虚拟机,在使用plsql进行开发的时候发现很慢很卡,经常半天反应不过来.机器是不差的,1w5的thinkstation,实在受不了这种 速度,想着在ubuntu下搞一 ...

  7. UVA-11078(水题)

    题意: 给一个序列,找两个整数a[i],a[j]使得a[i]-a[j]最大; 思路: 从前往后扫一遍;水题; AC代码: #include <bits/stdc++.h> /* #incl ...

  8. BroadcastReceiver中调用Service

    首先是代码: package com.larry.msglighter; import android.content.BroadcastReceiver; import android.conten ...

  9. Codefroces #404 Div2

    A题 分析:把多面体和面数一一对应即可 #include<iostream> #include<map> #include<cstring> #include< ...

  10. 小K的农场(差分约束)

    题目大意 n个点 m条描述 农场 a 比农场 b 至少多种植了 c 个单位的作物. 农场 a 比农场 b 至多多种植了 c 个单位的作物. 农场 a 与农场 b 种植的作物数一样多. 题解 差分约束裸 ...