PL/SQL学习笔记(三)
-----创建一个序列,再创建一个表(主键是数字),通过序列生成该表的主键值。
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学习笔记(三)的更多相关文章
- ORALCE PL/SQL学习笔记
ORALCE PL/SQL学习笔记 详情见自己电脑的备份数据资料
- PL/SQL学习笔记之集合
一:PL/SQL集合 集合是一个有序且存有相同的类型数据的数据结构. PL/SQL提供了三种集合类型: 索引表(关联数组) 嵌套表 数组 二:索引表:一个索引表(也叫关联数组)是一组键 - 值对.每个 ...
- PL/SQL学习笔记之变量、常量、字面量、字符串
一:变量 1:变量声明与初始化 variable_name datatype(约束) [:= | DEFAULT 初始值] 如: sales , ); name ); a ; greetings ) ...
- PL/SQL学习笔记之基本块格式与语法
一:PL/SQL程序块 PL/SQL是一种块结构的语言,一个PL/SQL程序就是一个 代码逻辑块. PL/SQL程序由三部分构成: 1 声明 部分 使用关键字DECLARE开头,它是一个可选的部分,用 ...
- Oracle之PL/SQL学习笔记
自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了一下,给大家分享,有不足之处还望大家 ...
- [Oracle] PL/SQL学习笔记
-- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...
- PL/SQL学习笔记程序单元
一:程序单元组成 一个PL/SQL程序单元主要包括三部分: 声明与定义部分:声明变量.常量.类型等:定义过程.函数等: 执行部分:执行PL/SQL语句:调用过程.参数:处理游标等: 异常处理部分:处理 ...
- PL/SQL学习笔记之日期时间
一:PL/SQL时间相关类型 PL/SQL提供两个和日期时间相关的数据类型: 日期时间(Datetime)数据类型 时间间隔类型 二:日期时间类型 datetime数据类型有: DATE TIMEST ...
- PL/SQL学习笔记之包
一:包 包是由一组相关的函数,过程,变量,游标等PL/SQL程序设计元素的组合而成的一个PL/SQL程序单元,相当于Java中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...
随机推荐
- code::Blocks 汉化经验
首先,在网上下载一个code::Block的汉化包 code::Block汉化包下载地址,复制到地址栏即可. http://files.cnblogs.com/files/QW-lzm/codebl ...
- Java 猫扑(mop)打卡小应用
唉 mop又没打卡,前面十几天全没啦,像我们这些IT码农虽然天天上网,但是总是忘记打卡,这不一失足生成千古恨,失败了撒.好不容易每次打卡都能得几百份的,唉.1. [代码][Java]代码 pac ...
- 合并table中某一列相邻的相同的行
合并table中某一列相邻的相同的行1. [代码]合并table中某一列相邻的相同的行 <!DOCTYPE html><html> <head> ...
- Android ViewDragHelper及移动处理总结
概述 2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用.我们知道在我们实际的开发中往往会涉及到很多的拖动效 ...
- [Selenium] 如何绕过 IE 的安全模式
自从 IE7 引入 Protected Mode 以来, IE 浏览器的安全性的确得到了一定程度的提高.其原理从本质来讲,在浏览某些需要启用保护模式的页面时,会开启一个新的浏览器会话以完成任务,而此时 ...
- Linux命令排查线上问题常用的几个
排查线上问题常用的几个Linux命令 https://www.cnblogs.com/cjsblog/p/9562380.html top 相当于Windows任务管理器 可以看到,输出结果分两部分, ...
- 洛谷P3830 [SHOI2012]随机树——概率期望
题目:https://www.luogu.org/problemnew/show/P3830 询问1:f[x]表示有x个叶节点的树的叶节点平均深度: 可以把被扩展的点的深度看做 f[x-1] ,于是两 ...
- const 和指针
c++用了那么久,觉得 const 和指针配合到一起的时候就会有点点分不出来. 如下: const Data* pData; Data const * pData Data * const pData ...
- 【旧文章搬运】Windbg+Vmware驱动调试入门(三)---Windbg基本调试入门
原文发表于百度空间,2009-01-09========================================================================== 这一节的内 ...
- js获取动态日期时间
var timer=null; function tt(n){ if(n<10){ return '0'+n }else{ return n+'' } } timer=setInterval(f ...