Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Advanced Topics.

Slides:



Advertisements
Similar presentations
® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 12: SQL PL Stored Procedures.
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Using Relational Databases and SQL
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Definition Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
C++ for Engineers and Scientists Third Edition
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 7:
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
LIS651 lecture 7 PHP mySQL Thomas Krichel
Introduction to Databases Chapter 7: Data Access and Manipulation.
Security, Transactions, and Views. Security Achieved through GRANT & REVOKE Assumes the database can recognize its users and verify their identity can.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Advanced SQL: Cursors & Stored Procedures
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
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
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management Systems based on the Relational Model Developed in 1970s.
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.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
SQL DOM: Compile Time Checking of Dynamic SQL Statements Russel A. McClure Ingolf H. Krüger ICSE 2005 University of California, San Diego Department of.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Oracle Data Integrator User Functions, Variables and Advanced Mappings
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Procedural Constructs in SQL Chapter 5.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
1 Stored Procedures in MySQL Part I. 2 Objectives SQL Vs. MySQL SP MySQL SP Parameters MySQL SP Control Structures.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1. Advanced SQL Functions Procedural Constructs Triggers.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
INLS 623– Stored Procedures
Equations Quadratic in form factorable equations
Stored Procedures.
Programmability by Adrienne Watt.
Views, Stored Procedures, Functions, and Triggers
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Database Application Development
Error Handling Summary of the next few pages: Error Handling Cursors.
CPSC-310 Database Systems
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
CS4222 Principles of Database System
Review for Final Exam.
Local Variables, Global Variables and Variable Scope
CS122B: Projects in Databases and Web Applications Winter 2018
Information Management
CS122B: Projects in Databases and Web Applications Spring 2018
Equations Quadratic in form factorable equations
CS122B: Projects in Databases and Web Applications Winter 2019
Database Application Development
Presentation transcript:

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Advanced Topics

Topics for Today Stored Functions (SQL-Invoked Routines) Stored Procedures (SQL-Invoked Routines) Final Review This Thursday: MySQL Connectors Database-Enabled Applications More Final Review

Stored Functions Simple way to write your own functions Functions must return a single value Stored functions are defined in database schema Not global, must be associated with a database Can use SQL Drawbacks Can’t define your own aggregates For custom aggregates, use C/C++ to make UDFs (user-defined functions).

Stored Functions Single Statement Syntax CREATE FUNCTION name(varname type, varname type, varname type,...) RETURNS data_type RETURN single_query | single_command; Compound Statement Syntax CREATE FUNCTION name(varname type, varname type, varname type,...) RETURNS data_type BEGIN compound_statements END;

Stored Functions Drop Function Syntax DROP FUNCTION [IF EXISTS] name;

Stored Functions Using compound table syntax, you can declare variables initialize variables use conditional statements use loop statements use subqueries Statements must be terminated by a semicolon Statements must occur between BEGIN and END

Stored Functions To declare a variable: DECLARE name data_type [DEFAULT expression]; To initialize a variable: SET name = expression | subquery; To return a value: RETURN expression | subquery;

Stored Functions To define an IF statement: IF expression THEN statement_list ELSEIF expression THEN statement_list ELSE statement_list ENDIF;

Stored Functions To define a LOOP statement: [begin_label] LOOP statement_list END LOOP [end_label] LOOP is similar to for(;;) {} in C++ and Java ITERATE is similar to continue in C++ and Java LEAVE is similar to break in C++ and Java

Stored Functions Example #1: Write a function to solve the quadratic equation. Functions parameters should be a, b, and c for the constants and s = {1, 2} for the first or second solution. CREATE FUNCTION QUADRATIC_V1(a REAL, b REAL, c REAL, r INT) RETURNS REAL RETURN IF(r = 1, (-b + SQRT(b*b - 4*a*c))/(2*a), (-b - SQRT(b*b - 4*a*c))/(2*a));

Stored Functions Example #2: Rewrite the previous example using the compound-statement syntax. CREATE FUNCTION QUADRATIC_V2(a REAL, b REAL, c REAL, r INT) RETURNS REAL BEGIN DECLARE denom, discr REAL; SET discr = SQRT(b*b - 4*a*c); SET denom = 2*a; RETURN IF(r = 1, (-b + discr)/(denom), (-b - discr)/(denom)); END

Stored Functions Example #3: Write a stored function that computes the number of occurrences of a certain character within a string. CREATE FUNCTION OCCURRENCE(s VARCHAR(256), c CHAR(1)) RETURNS INT BEGIN DECLARE retval INT DEFAULT 0; -- A BUNCH OF STUFF GOES HERE RETURN retval; END

Stored Functions Solution: CREATE FUNCTION OCCURRENCE(s VARCHAR(256), c CHAR(1)) RETURNS INT BEGIN DECLARE retval INT DEFAULT 0; DECLARE i INT DEFAULT 1; loop_label: LOOP IF i > CHAR_LENGTH(s) THEN LEAVE loop_label; END IF; IF SUBSTRING(s, i, 1) = c THEN SET retval = retval + 1; END IF; SET i = i + 1; END LOOP; RETURN retval; END

Stored Procedures A stored procedure is a stored function that does not return a value Stored procedures are defined in database schema Not global, must be associated with a database Can use SQL Provides an extra layer of security If only stored procedures are allowed (no selection queries or commands), what you can do to the database is limited by the procedures the database supports

Stored Procedures Syntax is the same as stored functions, with two exceptions: No RETURNS and RETURN statements You cannot use a stored procedure in a selection query, you must use the CALL keyword CALL Syntax CALL stored_procedure_name(param1, param2,...);

Stored Procedures Example: Write a stored procedure for the toy store database that inserts a new genre into the database. CREATE PROCEDURE ADD_GENRE(g VARCHAR(32)) BEGIN INSERT INTO genre VALUES(0, g); END