-- 感觉有用点个赞呗^v^

select * from emp;drop view persin_vw;--删除视图create table emp as select * from scott.emp;  --复制scott用户下的emp表--视图:视图就是封装了一条复杂的查询语句
create view emp_vw as
select * from emp e where e.deptno = 20;    --创建一个视图,视图显示的是编号为20的全部员工select * from emp_vw e;      --创建好的视图,可以进行查询create or replace view emp_vw as
select * from emp e where e.deptno = 10;    --创建一个视图,如果存在相同名称的则会覆盖,如果没有,创建create or replace view emp_vm as
select * from emp e where e.deptno = 20 with read only;   --创建一个只读视图--索引:可以加快查询速度,但会降低增删改速度
--单列索引,建立在一列上的索引
create index eid on emp(ename);  --eid:索引名,   emp:表名,   ename:列名
--复合索引:建立在多个列上
create index pid_pname on person(pid,pname);
--索引的使用规则/*在大表上建立索引才有意义在where子句后面或者是连接条件上的字段上建立索引表中数据频繁增删改,不建议进行添加索引*/--plsql基本语法
--为职工涨工资,每人涨 10%的工资。
update emp set sal=sal*1.1;
--按职工的职称长工资,总裁涨 1000元,经理涨800 元,其他人员涨 400 元。无法用一条sql进行操作,借助plsql/*语法结构declare说明部分 (变量说明,游标申明,例外说明 〕begin语句序列 (DML 语句〕…exception例外处理语句End;*/
--定义变量: 变量名 变量类型
varl number(2);
psal char(15);--定义常量
married constant boolean := true;        -- :=  表示赋值
--引用型变量
myname emp.ename%type;      --引用emp表中ename的类型
--使用into进行赋值
declareemp_name emp.ename%type;          --定义一个变量emp_name 类型和emp表中的ename数据类型是一样的
beginselect e.ename into emp_name from emp e where e.empno = 7369;   --查询emp表中empno=7369的ename,将值赋值给dbms_output.put_line(emp_name);      --打印显示变量中的值
end;--记录型变量
emp_mytype emp%rowtype;         --可以记录emp表的一行类型
--记录变量分量的引用
emp_rec.ename:='ADAMS';
--例子
declarep emp%rowtype;
beginselect * into p from emp e where e.empno = 7369;      dbms_output.put_line(p.ename || '的工资为' || p.sal);     -- || 表示连接符号
end;--if分支
--语法1:如果从控制台输入 1 则输出我是 1
declarepnum number := #
beginif pnum = 1 thendbms_output.put_line('我是' || pnum);end if;
end;
--语法2:如果从控制台输入 1 则输出我是 1否则输出我不是 1
declarepnum number := #
beginif pnum = 1 thendbms_output.put_line('我是' || pnum);elsedbms_output.put_line('我不是1');end if;
end;
--语法3:判断人的不同年龄段 18岁以下是未成年人,18岁以上 40以下是成年人,40以上是老年人
declarepnum number := #
beginif pnum < 18 thendbms_output.put_line('未成年');elsif pnum >= 18 and pnum < 40 thendbms_output.put_line('成年人');elsif pnum >= 40 thendbms_output.put_line('老年人');end if;
end;
--LOOP循环分支
--使用语法 1 输出 1 到10 的数字
declarestep number :=1;    --变量,赋予初始值1
beginwhile step <=10 loopdbms_output.put_line(step);step := step+1;   --没循环一次,+1end loop;
end;
--使用语法 2 输出 1 到10 的数字
declarestep number := 1;
beginloopexit when step > 10;dbms_output.put_line(step);step := step+1;end loop;
end;
--使用语法 3 输出 1 到10 的数字
declarestep number := 1;
beginfor step in 1..10loopdbms_output.put_line(step);end loop;
end;--游标cursor
--语法:CURSOR 游标名 [ (参数名 数据类型,参数名 数据类型,...)] IS SELECT 语句;
cursor c1 is select ename from emp;
--游标的使用步骤/*1.打开游标: open c1  打开游标执行查询2.取一行游标的值    fetch c1 into pjob;    取一行到变量中3.关闭游标: close c1; 关闭游标,释放资源4.游标的结束方式exit when c1%notfound*/
--使用游标方式输出 emp 表中的员工编号和姓名
declarecursor pc is select * from emp;    --创建一个游标pemp emp%rowtype;                  --定义一个记录型变量
beginopen pc;                           --打开游标loop                          --循环遍历fetch pc into pemp;         --提取一行数据,存储到变量中exit when pc%notfound;      --退出条件:没有下一行之时dbms_output.put_line(pemp.empno || '---' || pemp.ename);  --打印end loop;                     --循环结束close pc;                          --关闭资源
end;--按员工的工种涨工资,总裁 1000 元,经理涨 800 元其,他人员涨 400 元。
declarecursor p is select * from emp;addsal emp.sal%type;pemp emp%rowtype;
beginopen p;loopfetch p into pemp;exit when p%notfound;if pemp.job = 'PRESIDENT' then addsal := 1000;elsif pemp.job = 'MANAGER' then addsal := 800;else addsal := 400;end if; end loop;update emp e set e.sal = e.sal + addsal where e.empno = pemp.empno;commit;close p;
end;
--写一段PL/SQL 程序,为10号部门员工涨工资1000元。
declarecursor p is select * from emp;addsal emp.sal%type;pemp emp%rowtype;
beginopen p;loopfetch p into pemp;exit when p%notfound;if pemp.deptno = 10 then addsal := 1000;else addsal := 0;end if;end loop;update emp e set e.sal = e.sal + addsal where e.deptno = pemp.deptno;commit;close p;
end;--存储过程
/*存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的 SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
*/
/*创建存储过程语法:create [or replace] PROCEDURE  过程名[( 参数名 in/out  数据类型)] ASbeginPLSQL 子程序体;End;--或者create [or replace] PROCEDURE  过程名[( 参数名 in/out  数据类型)] isbeginPLSQL 子程序体;End;
*/
--创建一个helloword存储过程
create or replace procedure helloworld as
begindbms_output.put_line('helloworld');
end;
--在plsql中调用存储过程
beginhelloworld;
end;
--删除存储过程
drop procedure helloword;--给指定的员工涨 指定 工资,并打印出涨前和涨后的工资
create or replace procedure addsal(eno in number, addsal in number) ispemp emp%rowtype;
beginselect * into pemp from emp e where e.empno = eno;update emp set sal = nvl(sal,0) + addsal where empno = eno;dbms_output.put_line('涨工资前:' || pemp.sal || '---' || '涨工资后:' || (pemp.sal+addsal));
end;--调用存储过程
beginaddsal(eno => 7369,addsal => 1000);commit;
end;--存储函数
/*create or replace function  函数名(Name in type, Name in type, ...) return  数据类型 is结果变量  数据类型;beginreturn( 结果变量);end;
*/
--使用存储函数来查询指定员工的年薪
create or replace function findsal(eno in emp.empno%type) return number ispemp emp%rowtype;
beginselect * into pemp from emp e where e.empno = eno;return pemp.sal * 12 + nvl(pemp.comm,0);
end;
--调用存储函数
declarevarl number;
beginvarl := findsal(7369);dbms_output.put_line(varl);
end;
/*存储函数和存储过程的区别  图1-1*/
--触发器
/*数据库触发器是一个与表相关联的、存储的 PL/SQL 程序。每当一个特定的数据操作语句(Insert,update,delete)在指定的表上发出时,Oracle 自动地执行触发器中定义的语句序列。
*/
--触发器可用于:
/*1. 数据确认2. 实施复杂的安全性检查3. 做审计,跟踪表上所做的数据操作等4. 数据的备份和同步
*/
--类型
/*
- 语句级触发器 :在指定的操作语句操作之前或之后执行一次,不管这条语句影响了多少行 。
- 行级触发器:触发语句作用的每一条记录都被触发。在行级触 发器中使用 old 和 new伪记录变量, 识别值的状态。
*/
--语法
/*
CREATE [or replace] TRIGGER 触发器名{BEFORE | AFTER}{DELETE | INSERT | UPDATE [OF  列名]}ON 表名[FOR EACH ROW [WHEN( 条件) ] ]
beginPLSQL块
End;
*/
--插入员工后打印一句话“一个新员工插入成功”
create or replace trigger testtriggerafterinsert on person
declare
begindbms_output.put_line('一个员工插入成功');
end;
--在person表中插入数据
insert into person values(12,'songwenhui');
select * from person;--在行级触发器中触发语句与伪记录变量的值--图1-2

--判断员工涨工资之后的工资的值一定要大于涨工资之前的工资
create or replace trigger addsalbefore update of sal on empfor each row
beginif :old.sal >= :new.sal then raise_application_error(-20002, '涨前的工资不能大于涨后的工资');end if;
end;
--调用
update emp t set t.sal = t.sal - 1;  --报错

转载于:https://www.cnblogs.com/Mykebai/p/11006828.html

Oracle_视图_索引_plsql_游标_存储过程_存储函数_触发器相关推荐

  1. oracle 查看函数被哪些触发器引用_oracle如何查看存储过程,存储函数,触发器的具体内容...

    (1)set serveroutput on 实现plsql developer 打印输出 (2)如何查看存储过程,存储函数,触发器的内容 查 user_sources表 eg:查询GET_DEPT_ ...

  2. Oralce数据库之存储过程、存储函数、触发器和数据字典

    Oracle数据库总结 Oracle数据库之集合运算 Oracle数据库之数据处理 Oracle数据库之建表和管理表 Oracle数据库之对象视图.索引.序列.同义词 一.存储过程和存储函数 1.概念 ...

  3. 【MySQL数据库设计与应用(六)】存储程序(存储过程,存储函数,触发器,事件)

    文章目录 1 存储程序介绍 1.1 什么是存储程序 1.2 存储例程 1.3 触发器 1.4 事件 2 创建和调用存储过程 2.1 创建和调用存储过程 2.2 存储过程的参数模式 2.3 存储过程返回 ...

  4. 存储过程和存储函数和触发器示例

    1.存储过程示例:为指定的职工在原工资的基础上长10%的工资 SQL> create or replace procedure raiseSalary(empid in number)aspSa ...

  5. 数据库存储过程与存储函数

    数据库存储过程与存储函数_伱糸淂忄-CSDN博客 MySQL数据库之存储过程与存储函数 - 奥辰 - 博客园 1.数据库存储过程与存储函数 存储过程是一组为了完成特定功能的SQL语句集,经过编译后存储 ...

  6. 第15章_存储过程与函数(创建存储过程、调用存储过程、存储函数的使用、存储过程和函数的查看、修改、删除)

    第15章_存储过程与函数 第15章_存储过程与函数 1. 存储过程概述 1.1 理解 1.2 分类 2. 创建存储过程 2.1 语法分析 2.2 代码举例 3. 调用存储过程 3.1 调用格式 3.2 ...

  7. (第15章 存储过程与存储函数)

    #第15章_存储过程与存储函数#0.准备工作CREATE DATABASE dbtest15;USE dbtest15;CREATE TABLE employees AS SELECT * FROM ...

  8. Oracle的存储过程和存储函数

    存储过程和存储函数:指存储在数据库中供所有用户程序调用的子程序叫做存储过程.存储函数. 他们也是数据库的对象,类似于表.视图.索引.序列.同义词等. 存储函数可以用return返回值,而存储过程不可以 ...

  9. MySQL初级篇——存储过程、存储函数的相关概念及应用举例

    文章目录: 1.什么是存储过程? 2.存储过程操作相关SQL 3.存储过程实操SQL 4.存储函数操作相关SQL 5.存储函数实操SQL 6.存储过程.存储函数的优缺点 1.什么是存储过程? 含义:存 ...

  10. 【宋红康 MySQL数据库】【基础版】【15】存储过程与存储函数

    文章目录 存储过程与存储函数 定义存储过程与存储函数 对比存储函数和存储过程 存储过程概述 理解 分类 创建存储过程 语法分析 代码举例 调用存储过程 调用格式 代码举例 如何调试 存储函数的使用 语 ...

最新文章

  1. 【工具】ApkTools
  2. Python数据科学-技术详解与商业实践视频教程
  3. [Android]ListView控件之Adapter性能优化
  4. C/C++程序从编译到最终生成可执行文件的过程分析
  5. 降低Java占用_如何减少JAVA应用程序的CPU使用率?
  6. tf 矩阵行和列交换_TF-搞不懂的TF矩阵加法
  7. 3. HTML中的容器标签
  8. 素数倒数的级数发散性的一个证明
  9. Oracle+ASM单机环境下,开启归档的最简单的方法
  10. CentOS 7 安装MongoDB 4.0
  11. sql数据库 订阅发布_如何使用中央发布者和多个订阅者数据库设置自定义SQL Server事务复制
  12. 小程序获取用户信息 php发送数据库,qq小程序如何获取用户信息并存入数据库实例...
  13. 微信小程序人脸识别之人脸属性检测
  14. excel打不开xlsx文件怎么办?
  15. 11.2.1 绝对值函数
  16. 【帧率倍频】基于FPGA的视频帧率倍频系统verilog开发实现
  17. c语言函数大全表格形式,C语言函数大全[表格形式].doc
  18. 最优布线问题 题解
  19. C++类外写构造函数实现编译报错:definition of implicitly-decleared ‘函数名’
  20. 网络工程师能做什么?

热门文章

  1. Redis分布式锁的正确实现方式
  2. Intellij Idea 创建maven WebAPP项目
  3. 总结|数学建模的收获
  4. 洛谷 3373 【模板】线段树 2
  5. Hive map阶段缓慢,优化过程详细分析
  6. CentOS_6.x安装VNC_Server
  7. Redis配置文件redis.conf参数详解
  8. urllib urllib2 自己用
  9. windows上传文件到linux乱码解决
  10. WPF MVVM 网易云音乐