INLS 623– S TORED P ROCEDURES Instructor: Jason Carter.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

ICS 434 Advanced Database Systems
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
12 Chapter 12 Client/Server Systems Hachim Haddouti.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Python quick start guide
Database Applications.  Database Programming  Web databases  Application architecture.
INLS 623– T HREE T IERED A RCHITECTURES Instructor: Jason Carter.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
chap13 Chapter 13 Programming in the Large.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Csi315csi315 Client/Server Models. Client/Server Environment LAN or WAN Server Data Berson, Fig 1.4, p.8 clients network.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
JavaScript, Fourth Edition
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
INLS 623 – D ATABASE A PPLICATION D EVELOPMENT AND I NTERNET A PPLICATIONS Instructor: Jason Carter.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Chapter Looping 5. The Increment and Decrement Operators 5.1.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 LM 6 Database Applications Dr. Lei Li. Learning Objectives Explain three components of a client-server system Describe differences between a 2-tiered.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
INLS 623– Stored Procedures
Chapter 4 Repetition Statements (loops)
Stored Procedures.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Topics Introduction to Repetition Structures
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
#01 Client/Server Computing
Outline Altering flow of control Boolean expressions
Introduction to Databases Transparencies
Lecture 1: Multi-tier Architecture Overview
Introduction to Object-Oriented Programming with Java--Wu
PHP.
3 Control Statements:.
Introduction to Repetition Structures
Information Management
Understand the interaction between computer hardware and software
Flow of Control.
How to allow the program to know when to stop a loop.
Web Application Development Using PHP
#01 Client/Server Computing
Looping and Repetition
Presentation transcript:

INLS 623– S TORED P ROCEDURES Instructor: Jason Carter

M IDTERM Oct 8

R EVIEW : I NTERNET HTML CSS JavaScript AJAX Cookies HTTP Request ResponseGET POST Redirect PHPSQL Java ASP.NET Python Client Server

T HREE T IERED A RCHITECTURES Presentation tier Client Program (Web Browser) Application Server Database System Middle tier Data management tier

T HE T HREE L AYERS Presentation tier Primary interface to the user Needs to adapt different displays (PC, cell, tablet, etc) Middle tier Implements business logic (implements complex actions, maintains state between different steps of workflow) Access different data management systems Data management tier One or more standard database management system

T ECHNOLOGIES HTML Javascript Client Program (Web Browser) Application Server (Apache) Application Server (Apache) Database System (MySQL) Database System (MySQL) PHP Cookies XML Stored Procedures Functions

A DVANTAGES OF THE T HREE - TIER A RCHITECTURE Heterogeneous Tiers can be independently maintained, modified, and replaced Scalability Data Management Tier can be scaled by database clustering without involving other tiers Middle Tier can be scaled by using load balancing Fault Tolerance Data Management Tier can be replicated without involving other tiers Software development Code is centralized Interaction between tiers through well-defined APIs: Can reuse standard components at each tier

D ISADVANTAGES OF 3-T IER A RCHITECTURE It is more complex It is more difficult to build a 3-tier application The physical separation of the tiers may affect the performance of all three If hardware and network bandwidth are not good enough because more networks, computers, and processes are involved

T ECHNOLOGIES HTML Javascript Client Program (Web Browser) Application Server (Apache) Application Server (Apache) Database System (MySQL) Database System (MySQL) PHP Cookies XML Stored Procedures Functions

S TORED P ROCEDURES Database program modules that are stored and executed by the DBMS at the server DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ;

W HY S TORED P ROCEDURES Reduces Duplication of effort and improves software modularity Multiple applications can use the stored procedure vs. the SQL statements being stored in the application language (PHP) Reduces communication and data transfer cost between client and server (in certain situations) Instead of sending multiple lengthy SQL statements, the application only has to send the name and parameters of the Stored Procedure Can be more secure than SQL statements Permission can be granted to certain stored procedures without granting access to database tables

D ISADVANTAGES OF S TORED P ROCEDURES Difficult to debug MySQL does not provide ways for debugging stored procedures Many stored procedures can increase memory use The more stored procedures you use, the more memory is used Can be difficult to maintain and develop stored procedures Another programming language to learn

C REATING S TORED P ROCEDURES DELIMITER // CREATE PROCEDURE NAME BEGIN SQL STATEMENT END // DELIMITER ; DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ;

S TORED P ROCEDURE IN W ORKBENCH

C ALLING S TORED P ROCEDURES CALL STORED_PROCEDURE_NAME CALL GetAllProducts();

V ARIABLES A variable is a name that refers to a value A name that represents a value stored in the computer memory PHP $name = “Jason” $age = 5; MySQL DECLARE name VARCHAR(255) DECLARE age INT

P ARAMETERS There may be times where you want to pass information to the stored procedures Getting user input from a form and using that input in a SQL statement

T HREE T YPES OF P ARAMETERS IN Default OUT INOUT

I N P ARAMETER Calling program has to pass an argument to the stored procedure.

A RGUMENTS AND P ARAMETERS DELIMITER // CREATE PROCEDURE GetOfficeByCountry( IN countryName VARCHAR(255) ) BEGIN SELECT * FROM offices WHERE country = countryName; END // DELIMITER ; Defining Calling CALL GetOfficeByCountry('USA') The values being copied from the calling stored procedure are calling arguments. The variables being copied into are called parameters. The values being copied from the calling stored procedure are calling arguments. The variables being copied into are called parameters.

T HREE T YPES OF P ARAMETERS IN Default OUT INOUT

O UT P ARAMETER OUT – the value of an OUT parameter can be changed inside the stored procedure and its new value is passed back to the calling program OUT is a keyword

O UT P ARAMETER DELIMITER // CREATE PROCEDURE CountOrderByStatus(IN orderStatus VARCHAR(25), OUT total INT) BEGIN SELECT count(orderNumber) INTO total FROM orders WHERE status = orderStatus; END// DELIMITER ; Defining Calling CALL The out parameter is used outside of the stored procedure.

T HREE T YPES OF P ARAMETERS IN Default OUT INOUT

C ONDITIONALS $sql = "select * from products WHERE quantityInStock < '".$quantityInStock."’; $sql = "select * from products WHERE quantityInStock > '".$quantityInStock."’; Could we have one call to the database instead of two?

C ONDITIONALS

T HE “I F ” S TATEMENT Mysql Syntax IF if_expression THEN commands [ELSEIF elseif_expression THEN commands] [ELSE commands] END IF; First line is known as the IF clause Includes the keyword IF followed by condition followed by the keyword THEN When the IFstatement executes, the condition is tested, and if it is true the block statements are executed. Otherwise, block statements are skipped

“I F E XPRESSION ”: BOOLEAN E XPRESSIONS AND O PERATORS

IF S TATEMENT DELIMITER // CREATE PROCEDURE GetProductsInStockBasedOnQuantitityLevel(IN p_operator VARCHAR(255), IN p_quantityInStock INT) BEGIN IF p_operator = "<" THEN select * from products WHERE quantityInStock < p_quantityInStock; ELSEIF p_operator = ">" THEN select * from products WHERE quantityInStock > p_quantityInStock; END IF; END // DELIMITER ;

IF S TATEMENT CREATE PROCEDURE GetProductsInStockBasedOnQuantitityLevel (IN p_operator VARCHAR(255), IN p_quantityInStock INT) The ooperator > or < The number in stock

T HE IF S TATEMENT IF p_operator = "<" THEN select * from products WHERE quantityInStock < p_quantityInStock; ELSEIF p_operator = ">" THEN select * from products WHERE quantityInStock > p_quantityInStock; END IF;

L OOPS While Repeat Loop Repeats a set of commands until some condition is met Iteration: one execution of the body of a loop If a condition is never met, we will have an infinite loop

W HILE L OOP WHILE expression DO Statements END WHILE The expression must evaluate to true or false while loop is known as a pretest loop Tests condition before performing an iteration Will never execute if condition is false to start with Requires performing some steps prior to the loop

I NFINITE L OOPS Loops must contain within themselves a way to terminate Something inside a while loop must eventually make the condition false Infinite loop: loop that does not have a way of stopping Repeats until program is interrupted Occurs when programmer forgets to include stopping code in the loop

W HILE L OOP DELIMITER // CREATE PROCEDURE WhileLoopProc() BEGIN DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = ''; WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE; SELECT str; END// DELIMITER ;

W HILE L OOP Creating Variables DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = '';

W HILE L OOP WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE;

P RACTICE Create a stored procedure that lists all orders Create a stored procedure that lists all customers Create a stored procedure that takes as input a parameter. The parameter is customer state. The procedure should list all customers from that state Create a stored procedure that counts the number of customers in the customers table and returns that number.