INLS 623 Stored Procedures Review and Advantages

undefined
INLS 623– S
TORED
P
ROCEDURES
Instructor: Jason Carter
M
IDTERM
March 9
Review March 2
Topics Covered:
Normalization
Advanced SQL
Database and Internet Applications
Stored Procedures/Triggers
Indexing
D
ATABASE
 
AND
 I
NTERNET
 A
PPLICATIONS
HTML
CSS
JavaScript
AJAX
Cookies
HTTP
Request
Response
GET
POST
Redirect
PHP
SQL
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)
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)
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
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
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
T
HREE
 T
YPES
 
OF
 P
ARAMETERS
IN
Default
OUT
INOUT
C
ONDITIONALS
 $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 
IF
statement 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
 
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
Look at homework
Slide Note

Functions

Embed
Share

In this information-rich content, explore topics covered in INLS 623 such as stored procedures, triggers, indexing, normalization, and advanced SQL. Discover the intricacies of database and internet applications, three-tiered architectures, and the advantages and disadvantages of three-tier architecture. Dive into technologies utilized, including HTML, JavaScript, PHP, cookies, Apache, MySQL, XML, and stored procedures/functions. Uncover the benefits of a three-tier architecture and the potential challenges it presents.

  • Stored procedures
  • Database applications
  • Three-tier architecture
  • Advantages
  • Disadvantages

Uploaded on Feb 19, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. INLS 623 STORED PROCEDURES Instructor: Jason Carter

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

  3. DATABASE AND INTERNET APPLICATIONS Client Server HTTPRequest Response GET POSTRedirect HTML Java JavaScript AJAX PHP SQL ASP.NET CSS Python Cookies

  4. THREE TIERED ARCHITECTURES Presentation tier Client Program (Web Browser) Middle tier Application Server Data management tier Database System

  5. THE THREE LAYERS 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

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

  7. ADVANTAGESOFTHE 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

  8. DISADVANTAGESOF 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

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

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

  11. 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

  12. DISADVANTAGESOF 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

  13. CREATING STORED PROCEDURES DELIMITER // CREATE PROCEDURE NAME BEGIN SQL STATEMENT END // DELIMITER ; DELIMITER // CREATE PROCEDURE GetAllProducts() BEGIN SELECT * FROM products; END // DELIMITER ;

  14. CALLING STORED PROCEDURES CALL STORED_PROCEDURE_NAME CALL GetAllProducts();

  15. 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

  16. 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

  17. THREE TYPESOF PARAMETERS IN Default OUT INOUT

  18. IN PARAMETER Calling program has to pass an argument to the stored procedure.

  19. ARGUMENTSAND 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.

  20. THREE TYPESOF PARAMETERS IN Default OUT INOUT

  21. 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

  22. OUT PARAMETER 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.

  23. THREE TYPESOF PARAMETERS IN Default OUT INOUT

  24. 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?

  25. CONDITIONALS

  26. THEIF 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

  27. IFEXPRESSION: BOOLEAN EXPRESSIONSAND OPERATORS

  28. 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 ;

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

  30. 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;

  31. LOOPS 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

  32. 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

  33. 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

  34. 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 ;

  35. WHILE LOOP Creating Variables DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = '';

  36. WHILE LOOP WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE;

  37. PRACTICE Look at homework

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#