1. -- 感觉有用点个赞呗^v^
  2.  
  3. select * from emp;
  4.  
  5. drop view persin_vw;--删除视图
  6.  
  7. create table emp as select * from scott.emp; --复制scott用户下的emp
  8.  
  9. --视图:视图就是封装了一条复杂的查询语句
  10. create view emp_vw as
  11. select * from emp e where e.deptno = 20; --创建一个视图,视图显示的是编号为20的全部员工
  12.  
  13. select * from emp_vw e; --创建好的视图,可以进行查询
  14.  
  15. create or replace view emp_vw as
  16. select * from emp e where e.deptno = 10; --创建一个视图,如果存在相同名称的则会覆盖,如果没有,创建
  17.  
  18. create or replace view emp_vm as
  19. select * from emp e where e.deptno = 20 with read only; --创建一个只读视图
  20.  
  21. --索引:可以加快查询速度,但会降低增删改速度
  22. --单列索引,建立在一列上的索引
  23. create index eid on emp(ename); --eid:索引名, emp:表名, ename:列名
  24. --复合索引:建立在多个列上
  25. create index pid_pname on person(pid,pname);
  26. --索引的使用规则
  27. /*
  28. 在大表上建立索引才有意义
  29. 在where子句后面或者是连接条件上的字段上建立索引
  30. 表中数据频繁增删改,不建议进行添加索引
  31. */
  32.  
  33. --plsql基本语法
  34. --为职工涨工资,每人涨 10%的工资。
  35. update emp set sal=sal*1.1;
  36. --按职工的职称长工资,总裁涨 1000元,经理涨800 元,其他人员涨 400 元。无法用一条sql进行操作,借助plsql
  37. /*语法结构
  38. declare
  39. 说明部分 (变量说明,游标申明,例外说明 〕
  40. begin
  41. 语句序列 (DML 语句〕…
  42. exception
  43. 例外处理语句
  44. End;
  45. */
  46. --定义变量: 变量名 变量类型
  47. varl number(2);
  48. psal char(15);
  49.  
  50. --定义常量
  51. married constant boolean := true; -- := 表示赋值
  52. --引用型变量
  53. myname emp.ename%type; --引用emp表中ename的类型
  54. --使用into进行赋值
  55. declare
  56.  
  57. emp_name emp.ename%type; --定义一个变量emp_name 类型和emp表中的ename数据类型是一样的
  58. begin
  59. select e.ename into emp_name from emp e where e.empno = 7369; --查询emp表中empno=7369ename,将值赋值给
  60. dbms_output.put_line(emp_name); --打印显示变量中的值
  61. end;
  62.  
  63. --记录型变量
  64. emp_mytype emp%rowtype; --可以记录emp表的一行类型
  65. --记录变量分量的引用
  66. emp_rec.ename:='ADAMS';
  67. --例子
  68. declare
  69. p emp%rowtype;
  70. begin
  71. select * into p from emp e where e.empno = 7369;
  72. dbms_output.put_line(p.ename || '的工资为' || p.sal); -- || 表示连接符号
  73. end;
  74.  
  75. --if分支
  76. --语法1:如果从控制台输入 1 则输出我是 1
  77. declare
  78. pnum number := #
  79. begin
  80. if pnum = 1 then
  81. dbms_output.put_line('我是' || pnum);
  82. end if;
  83. end;
  84. --语法2:如果从控制台输入 1 则输出我是 1否则输出我不是 1
  85. declare
  86. pnum number := #
  87. begin
  88. if pnum = 1 then
  89. dbms_output.put_line('我是' || pnum);
  90. else
  91. dbms_output.put_line('我不是1');
  92. end if;
  93. end;
  94. --语法3:判断人的不同年龄段 18岁以下是未成年人,18岁以上 40以下是成年人,40以上是老年人
  95. declare
  96. pnum number := #
  97. begin
  98. if pnum < 18 then
  99. dbms_output.put_line('未成年');
  100. elsif pnum >= 18 and pnum < 40 then
  101. dbms_output.put_line('成年人');
  102. elsif pnum >= 40 then
  103. dbms_output.put_line('老年人');
  104. end if;
  105. end;
  106. --LOOP循环分支
  107. --使用语法 1 输出 1 10 的数字
  108. declare
  109. step number :=1; --变量,赋予初始值1
  110. begin
  111. while step <=10 loop
  112. dbms_output.put_line(step);
  113. step := step+1; --没循环一次,+1
  114. end loop;
  115. end;
  116. --使用语法 2 输出 1 10 的数字
  117. declare
  118. step number := 1;
  119. begin
  120. loop
  121. exit when step > 10;
  122. dbms_output.put_line(step);
  123. step := step+1;
  124. end loop;
  125. end;
  126. --使用语法 3 输出 1 10 的数字
  127. declare
  128. step number := 1;
  129. begin
  130. for step in 1..10
  131. loop
  132. dbms_output.put_line(step);
  133. end loop;
  134. end;
  135.  
  136. --游标cursor
  137. --语法:CURSOR 游标名 [ (参数名 数据类型,参数名 数据类型,...)] IS SELECT 语句;
  138. cursor c1 is select ename from emp;
  139. --游标的使用步骤
  140. /*
  141. 1.打开游标: open c1 打开游标执行查询
  142. 2.取一行游标的值 fetch c1 into pjob; 取一行到变量中
  143. 3.关闭游标: close c1; 关闭游标,释放资源
  144. 4.游标的结束方式exit when c1%notfound
  145. */
  146. --使用游标方式输出 emp 表中的员工编号和姓名
  147. declare
  148. cursor pc is select * from emp; --创建一个游标
  149. pemp emp%rowtype; --定义一个记录型变量
  150. begin
  151. open pc; --打开游标
  152. loop --循环遍历
  153. fetch pc into pemp; --提取一行数据,存储到变量中
  154. exit when pc%notfound; --退出条件:没有下一行之时
  155. dbms_output.put_line(pemp.empno || '---' || pemp.ename); --打印
  156. end loop; --循环结束
  157. close pc; --关闭资源
  158. end;
  159.  
  160. --按员工的工种涨工资,总裁 1000 元,经理涨 800 元其,他人员涨 400 元。
  161. declare
  162. cursor p is select * from emp;
  163. addsal emp.sal%type;
  164. pemp emp%rowtype;
  165. begin
  166. open p;
  167. loop
  168. fetch p into pemp;
  169. exit when p%notfound;
  170. if
  171. pemp.job = 'PRESIDENT' then addsal := 1000;
  172. elsif pemp.job = 'MANAGER' then addsal := 800;
  173. else addsal := 400;
  174. end if;
  175. end loop;
  176. update emp e set e.sal = e.sal + addsal where e.empno = pemp.empno;
  177. commit;
  178. close p;
  179. end;
  180. --写一段PL/SQL 程序,为10号部门员工涨工资1000元。
  181. declare
  182. cursor p is select * from emp;
  183. addsal emp.sal%type;
  184. pemp emp%rowtype;
  185. begin
  186. open p;
  187. loop
  188. fetch p into pemp;
  189. exit when p%notfound;
  190. if
  191. pemp.deptno = 10 then addsal := 1000;
  192. else addsal := 0;
  193. end if;
  194.  
  195. end loop;
  196. update emp e set e.sal = e.sal + addsal where e.deptno = pemp.deptno;
  197. commit;
  198. close p;
  199. end;
  200.  
  201. --存储过程
  202. /*
  203. 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的 SQL 语句集,
  204. 经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。
  205. 存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
  206. */
  207. /*
  208. 创建存储过程语法:
  209.  
  210. create [or replace] PROCEDURE 过程名[( 参数名 in/out 数据类型)] AS
  211. begin
  212. PLSQL 子程序体;
  213. End;
  214. --或者
  215. create [or replace] PROCEDURE 过程名[( 参数名 in/out 数据类型)] is
  216. begin
  217. PLSQL 子程序体;
  218. End;
  219. */
  220. --创建一个helloword存储过程
  221. create or replace procedure helloworld as
  222. begin
  223. dbms_output.put_line('helloworld');
  224. end;
  225. --在plsql中调用存储过程
  226. begin
  227. helloworld;
  228. end;
  229. --删除存储过程
  230. drop procedure helloword;
  231.  
  232. --给指定的员工涨 指定 工资,并打印出涨前和涨后的工资
  233. create or replace procedure addsal(eno in number, addsal in number) is
  234. pemp emp%rowtype;
  235. begin
  236. select * into pemp from emp e where e.empno = eno;
  237. update emp set sal = nvl(sal,0) + addsal where empno = eno;
  238. dbms_output.put_line('涨工资前:' || pemp.sal || '---' || '涨工资后:' || (pemp.sal+addsal));
  239. end;
  240.  
  241. --调用存储过程
  242. begin
  243. addsal(eno => 7369,addsal => 1000);
  244. commit;
  245. end;
  246.  
  247. --存储函数
  248. /*
  249. create or replace function 函数名(Name in type, Name in type, ...) return 数据类型
  250. is
  251. 结果变量 数据类型;
  252. begin
  253. return( 结果变量);
  254. end;
  255. */
  256. --使用存储函数来查询指定员工的年薪
  257. create or replace function findsal(eno in emp.empno%type) return number is
  258. pemp emp%rowtype;
  259. begin
  260. select * into pemp from emp e where e.empno = eno;
  261. return pemp.sal * 12 + nvl(pemp.comm,0);
  262. end;
  263. --调用存储函数
  264. declare
  265. varl number;
  266. begin
  267. varl := findsal(7369);
  268. dbms_output.put_line(varl);
  269. end;
  270. /*
  271. 存储函数和存储过程的区别
    图1-1
  272. */
  273. --触发器
  274. /*
  275. 数据库触发器是一个与表相关联的、存储的 PL/SQL 程序。
  276. 每当一个特定的数据操作语句(Insert,update,delete)在指定的表上发出时,
  277. Oracle 自动地执行触发器中定义的语句序列。
  278. */
  279. --触发器可用于:
  280. /*
  281. 1. 数据确认
  282. 2. 实施复杂的安全性检查
  283. 3. 做审计,跟踪表上所做的数据操作等
  284. 4. 数据的备份和同步
  285. */
  286. --类型
  287. /*
  288. - 语句级触发器 :在指定的操作语句操作之前或之后执行一次,不管这条语句影响了多少行 。
  289. - 行级触发器:触发语句作用的每一条记录都被触发。在行级触 发器中使用 old 和 new伪记录变量, 识别值的状态。
  290. */
  291. --语法
  292. /*
  293. CREATE [or replace] TRIGGER 触发器名
  294. {BEFORE | AFTER}
  295. {DELETE | INSERT | UPDATE [OF 列名]}
  296. ON 表名
  297. [FOR EACH ROW [WHEN( 条件) ] ]
  298. begin
  299. PLSQL块
  300. End;
  301. */
  302. --插入员工后打印一句话“一个新员工插入成功”
  303. create or replace trigger testtrigger
  304. after
  305. insert
  306. on person
  307. declare
  308. begin
  309. dbms_output.put_line('一个员工插入成功');
  310. end;
  311. --在person表中插入数据
  312. insert into person values(12,'songwenhui');
  313. select * from person;
  314.  
  315. --在行级触发器中触发语句与伪记录变量的值
    --图1-2

  1. --判断员工涨工资之后的工资的值一定要大于涨工资之前的工资
  2. create or replace trigger addsal
  3. before
  4. update of sal on emp
  5. for each row
  6. begin
  7. if :old.sal >= :new.sal
  8. then raise_application_error(-20002, '涨前的工资不能大于涨后的工资');
  9. end if;
  10. end;
  11. --调用
  12. update emp t set t.sal = t.sal - 1; --报错

Oracle_视图_索引_plsql_游标_存储过程_存储函数_触发器的更多相关文章

  1. Oracle03——游标、异常、存储过程、存储函数、触发器和Java代码访问Oracle对象

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7476717.html 1.游标(光标)Cursor 在写java程序中有集合的概念,那么 ...

  2. Oracle-4 - :超级适合初学者的入门级笔记:plsql,基本语法,记录类型,循环,游标,异常处理,存储过程,存储函数,触发器

    初学者可以从查询到现在的pl/sql的内容都可以在我这里的笔记中找到,希望能帮到大家,视频资源在 资源, 我自己的全套笔记在  笔记 在pl/sql中可以继续使用的sql关键字有:update del ...

  3. MySQL学习笔记九:存储过程,存储函数,触发器

    存储过程 1.存储过程由一组特定功能的SQL语句组成,对于大型应用程序优势较大,相对不使用存储过程,具有以下优点: a.性能提高,因为存储过程是预编译的,只需编译一次,以后调用就不须再编译 b.重用性 ...

  4. Oracle 存储过程以及存储函数

    以下的一些例子是基于scott用户下的emp表的数据,一和二使用的均为in,out参数,最后一个综合练习使用了 in out参数 一.存储过程 1.创建无参的存储过程示例  ------ hello ...

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

    1 引言 存储过程和存储函数类似于面向对象程序设计语言中的方法,可以简化代码,提高代码的重用性.本文主要介绍如何创建存储过程和存储函数,以及存储过程与函数的使用.修改.删除等操作. 2 存储过程与存储 ...

  6. 编程开发之--Oracle数据库--存储过程和存储函数(2)

    上一小结我们简单介绍了存储过程和存储函数,对存储过程和存储函数有了一个基本的了解,接下来介绍在java程序中如何调用我们创建的存储过程和存储函数 1.在应用程序中调用我们的存储过程 创建一个简单的Ja ...

  7. oracle存储过程和存储函数

    存储过程 1.存储过程简介 下面先来简单介绍一下oracle的存储过程的语法,如下: create or replace procedure Tony_Process ( num in number, ...

  8. MySQL-快速入门(8)存储过程、存储函数

    1.存储过程 1>创建存储过程:create procedure create procedure sp_name ([in | out | inout] param_name type) [c ...

  9. mysql存储过程和存储函数

    mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...

随机推荐

  1. JVM性能调优详解

    前面我们学习了整个JVM系列,最终目标的不仅仅是了解JVM的基础知识,也是为了进行JVM性能调优做准备.这篇文章带领大家学习JVM性能调优的知识. 性能调优 性能调优包含多个层次,比如:架构调优.代码 ...

  2. ASCALL码对照表

    ASCALL码对照表 目前计算机中用得最广泛的字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Standard Code for Information Inter ...

  3. SpringBoot 源码解析 (一)----- SpringBoot核心原理入门

    Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for building all Sp ...

  4. html5基本页面

    html5基本页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  5. 你真的会写单测吗?TDD初体验

    前言: 昨天读到了一篇文章,讲的是TDD,即Test-Driven Development,测试驱动开发.大体意思是,它要求在编写某个功能的代码之前先编写测试代码,然后只编写使测试通过的功能代码,通过 ...

  6. java多线程与线程并发三:线程同步通信

    本文章内容整理自:张孝祥_Java多线程与并发库高级应用视频教程. 有些时候,线程间需要传递消息,比如下面这道面试题: 子线程循环10次,然后主线程循环100次,然后又回到子线程循环50次,然后再回到 ...

  7. leetcode算法笔记:二叉树,动态规划和回溯法

    在二叉树中增加一行 题目描述 给定一个二叉树,根节点为第1层,深度为 1.在其第 d 层追加一行值为 v 的节点. 添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N, ...

  8. [Verilog] 从系统时钟转换出想要的时钟

    如何50MHZ时钟转换出一个250KHZ的时钟出来? 假如系统时钟是50MHZ,然后想得到250KHZ的新时钟,那么50MHZ / 250KHZ = 200倍,然后令k=200,程序如下: ; :] ...

  9. nuxt遇到的问题(一)window 或 document is not defined

    因为用了VUE做的官网,既然是官网了避免不了SEO的问题了(该死当初就不应该选择用vue) 很自然就是选择了使用nuxt.js来做ssr预渲染了. 因为网站不是响应式的,PC / 移动端要进行对应跳转 ...

  10. SparkSQL--数据源Parquet的加载和保存

    一.通用的load和save操作 对于Spark SQL的DataFrame来说,无论是从什么数据源创建出来的DataFrame,都有一些共同的load和save操作.load操作主要用于加载数据,创 ...