PL/SQL学习笔记(二)
select * from protype;
select * from product;
---笛卡尔连接查询(交叉连接)
select * from protype,product;
select * from protype cross join product;---标准写法
---内链接查询
select * from protype pt,product pd where pt.pt_id=pd.p_type;
select * from protype pt inner join product pd on pt.pt_id=pd.p_type;---标准写法(inner可省略不写)
---外连接查询(outer均可省略)
--左外连接查询(左边全部显示,右边匹配显示,匹配不到则补空格)
select * from protype pt left outer join product pd on pt.pt_id=pd.p_type;
--右外连接查询(右边全部显示,左边匹配显示,匹配不到则补空格)
select * from protype pt right outer join product pd on pt.pt_id=pd.p_type;
--全外键连接查询(左右两边相互匹配显示,匹配不到相互补空格)
select * from protype pt full outer join product pd on pt.pt_id=pd.p_type;
--在产品信息表中显示产品类别名称
select pd.*,pt.pt_name from product pd left join protype pt on pd.p_type=pt.pt_id;
---创建视图:(创建视图前需要系统管理员对当前用户(模式、方案)进行授权:grant create view to x1;)
create view v_pt
as
select pd.*,pt.pt_name from product pd left join protype pt on pd.p_type=pt.pt_id;
--视图的使用:
select * from v_pt where p_id='137';
--查询当前用户拥有的所有视图:
select * from user_views;
---创建序列(等差数组)
create sequence myseq1
start with 5 ---从5开始
increment by 3 ---每次自增3
nomaxvalue ---不限制最大值
nocycle ---不循环(不回退)
cache 20; ---为了提高效率,预先缓存20个序列值
---------------------------
select myseq1.nextval from dual; ---获取下一个序列值
select myseq1.currval from dual; ---获取当前序列值(最近一次获取的值)
insert into protype values('B'||myseq1.nextval,myseq1.currval||'号产品类型'); ---利用序列向表格中插入不同编号
select * from protype;
alter sequence myseq1 increment by 1; ---修改序列(注意:序列初始值一旦定义则不可修改,其他参数均可修改)
---PL/SQL程序:
---入门示例:利用循环求1~100累加之和
declare --参数定义区
s integer :=0;
i integer;
begin --程序开始
i:=1;
loop --循环开始
if i>100 then --if条件判断(或用when:exit when i>100 )
exit; --结束循环
end if;
s:=s+i;
i:=i+1;
end loop; --循环结束
dbms_output.put_line('s='||s); --控制台输出语句(如需在命令窗口输出需在程序末尾加''/"并开启服务器输出 :set serveroutput on)
end; --程序结束
-----while循环-----------
declare
s integer :=0;
i integer;
begin -
i:=1;
while i<100 loop
s:=s+i;
i:=i+1;
end loop;
dbms_output.put_line('s='||s);
end; --程序结束
-----for循环-----------
declare
s integer :=0;
begin
for i in 1..100 loop
s:=s+i;
end loop;
dbms_output.put_line('s='||s);
end;
--------PL/SQL语句查询结果必须赋值(into cnt)否则报错-----------
declare
cnt integer;
begin
select count(*) into cnt from protype; --
dbms_output.put_line(cnt);
end;
----------查询一条记录并打印1---------------
declare
pid varchar2(20);
pname varchar2(20);
begin
select * into pid,pname from protype where pt_id='6666';
dbms_output.put_line(pid||','||pname);
end;
select * from protype;
----------查询一条记录并打印(简化)2---------------
declare
---定义一个记录类型:
type PT is record(
pid varchar2(20),
---取字段类型
pname protype.pt_name%type ---protype.pt_name%type:表示表protype中pt_name字段的类型
);
mypt PT; --定义一个PT类型的变量pt
begin
select * into mypt from protype where pt_id='6666';
dbms_output.put_line(mypt.pid||','||mypt.pname);
end;
------------查询一条记录并打印(简化)3-------------------------
declare
---table(表名)%rowtype:获取表中记录的类型
mypt protype%rowtype; ---定义表中记录(行)类型对象
begin
select * into mypt from protype where pt_id='6666';
dbms_output.put_line(mypt.pt_id||','||mypt.pt_name); ---记录类型对象调用字段名
end;
--------游标(代表一个查询出来的结果(集),其内部有读取记录的指针(默认指向第一行之前))---------
---1:游标是从数据表中提取出来的数据,以临时表的形式存放在内存中
---2:利用 fetch 语句可以移动游标内部的指针,从而对游标中的数据进行各种操作
---分类:
--->显式游标
--->隐式游标
--------------------------------------------显式游标-------------------------------------------------------------------
---------示例1:查询并打印产品类型表protype的所有记录------------
declare
--定义一个游标(常量)
cursor cur
is
select * from protype;
pt protype%rowtype;
begin
if not cur%isopen then ---如果游标未打开,则打开游标(isopen:游标的一个属性)
open cur;
end if;
loop
--将当前游标指向的记录赋给pt
fetch cur into pt; --fetch必须与into配套使用,即每次推动游标都必须将游标对应的记录赋值
-- exit when cur%rowcount >5; (只查询前5条记录,rowcunt:游标的一个属性)
exit when cur%notfound; --如果没找到游标则退出(notfound:游标的一个属性)
dbms_output.put_line(pt.pt_id||','||pt.pt_name);
end loop;
close cur; ---关闭游标
end;
--------示例二:游标(配合for循环)简化使用--------------
declare
cursor cur
is
select * from protype;
begin
for pt in cur loop --将查出来的记录类型自动匹配给pt,并且自动打开、关闭游标
dbms_output.put_line(pt.pt_id||','||pt.pt_name);
end loop;
end;
-----------------示例二另一种写法------------------
declare
begin
for pt in (select * from protype) loop --游标本身就是一个查询语句(的结果)
dbms_output.put_line(pt.pt_id||','||pt.pt_name);
end loop;
end;
-------------------示例三:带参数的游标------------------------------------
declare
cursor cur(pname varchar2) ---此处参数类型varchar2不用表明长度
is
select * from protype where pt_name like '%'||pname||'%';
begin
for pt in cur('2') loop
dbms_output.put_line(pt.pt_id||','||pt.pt_name);
end loop;
end;
-----------------------------------------------隐式游标-----------------------------------------------------------
---sql:隐式游标,表示刚刚执行过的sql查询结果(集)
declare
cnt integer;
begin
select count(*) into cnt from protype;
dbms_output.put_line(sql%rowcount); --此处sql为上行查询语句的执行结果
dbms_output.put_line(cnt);
end;
---------删除产品类型编号p_id为4位的记录方式一
declare
begin
delete from protype where pt_id like '____';
dbms_output.put_line(sql%rowcount); --此处sql为上行查询语句的执行结果
end;
---------删除产品类型编号p_id为4位的记录方式二
declare
begin
delete from protype where length(pt_id)=4;
dbms_output.put_line(sql%rowcount); --此处sql为上行查询语句的执行结果
end;
rollback;
select * from protype;
select * from v_pd;
---开发一个无参的存储过程:打印产品类型表的记录数
create or replace procedure myproc1
is
cnt integer;
begin
select count(*) into cnt from protype;
dbms_output.put_line(cnt);
end;
---测试调用存储过程myproc1
declare
begin
myproc1;
end;
--sqlplus命令窗口测试:
set serveroutput on; --开启控制台输出服务
execute myproc1; --execute执行该存储过程
exec myproc2; --execute 可简写为exec
--开发一个仅有输入参数的存储过程:按编号条件打印员工的记录数
create or replace procedure myproc2
(v_id in varchar2) --v_id 为输入参数
is
cnt integer;
begin
select count(*) into cnt from protype where pt_id like '%'||v_id||'%';
dbms_output.put_line(cnt);
end;
---测试调用存储过程myproc2
declare
begin
myproc2('1');
end;
--开发一个带有输出参数的存储过程:按编号条件查询员工的记录数
create or replace procedure myproc3
(v_id in varchar2,cnt out integer) --cnt 为输出参数
is
begin
select count(*) into cnt from protype where pt_id like '%'||v_id||'%';
end;
--测试带有输出参数的存储过程myproc3
declare
n integer;
begin
myproc3('1',n);
dbms_output.put_line(n);
end;
--开发一个有输出游标参数的存储过程
create or replace procedure myproc4
(cur out sys_refcursor) -- sys_refcursor为游标变量类型
is
begin
open cur for select * from protype;
end;
--测试
declare
cur sys_refcursor;
pt protype%rowtype;
begin
myproc4(cur);
loop
fetch cur into pt;
exit when cur%notfound;
dbms_output.put_line(pt.pt_id||','||pt.pt_name);
end loop;
close cur;
end;
--开发一个有返回游标的函数(函数必须要有返回值 )
create or replace function myfun1
(v_id in varchar2)
return sys_refcursor
is
cur sys_refcursor;
begin
open cur for select * from protype where pt_id like '%'||v_id||'%';
return cur;
end;
--测试
declare
cur sys_refcursor;
pt protype%rowtype;
begin
cur:=myfun1('1');
loop
fetch cur into pt;
exit when cur%notfound;
dbms_output.put_line(pt.pt_id||','||pt.pt_name);
end loop;
close cur;
end;
--触发器测试(利用触发器实现主键自增)
create table mytab(
t_id number primary key,
t_name varchar2(30)
);
create sequence myseq3
start with 100
increment by 1
nomaxvalue
nocycle
cache 20;
create or replace trigger mytab_pkauto
before insert on mytab
for each row
when(new.t_id is null) -- new 为关键字,代表了新记录,old代表旧记录
declare
begin
select myseq3.nextval into :new.t_id from dual;
end;
insert into mytab(t_name) values('kkkk');
select * from mytab;
PL/SQL学习笔记(二)的更多相关文章
- ORALCE PL/SQL学习笔记
ORALCE PL/SQL学习笔记 详情见自己电脑的备份数据资料
- 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中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...
- PL/SQL学习笔记之集合
一:PL/SQL集合 集合是一个有序且存有相同的类型数据的数据结构. PL/SQL提供了三种集合类型: 索引表(关联数组) 嵌套表 数组 二:索引表:一个索引表(也叫关联数组)是一组键 - 值对.每个 ...
- PL/SQL学习笔记之异常
一:异常 程序执行过程中出现错误情况被称为异常,主要有两种类型的异常: 系统定义的异常 用户定义的异常 二:系统定义的异常 Exception Oracle Error SQLCODE 描述 ACCE ...
- PL/SQL学习笔记之游标
一:游标 Oracle会创建一个上下文区域,用于处理SQL语句,其中包含需要处理的语句.处理结果等等. 游标指向这一上下文的区域. PL/SQL通过控制游标在上下文区域移动,来获取SQL语句的结果信息 ...
随机推荐
- POJ3624(01背包:滚动 实现)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30417 Accepted: 13576 ...
- vue全局配置
Vue.config 是一个对象,包含Vue的全局配置.可以在启动应用之前修改下列的属性: Vue.config.slient=true; 取消Vue所有的日志与警告 默认值false ...
- Struts过滤器
StrutsPrepareAndExecuteFilter过滤器其实是包含2部分的 StrutsPrepareFilter:做准备 StrutsExecuteFilter:进入Struts2的核心处理 ...
- windows下mysql5.1忘记root密码解决方法[win7]
步骤如下:1.停止mysql服务(以管理员身份,在cmd命令行下运行) net stop mysql2.使用 mysqld –skip-grant-tables 命令启动mysql数据库 D:\> ...
- HDOJ-1021
Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- error: templates may not be ‘virtual’
模板函数不能是虚函数,原因如下: 首先呢,模板函数并不是函数,他需要特定的类型去实例化成为函数.你定义一个函数模板,是不生成任何函数的,只有当你用代码去调用它时,才会根据你的类型去实例化成为特定 ...
- Numpy学习笔记<1>
1 numpy的ndarray:一种多维数组 a:创建ndarry 注意:np.array会尝试为新建的数组一个合适的数据类型 保存在dtype中 b:嵌套序列转换为一个多维数组 c:输出数据类型 ...
- Flutter实战视频-移动电商-12.首页_GridView类别导航制作
12.首页_GridView类别导航制作 首页导航区的制作 外面用一个gridview来写.里面单独提出来 新建导航组件 还是在home_page.dart里面写代码 新建一个静态的组件: 快捷键写组 ...
- lightoj1063【求割点】
题意不懂啊...... 只知道求割点. #include <bits/stdc++.h> using namespace std; typedef long long LL; typede ...
- HDU5918【KMP大法好,虽然我不会】
#include <bits/stdc++.h> using namespace std; typedef long long LL; const; int n,m; int a[MAX] ...