Oracle | PL/SQL 学习 | Oracle Office Document | #1

PL/SQL, the Oracle procedural extension of SQL, is a portable, high-performance transaction-processing language

PL / SQL对SQL的一种补充,是一种可移植的高性能交易处理语言。

优点:

  1. 轻量集成
  2. 高性能
  3. 便捷
  4. 可拓展性
  5. 可管理
  6. 面向对象编程

特征:

  1. PL/SQL combines the data-manipulating power of SQL with the processing power of procedural languages.
  2. When you can solve a problem with SQL, you can issue SQL statements from your PL/SQL program, without learning new APIs
  3. PL/SQL lets you declare constants and variables, control program flow, define subprograms, and trap runtime errors.
  4. You can break complex problems into easily understandable subprograms, which you can reuse in multiple applications.

解析:

  • 在SQL(数据处理)基础上增加了过程处理的能力,这是迈向高级语言编程的重要指标。
  • 强调兼容性,可以在PL/SQL中毫不客气地使用SQL语句。
  • 强调分治思想,可以将复杂的过程拆分成多个子过程,并让子过程有效的沟通。

基于上述理念结合高级语言的一些流程控制,引申出一些语法

基本特性:

  • Error Handling:错误处理,使用Exception去捕获异常
  • Block:编程单元(块)
  • Variables and Constants:变量和常量
  • Subprograms:子程序
  • Package:执行单元,可以理解成一个应用程序
  • Triggers:触发器,响应在数据库中发生的事件
  • Input and Output:取决于执行逻辑
  • Data Abstraction:对数据进行抽象,可以理解成数据结构
  • Cursors:指向私有的SQL域,例如可以查询这个语句有多少行收到影响
  • Composite Variables:可以理解成列表和数组,指具有内部组件的变量
  • Using the %ROWTYPE and %TYPE Attribute
    • %TYPE:为了达到和参照变量一致的数据类型,同时不用修改PL/SQL代码的需求
    • %ROWTYPE:让你声明一条代表数据库中表或视图全部或部分行的记录

执行引擎流程:

  • 分别给SQL语句执行器和程序语句执行器来解析执行