Database Application Development

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Midterm Review Lecture 14b. 14 Lectures So Far 1.Introduction 2.The Relational Model 3.Disks and Files 4.Relational Algebra 5.File Org, Indexes 6.Relational.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Announcements Read JDBC Project Step 5, due Monday.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
ICS 321 Fall 2009 ODBC, JDBC & Stored Procedures Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/8/20091Lipyeow.
Advanced SQL: Cursors & Stored Procedures
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections –
1 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
1 Database Application Development Programming With Databases.
CS4222 Principles of Database System
CF 1334 Sistem Basis Data (3 SKS)
Diskusi-08 Jelaskan dan berikan contoh penggunaan theta join, equijoin, natural join, outer join, dan semijoin The slides for this text are organized into.
Diskusi-5 Sebutkan perangkat (tools) yang berpotensi mendukung kebutuhan tugas-tugas manajerial (management work) Jelaskan enam karakteristik informasi.
MODELS OF DATABASE AND DATABASE DESIGN
Tree-Structured Indexes
Storage and Indexes Chapter 8 & 9
Query-by-Example (QBE)
Database Application Development
Diskusi-1 Bacalah materi chapter-01, lalu buatlah ringkasan yang berisi tentang : Definisi EUIS Siapa end user Dampak euis pada lingkungan kerja Perencanaan.
Latihan Answer the following questions using the relational schema from the Exercises at the end of Chapter 3: Create the Hotel table using the integrity.
Diskusi-16 Buatlah ringkasan tentang pertimbangan dalam desain yang ergonomis pada tiga perangkat utama komputer yaitu monitor, keyboard dan mouse (lihat.
Database Application Development
Hash-Based Indexes Chapter 11
Latihan Create a separate table with the same structure as the Booking table to hold archive records. Using the INSERT statement, copy the records from.
Tugas-05 a. Sebutkan primary key masing-masing tabel
Introduction to Query Optimization
Relational Algebra Chapter 4, Part A
Modeling Your Data Chapter 2 cs542
File Organizations Chapter 8 “How index-learning turns no student pale
COSC 6340 Projects & Homeworks Spring 2002
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
External Sorting The slides for this text are organized into chapters. This lecture covers Chapter 11. Chapter 1: Introduction to Database Systems Chapter.
File Organizations and Indexing
File Organizations and Indexing
Team Project, Part II NOMO Auto, Part II IST 210 Section 4
Hash-Based Indexes Chapter 10
Selected Topics: External Sorting, Join Algorithms, …
SQL: The Query Language Part 1
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Topics Covered in COSC 6340 Data models (ER, Relational, XML)
B+Trees The slides for this text are organized into chapters. This lecture covers Chapter 9. Chapter 1: Introduction to Database Systems Chapter 2: The.
Database Management Systems CSE594
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
Evaluation of Relational Operations: Other Techniques
Chapter 8 Advanced SQL Pearson Education © 2009.
Distributed Databases
Introduction to Database Systems
Chapter 11 Instructor: Xin Zhang
Tree-Structured Indexes
File Organizations and Indexing
COSC 3480 Projects & Homeworks Fall 2003
Database Application Development
Relational Calculus Chapter 4, Part B
Presentation transcript:

Database Application Development Chapter 6 PSM (Stored Procedures) The slides for this text are organized into chapters. This lecture covers Chapter 6. I Foundations •Chapter 1: Introduction •Chapter 2: ER Model and Conceptual Design •Chapter 3: The Relational Model and SQL DDL •Chapter 4: Relational Algebra and Relational Calculus •Chapter 5: SQL II Applications •Chapter 6: Database Application Development •Chapter 7: Database-backed Internet Applications III Systems •Chapter 8: Overview of Storage and Indexing •Chapter 9: Data Storage •Chapter 10: Tree Indexes •Chapter 11: Hash Indexes IV Systems •Chapter 12: Overview of Query Evaluation •Chapter 13: External Sorting •Chapter 14: Evaluation of Relational Operators: First part (joins) and second part (other operators) •Chapter 15: A Typical Relational Optimizer V Systems •Chapter 16: Overview of Transaction Management •Chapter 17: Concurrency Control •Chapter 18: Recovery VI Applications •Chapter 19: Schema Refinement, Functional Dependencies, Normalization •Chapter 20: Physical Database Design, Database Tuning •Chapter 21: Security and Authorization VII Advanced Topics •Chapter 22: Parallel and Distributed Database Systems •Chapter 23: Data Warehousing and Decision Support •Chapter 24: Object-Database Systems •Chapter 25: Deductive Databases •Chapter 26: Data Mining •Chapter 27: Information Retrieval and XML Data Management •Chapter 28: Spatial Databases  

Stored Procedures What is a stored procedure: Advantages: SQL allows you to define procedures and functions and store in the DB server Program executed through single SQL statement Executed in process space by the database server Advantages: Can encapsulate application logic while staying “close” to the data (usually implies efficiency) Reuse of application logic by different users Avoid tuple-at-a-time return of records through cursors (as would be done by JDBC)

SQL/PSM Declare a stored procedure: CREATE PROCEDURE name(p1, p2, …, pn) local variable declarations procedure code for body; Declare a function: CREATE FUNCTION name (p1, …, pn) RETURNS sqlDataType local variable declarations function code;

Stored Procedures: Examples CREATE PROCEDURE ShowNumReservations SELECT S.sid, S.sname, COUNT(*) FROM Sailors S, Reserves R WHERE S.sid = R.sid GROUP BY S.sid, S.sname;

Stored Procedures: Examples Stored procedures can have parameters: <name> <mode> <type> <mode> is one of {IN, OUT, INOUT} eg: IN val1 int CREATE PROCEDURE IncreaseRating( IN sailor_sid INTEGER, IN increase INTEGER) UPDATE Sailors SET rating = rating + increase WHERE sid = sailor_sid;

Stored Procedures: Examples Stored procedure do not have to be in SQL: CREATE PROCEDURE TopSailors( IN num INTEGER) LANGUAGE JAVA EXTERNAL NAME “file:///c:/storedProcs/rank.jar”

Example: Procedure in PSM CREATE PROCEDURE testProcedure BEGIN INSERT INTO Student VALUES (5, ‘Joe’); END; Oracle’s version of PSM : CREATE PROCEDURE testProcedure IS . run; NOTE: mySQL later versions have initial support.

Also You can drop procedure by DROP PROCEDURE <procedureName> In PL/SQL, you can replace procedure by CREATE OR REPLACE PROCEDURE <procedureName> …

Calling Stored Procedures Calling Procedures call <procedureName> [(<paramList>)]; Embedded SQL: EXEC SQL BEGIN DECLARE SECTION Int sid; Int rating; EXEC SQL END DECLARE SECTION // now increase rating of this sailor EXEC CALL IncreaseRating(:sid,:rating);

Stored Procedures : Persistent Stored Modules (PSM)

SQL/PSM Most DBMSs allow users to write stored procedures in a simple general-purpose language (close to SQL) SQL/PSM standard is a representative of vendor-specific languages. Oracle supports a slightly different version of PSM called PL/SQL

Local Declarations CREATE PROCEDURE testProcedure (num IN int, name IN varchar) IS BEGIN INSERT INTO Student VALUES (num, name); END;

Local Declarations CREATE PROCEDURE testProcedure (num IN int, name IN varchar) IS num1 int; -- local variable BEGIN num1 := 10; INSERT INTO Student VALUES (num1, name); END;

Other PSM features Assignment statements: PL/SQL <varName> := <expression>

Control Structures: IF THEN ELSE IF <condition> THEN <statementList> ELSIF <condition> THEN ELSIF … ELSE <statementList> END IF;

Loops LOOP <statementList> END LOOP; To exit from a loop use

Loops: Example CREATE PROCEDURE testProcedure (num IN int, name IN varchar) IS num1 int; BEGIN num1 := 10; LOOP INSERT INTO Student VALUES (num1, name); num1 := num1 + 1; IF (num1 > 15) THEN EXIT; END IF; END LOOP; END;

FOR Loops FOR i in [REVERSE] <lowerBound> .. <upperBound> LOOP <statementList> END LOOP Example: FOR i in 1 .. 5 LOOP INSERT INTO Student (sNumber) values (10 + i); END LOOP;

WHILE LOOPS WHILE <condition> LOOP <statementList> END LOOP;

Functions CREATE FUNCTION <functionName> [(<paramList>)] RETURNS type AS <localDeclarations> BEGIN <functionBody>; END; You can call a function as part of an SQL expression Drop a function: drop function <functionName>

Functions: Example CREATE FUNCTION testFunction RETURN int AS num1 int; BEGIN SELECT MAX (sNumber) INTO num1 FROM Student; RETURN num1; END; SELECT * from Student where sNumber = testFunction ();

SQL/PSM Example CREATE FUNCTION rate Sailor (IN sailorId INTEGER) RETURNS INTEGER DECLARE rating INTEGER; DECLARE numRes INTEGER; BEGIN SET numRes = (SELECT COUNT(*) FROM Reserves R WHERE R.sid = sailorId); IF (numRes > 10) THEN rating =1; ELSE rating = 0; END IF; RETURN rating; END

Other Info Re Oracle : Oracle stores procedures and functions in catalog as relational tables: Check user_procedures Check user_functions You may run queries against them such as: describe user_procedures; select object_name from user_procedures;

Summary : Stored Procedures Stored procedures execute application logic directly at the server SQL/PSM standard for writing stored procedural logic: Used stand-alone on explicit call Used functions in WHERE clause of SQL statement Used in body of triggers