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中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...
随机推荐
- js split分割字符串成数组
str = "2,2,3,5,6"; //这是一字符串 var strs = new Array(); //定义一数组 strs = str.split("," ...
- 一步一步学Silverlight 2系列(31):图形图像综合实例—实现水中倒影效果
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- WAS:节点不同步问题
刀片服务器硬盘坏了,换了硬盘后,通过dmgr无法重启该节点上的server. 单机./starServer 后,服务虽然启动了,但后台一直提示如下: [-- ::: CST] RoleViewLead ...
- 【Java】DateUtil(1)
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- golang copy函数
数组切片内容复制 转自:http://studygolang.com/articles/4560 用于将内容从一个数组切片复制到另一个数组切片.如果加入的两个数组切片不一样大,就会按其中较小的那个数组 ...
- 字符串转Unicode码
var str = '中'; var charCode = str.charCodeAt(0); console.log(charCode); // => 20013; str.charCode ...
- Bootstrap-CSS:响应式实用工具
ylbtech-Bootstrap-CSS:响应式实用工具 1.返回顶部 1. Bootstrap 响应式实用工具 Bootstrap 提供了一些辅助类,以便更快地实现对移动设备友好的开发.这些可以 ...
- pl/sql developer安装使用即时客户端
pl/sql developer安装使用即时客户端 背景:由于Oracle的安装比较麻烦,大部分人都使用远程Oracle,而Oracle Instantclient Basic package可以解决 ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 16. 角色管理
注入UserManager和RoleManager 建立View页面.这段视频中没有录. RoleManager的服务没有注册 注册的地方进行修改 再次运行就可以了 这个ViewModel实际上只需要 ...
- 任务47:Identity MVC:ReturnUrl实现
任务47:Identity MVC:ReturnUrl实现 在最上面加一个私有的方法 登陆也加上returnUrl Login的post方法.加入returnUrl的参数 登陆界面也需要加上 asp- ...