INLS 623– Stored Procedures

Slides:



Advertisements
Similar presentations
ICS 434 Advanced Database Systems
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Distributed Information Systems - The Client server model
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Software Development Architectures Ankur K. Rajopadhye Louisiana Tech University.
Python quick start guide
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.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
INLS 623– S TORED P ROCEDURES Instructor: Jason Carter.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Jim Janson. Agenda Evolution of software architectures 3-tier User interfaces Application servers Databases.
CS 100 Introduction to Computing Seminar October 7, 2015.
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.
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,
1 LM 6 Database Applications Dr. Lei Li. Learning Objectives Explain three components of a client-server system Describe differences between a 2-tiered.
Programming Paradigms, Software Architectural Patterns, and MVC CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
Introduction to Dynamic Web Programming
Stored Procedures.
N-Tier Architecture.
Web Technologies IT230 Dr Mohamed Habib.
Database System Concepts and Architecture
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Instructor: Jason Carter
Topics Introduction to Repetition Structures
MVC and other n-tier Architectures
Views, Stored Procedures, Functions, and Triggers
The Client/Server Database Environment
CSC 480 Software Engineering
Chapter 5: Loops and Files.
Application Development Theory
PHP / MySQL Introduction
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
#01 Client/Server Computing
Web Browser server client 3-Tier Architecture Apache web server PHP
Outline Altering flow of control Boolean expressions
Introduction to Databases Transparencies
Lecture 1: Multi-tier Architecture Overview
Web Application Architectures
Introduction to Object-Oriented Programming with Java--Wu
Tiers vs. Layers.
PHP.
3 Control Statements:.
CMPT 354: Database System I
Web Application Architectures
Introduction to Repetition Structures
Information Management
Understand the interaction between computer hardware and software
COMPONENTS – WHY? Object-oriented source-level re-use of code requires same source code language. Object-oriented source-level re-use may require understanding.
JavaScript CS 4640 Programming Languages for Web Applications
Topics Introduction to Repetition Structures
Procedures Oracle & MySQL
PHP an introduction.
Web Application Architectures
Web Servers (IIS and Apache)
JavaScript 101 Lesson 8: Loops.
How to allow the program to know when to stop a loop.
Web Application Development Using PHP
JavaScript CS 4640 Programming Languages for Web Applications
#01 Client/Server Computing
Looping and Repetition
Presentation transcript:

INLS 623– Stored Procedures Functions Instructor: Jason Carter

Midterm March 9 Review March 2 Topics Covered: Normalization Advanced SQL Database and Internet Applications Stored Procedures/Triggers Indexing

Database and Internet Applications Server Client HTTP Request Response GET Redirect POST HTML Java JavaScript PHP SQL ASP.NET CSS AJAX Python Cookies

Three Tiered Architectures Client Program (Web Browser) Presentation tier Application Server Middle tier Database System Data management tier

The Three Layers Presentation tier Data management tier Middle 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

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

Advantages of the Three-tier Architecture 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

Disadvantages of 3-Tier Architecture 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

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

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

Why Stored Procedures 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

Disadvantages of Stored Procedures 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

Creating Stored Procedures DELIMITER // CREATE PROCEDURE NAME BEGIN SQL STATEMENT END // DELIMITER ; DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ;

Stored Procedure in Workbench

Stored Procedure in Workbench

Calling Stored Procedures CALL STORED_PROCEDURE_NAME CALL GetAllProducts();

Variables 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

Parameters 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

Three Types of Parameters IN Default OUT INOUT

In Parameter Calling program has to pass an argument to the stored procedure.

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

Three Types of Parameters IN Default OUT INOUT

Out Parameter 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

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

Three Types of Parameters IN Default OUT INOUT

Conditionals $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?

Conditionals

The “If” Statement 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

“IfExpression”: BOOLEAN Expressions and Operators

IF Statement 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 ;

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

The IF Statement IF p_operator = "<" THEN select * from products WHERE quantityInStock < p_quantityInStock; ELSEIF p_operator = ">" THEN select * from products WHERE quantityInStock > p_quantityInStock; END IF;

Loops Repeats a set of commands until some condition is met 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

The expression must evaluate to true or false While Loop 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

Infinite Loops 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

While Loop 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 ;

While Loop Creating Variables DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = '';

While Loop WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE;

Practice Look at homework