Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 2141 – Intro to Database Systems

Similar presentations


Presentation on theme: "CSCI 2141 – Intro to Database Systems"— Presentation transcript:

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

2 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?

3 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

4 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

5 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/ 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

6 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)

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

8 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");

9 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

10 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?

11 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");


Download ppt "CSCI 2141 – Intro to Database Systems"

Similar presentations


Ads by Google