Lecture 08. Spring 2018 Borough of Manhattan Community College

Similar documents
ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

UNIT-IV (Relational Database Language, PL/SQL)

5. Single-row function

Lecture 07. Spring 2018 Borough of Manhattan Community College

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL

Now, we can refer to a sequence without having to use any SELECT command as follows:

ORACLE: PL/SQL Programming

Procedural Language Structured Query Language (PL/SQL)

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL Ed 2

Contents I Introduction 1 Introduction to PL/SQL iii

Oracle Database 11g: Program with PL/SQL Release 2

Oracle Database 12c R2: Program with PL/SQL Ed 2 Duration: 5 Days

Conditionally control code flow (loops, control structures). Create stored procedures and functions.

COSC344 Database Theory and Applications. Lecture 11 Triggers

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

IZ0-144Oracle 11g PL/SQL Certification (OCA) training

Persistent Stored Modules (Stored Procedures) : PSM

Question Bank PL/SQL Fundamentals-I

Oracle PLSQL Training Syllabus

INDEX. 1 Introduction. 2 Types of Cursors 2.1 Explicit cursor 2.2 Attributes 2.3 Implicit cursor 2.4 Attributes. 3 Parameterized cursor

Oracle PLSQL. Course Summary. Duration. Objectives

Oracle - Oracle Database: Program with PL/SQL Ed 2

RDBMS - PL SQL - Topic 5 - MSBTE QUESTIONS AND ANSWERS

SQL Interview Questions

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

Sql Server Syllabus. Overview

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

ORACLE DATABASE 12C INTRODUCTION

Introduction p. 1 The Logical and Physical View of Tables p. 1 Database Types p. 4 NULLs p. 6 DDL and DML Statements p. 7 Column and Table Constraint

PL/SQL Block structure

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Oracle Database: Introduction to SQL/PLSQL Accelerated

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

1Z Z0-146-Oracle Database 11g: Advanced PL/SQL Exam Summary Syllabus Questions

DB2 UDB: App Programming - Advanced

Topics Fundamentals of PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Chapter 1 SQL and Data

Oracle Developer Track Course Contents. Mr. Sandeep M Shinde. Oracle Application Techno-Functional Consultant

Introduction to Computer Science and Business

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

Course 492 Supplementary Materials. Mutating Tables

Question No : 1 Which statement is true about triggers on data definition language (DDL) statements?

Introduction to SQL/PLSQL Accelerated Ed 2

Oracle Database 11g: Program with PL/SQL

Oracle Database: Program with PL/SQL

Oracle Database 11g: SQL and PL/SQL Fundamentals

Database Programming with PL/SQL

5 Integrity Constraints and Triggers

Lesson B Objectives IF/THEN. Chapter 4B: More Advanced PL/SQL Programming

Slides by: Ms. Shree Jaswal

Oracle Database 11g & PL/SQL

Oracle Class VI. Exception Block Cursors For Loops

DATABASE DESIGN - 1DL400

Vendor: Oracle. Exam Code: 1Z Exam Name: Oracle Database 11g: Program with PL/ SQL. Version: Demo

Question: Which statement would you use to invoke a stored procedure in isql*plus?

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL Server. Lecture3 Cascading referential integrity constraint

2. Programming written ( main theme is to test our data structure knowledge, proficiency

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

ORACLE Job Placement Paper. Paper Type : General - other

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

1Z Oracle Database 11g - Program with PL/SQL Exam Summary Syllabus Questions

Databases 1. SQL/PSM and Oracle PL/SQL

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

Oracle Database 11g & PL/SQL

Oracle Exam 1z0-144 Oracle Database 11g: Program with PL/SQL Version: 8.5 [ Total Questions: 103 ]

Oracle 1Z Oracle Database 11g: Advanced PL/SQL.

M.SC(IT) I YEAR( ) CORE: ADVANCED DBMS-163B Semester : I Multiple Choice Questions

Oracle PL/SQL - 12c & 11g [Basic PL/SQL & Advanced PL/SQL]

Meet MariaDB Vicențiu Ciorbaru Software MariaDB Foundation * * 2017 MariaDB Foundation

Index. Boolean expression, , Business rules enforcement. see Declarative constraints table with Oracle constraints and,

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

Fundamentals, Design, and Implementation, 9/e Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M.

Programming the Database

JPexam. 最新の IT 認定試験資料のプロバイダ IT 認証であなたのキャリアを進めます

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

OVERVIEW OF THE TYPES OF PL/SQL BLOCKS:

PLSQL Interview Questions :

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

3ISY402 DATABASE SYSTEMS

2 PL/SQL - fundamentals Variables and Constants Operators SQL in PL/SQL Control structures... 7

DB Creation with SQL DDL

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Oracle EXAM 1Z0-144 Oracle Database 11g: Program with PL/SQL

Review -Chapter 4. Review -Chapter 5

PL/SQL is a combination of SQL along with the procedural features of programming languages.

Assorted Topics Stored Procedures and Triggers Pg 1

Example 1 - Create Horizontal View. Example 2 - Create Vertical View. Views. Views

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Part 18. Developer 2000

SEF DATABASE FOUNDATION ON ORACLE COURSE CURRICULUM

The PL/SQL Engine: PL/SQL. A PL/SQL Block: Declaration Section. Execution Section. Declaration Section 3/24/2014

Outline. CS 235: Introduction to Databases. DB Application Programming. Interface Solutions. Basic PSM Form. Persistent Stored Modules

ORACLE TRAINING. ORACLE Training Course syllabus ORACLE SQL ORACLE PLSQL. Oracle SQL Training Syllabus

Lab # 4. Data Definition Language (DDL)


Transcription:

Lecture 08 Spring 2018 Borough of Manhattan Community College 1

The SQL Programming Language Recent versions of the SQL standard allow SQL to be embedded in high-level programming languages to help develop more complex database applications. To make the discussions more concrete, in this lecture, we base the presentation mainly on the Oracle programming language, PL/SQL (Procedural Language/SQL). PL/SQL has concepts similar to modern programming languages, such as variable and constant declarations, control structures and exception handling. PL/SQL is a block-structured language: blocks can be entirely separate or nested within one another. 2

The SQL Programming Language A PL/SQL block has up to three parts: an optional declaration part, in which variables, constants, and cursors are defined and possibly initialized; a mandatory executable part, in which the variables are manipulated; an optional exception part, to handle any exceptions raised during execution. 3

Declarations Variables and constant variables must be declared before they can be referenced in other statements. Examples of declarations are: vstaffno VARCHAR2(5); MAX_PROPERTIES CONSTANT NUMBER := 100; Note that it is possible to declare a variable as NOT NULL, although in this case an initial value must be assigned to the variable. vrent NUMBER(6, 2) NOT NULL := 600; 4

Declarations It is also possible to declare a variable to be of the same type as a column in a specified table or another variable using the %TYPE attribute. For example, to declare that the vstaffno variable is the same type as the staffno column of the Staff table, we could write: vstaffno vstaffno2 Staff.staffNo%TYPE; vstaffno%type; 5

Assignments In the executable part of a PL/SQL block, variables can be assigned in two ways: 1. using the normal assignment statement (:=), 2. or as the result of an SQL SELECT or FETCH statement. Examples: vstaffno := 'SG14'; vrent := 500; SELECT COUNT(*) INTO x FROM PropertyForRent WHERE staffno = vstaffno; the variable x is set to the result of the SELECT statement (the number of properties managed by staff member SG14). Note that in the SQL standard, an assignment uses the SET keyword: SET vstaffno = 'SG14' 6

Control Statements PL/SQL supports the usual conditional, iterative, and sequential flowof-control mechanisms. Conditional IF statement: The IF statement has the following form. 7

Control Statements Conditional CASE statement: The CASE statement allows the selection of an execution path based on a set of alternatives. 8

Control Statements Iteration statement (LOOP): The LOOP statement has the following form. LOOP <SQL statement list> EXIT WHEN (condition) END LOOP; LOOP <SQL statement list> IF (condition) THEN EXIT; END LOOP; 9

Control Statements Iteration statement (WHILE): The WHILE statement has the following form. WHILE (condition) LOOP END LOOP; <SQL statement list> 10

Control Statements Iteration statement (FOR): The FOR statement has the following form. FOR indexvariable IN lowerbound.. upperbound LOOP <SQL statement list> END LOOP; 11

Control Statements 12

Cursors in PL/SQL To handle a query that can return an arbitrary number of rows PL/SQL uses cursors to allow the rows of a query result to be accessed one at a time. In effect, the cursor acts as a pointer to a particular row of the query result. A cursor must be declared and opened before it can be used, and it must be closed to deactivate it after it is no longer required. Once the cursor has been opened, the rows of the query result can be retrieved one at a time using a FETCH statement, as opposed to a SELECT statement. 13

14

Cursors in PL/SQL When the cursor is first opened, this has the effect of parsing the SELECT statement specified in the CURSOR declaration, identifying the rows that satisfy the search criteria (called the active set), and positioning the pointer just before the first row in the active set. The code then loops over each row in the active set and retrieves the current row values into output variables using the FETCH INTO statement. Each FETCH statement advances the pointer to the next row of the active set. Some useful cursor attributes are: %NOTFOUND: Evaluates to true if the most recent fetch does not return a row. %FOUND: Evaluates to true if the most recent fetch returns a row. %ISOPEN: Evaluates to true if the cursor is open. %ROWCOUNT: Evaluates to the total number of rows returned so far. 15

Cursors in PL/SQL 16

Passing Parameters to Cursors PL/SQL allows cursors to be parameterized, so that the same cursor definition can be reused with different criteria. For example, we could change the following cursor defined previously: 17

Updating Rows Through a Cursor It is possible to update and delete a row after it has been fetched through a cursor. In this case, to ensure that rows are not changed between declaring the cursor, opening it, and fetching the rows in the active set, the FOR UPDATE clause is added to the cursor declaration. This has the effect of locking the rows of the active set to prevent any update conflict when the cursor is opened. For example, we may want to reassign the properties that SG14 manages to SG37. 18

Stored Procedures and Functions Subprograms are named PL/SQL blocks that can take parameters and be invoked. PL/SQL has two types of subprogram called stored procedures and functions. Procedures and functions can take a set of parameters given to them by the calling program and perform a set of actions. Both can modify and return data passed to them as a parameter. The difference between a procedure and a function is that a function will always return a single value to the caller, whereas a procedure does not. 19

Stored Procedures and Functions A parameter has a specified name and data type but can also be designated as: IN parameter is used as an input value only. OUT parameter is used as an output value only. IN OUT parameter is used as both an input and an output value. Example of a procedure: CREATE OR REPLACE PROCEDURE PropertiesForStaff (IN vstaffno VARCHAR2) AS... The procedure could then be executed as: EXECUTE PropertiesForStaff ('SG14'); 20

Stored Procedures and Functions 21

Stored Procedures and Functions 22

Stored Procedures and Functions 23

Stored Procedures and Functions 24

Stored Procedures and Functions 25

Triggers A trigger defines an action that the database should take when some event occurs in the application. The general format of a trigger in SQL is: Triggers can be dropped using DROP TRIGGER <TriggerName> statement. 26

Triggers Triggers are based on the Event Condition Action (ECA) model: The event that triggers the rule, which can be an INSERT, UPDATE, or DELETE statement on a specified table (or possibly view). It is also possible to specify whether the trigger should fire before the event or after the event. The condition that determines whether the action should be executed. The condition is optional but, if specified, the action will be executed only if the condition is true. The action to be taken. This block contains the SQL statements and code to be executed when a triggering statement is issued and the trigger condition evaluates to true. 27

Triggers There are two types of trigger: row-level triggers (FOR EACH ROW) that execute for each row of the table that is affected by the triggering event. statement-level triggers (FOR EACH STATEMENT) that execute only once even if multiple rows are affected by the triggering event. SQL also supports INSTEAD OF triggers: Provide a transparent way of modifying views that cannot be modified directly through SQL DML statements. They are called INSTEAD OF triggers because the trigger is fired instead of executing the original SQL statement. 28

Triggers 29

Triggers DreamHome has a rule that prevents a member of staff from managing more than 100 properties at the same time. 30

Triggers 31

Triggers 32

Using Triggers to Enforce Referential Integrity By default, Oracle enforces the referential actions ON DELETE NO ACTION and ON UPDATE NO ACTION on the named foreign keys. It also allows the additional clause ON DELETE CASCADE to be specified to allow deletions from the parent table to cascade to the child table. However, it does not support the ON UPDATE CASCADE action, or the SET DEFAULT and SET NULL actions. If any of these actions are required, they will have to be implemented as triggers or stored procedures, or within the application code. 33