CSCI 2141 – Intro to Database Systems

Slides:



Advertisements
Similar presentations
Transact-SQL. 1. Declare float = 10 select * from customers where discnt
Advertisements

Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
Python MySQL Database Access
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Programming using C# Joins SQL Injection Stored Procedures
CSE 3330 Database Concepts Stored Procedures. How to create a user CREATE USER.. GRANT PRIVILEGE.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
PL/SQL Chapter 8.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
Create Stored Procedures and Functions Database Management Fundamentals LESSON 2.4.
1. Advanced SQL Functions Procedural Constructs Triggers.
INLS 623– Stored Procedures
Task oriented processing
Stored Procedures.
© 2016, Mike Murach & Associates, Inc.
Instructor: Jason Carter
Difference between Oracle PL/SQL and MySQL
Database Systems: Design, Implementation, and Management Tenth Edition
Views, Stored Procedures, Functions, and Triggers
UNIT - V STORED PROCEDURE.
Prepared Statements Function and Triggers
PL/SQL Scripting in Oracle:
Oracle Stored Procedures and Functions
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
CSCI 2141 – Intro to Database Systems
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Processing: David M. Kroenke’s Chapter Seven:
CS122B: Projects in Databases and Web Applications Winter 2018
Chapter 7 Using SQL in Applications
Information Management
CS122B: Projects in Databases and Web Applications Spring 2018
Database Systems: Design, Implementation, and Management Tenth Edition
Oracle Stored Procedures and Functions
SQL Transactions 4/23/2019 See scm-intranet.
Procedures Oracle & MySQL
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

CSCI 2141 – Intro to Database Systems Advanced SQL – Stored Procedures

Stored Procedures (SP) Named collection of procedural and SQL statements Stored in the database – just like triggers Can be used to encapsulate and represent business transactions E.g. creating a SP to represent a product sale, or addition of a new customer In scenarios where you need to update many tables simultaneously, stored procedures may help?

Advantages of Stored Procedures Advantages of SP Reduced network traffic No transmission of individual SQL statements over the network Increased performance All transactions are executed locally on the server Encapsulation and security Column names of the table are hidden, and execution privileges to users can be granted Change in table structure will not affect code Only the number of variables passed may change

Stored Procedure – Syntax CREATE [OR REPLACE] PROCEDURE procedure_name [(argument [IN/OUT] data-type, … )] [IS/AS] [variable_namedata type[:=initial_value]] BEGIN SQL (or procedural) statements; … END Arguments – specify the parameters passed to the SP IN/OUT – whether parameter is for input, output or both Variables can be declared between the IS and BEGIN keywords

SP – Business Case MySQL SalesCo wants to provide an additional 5% discount for all PRODUCTs when the quantity on hand (P_QOH) is more than or equal to twice the minimum quantity (P_MIN) MySQL delimiter // CREATE PROCEDURE PRC_PROD_DISCOUNT (IN disc DECIMAL) BEGIN UPDATE PRODUCT SET P_DISCOUNT = P_DISCOUNT + disc/100 WHERE P_QOH >= P_MIN*2; END;// CALL PRC_PROD_DISCOUNT(5); Advantage is that your manager doesn’t need to be an SQL expert to be able to provide a 10% discount. She can simply call the procedure with the correct amount Also, you can pass values to stored procedures. Hence one method of writing interfaces is to create SPs in database and your application can simply call those SPs with values

Business Case 2 – Add a New Customer MySQL For MySQL DELIMITER // CREATE PROCEDURE PRC_CUS_ADD (IN w_ln VARCHAR(20), IN w_fn VARCHAR(20), IN w_init VARCHAR(20), IN w_ac VARCHAR(20), IN w_ph VARCHAR(20)) BEGIN DECLARE c_code INT; SELECT MAX(CUS_CODE) INTO c_code FROM CUSTOMER; INSERT INTO CUSTOMER (CUS_CODE, CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE) VALUES (c_code+1, W_LN, w_fn, w_init, w_ac, w_ph); END;// Advantages: Actual column names are hidden (encapsulated) Only need to call procedure and enter values to insert a customer (expertise in SQL not required) Any changes in table structure do not need change in code (only addition or removal of values from the call)

Exercise Table: SPECIES Table: ANIMAL SP_ID DESCRIP 1 INSECT 2 BIRD 3 FISH 4 MAMMAL ID NAME SP_ID LIFE_EXP 1 Cat 4 20 2 Elephant 70 3 Trout 5 Shark 25 Canary 6 Albatross 40 7 Swift Write a procedure named PRC_SP_ANIM_ADD that accepts two parameters –description of a species and name of an animal – and inserts a row in the Category and Animal table with appropriate values.

Solution delimiter // CREATE PROCEDURE PRC_SP_ANIM_ADD (IN sp_desc varchar(45), IN an_desc varchar(45)) BEGIN DECLARE spid INT(11); DECLARE anid INT(11); SELECT MAX(sp_id) INTO spid FROM species; INSERT INTO species VALUES (spid, sp_desc); SELECT MAX(id) INTO anid FROM animal; INSERT INTO animal (id, name, sp_id) VALUES (anid, an_desc, spid); END;// call PRC_SP_ANIM_ADD("INSECT", "ANT");

Solution – Spot the Logical Error delimiter // CREATE PROCEDURE PRC_SP_ANIM_ADD (IN sp_desc varchar(45), IN an_desc varchar(45)) BEGIN DECLARE spid INT(11); DECLARE anid INT(11); SELECT MAX(sp_id) INTO spid FROM species; INSERT INTO species VALUES (spid, sp_desc); SELECT MAX(id) INTO anid FROM animal; INSERT INTO animal (id, name, sp_id) VALUES (anid, an_desc, spid); END;// call PRC_SP_ANIM_ADD("INSECT", "ANT"); Error PK already exists

Corrected Solution delimiter // CREATE PROCEDURE PRC_SP_ANIM_ADD (IN sp_desc varchar(45), IN an_desc varchar(45)) BEGIN DECLARE spid INT(11); DECLARE anid INT(11); SELECT MAX(sp_id) INTO spid FROM species; INSERT INTO species VALUES (spid+1, sp_desc); SELECT MAX(id) INTO anid FROM animal; INSERT INTO animal (id, name, sp_id) VALUES (anid+1, an_desc, spid+1); END;// call PRC_SP_ANIM_ADD("INSECT", "ANT"); What if the species already exists in the SPECIES table?

Corrected Solution 2 delimiter // CREATE PROCEDURE PRC_SP_ANIM_ADD (IN sp_desc varchar(45), IN an_desc varchar(45)) BEGIN DECLARE spid INT(11); DECLARE anid INT(11); IF EXISTS (SELECT sp_id FROM species WHERE descrip = sp_desc) THEN SELECT sp_id INTO spid FROM species WHERE descrip = sp_desc; ELSE SELECT MAX(sp_id) INTO spid FROM species; INSERT INTO species VALUES (spid+1, sp_desc); END IF; SELECT MAX(id) INTO anid FROM animal; INSERT INTO animal (id, name, sp_id) VALUES (anid+1, an_desc, spid+1); END;// call PRC_SP_ANIM_ADD("INSECT", "ANT");