Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
CSC 2720 Building Web Applications Database and SQL.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
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.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Python MySQL Database Access
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
ABC Insurance Co. Paul Barry Steve Randolph Jing Zhou CSC8490 Database Systems & File Management Dr. Goelman Villanova University August 2, 2004.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CSC 2720 Building Web Applications Database and SQL.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
CSE 3330 Database Concepts Stored Procedures. How to create a user CREATE USER.. GRANT PRIVILEGE.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
Advanced SQL: Cursors & Stored Procedures
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
CMPE 226 Database Systems October 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
CS453: Databases and State in Web Applications (Part 2) Prof. Tom Horton.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
CHAPTER 10 PHP MySQL Database
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
Introduction to Database Programming with Python Gary Stewart
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Web Systems & Technologies
COMP 430 Intro. to Database Systems
Databases.
CS320 Web and Internet Programming SQL and MySQL
Web Technologies IT230 Dr Mohamed Habib.
Instructor: Jason Carter
CS311 Database Management system
Chapter 8 Advanced SQL Pearson Education © 2014.
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
CS122B: Projects in Databases and Web Applications Winter 2018
Chapter 8 Advanced SQL.
CS3220 Web and Internet Programming SQL and MySQL
Information Management
CS122B: Projects in Databases and Web Applications Spring 2018
Chapter 8 Advanced SQL Pearson Education © 2009.
CS3220 Web and Internet Programming SQL and MySQL
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008

1. dev.mysql.com mysql-triggers.html

DELIMITER $$ -- set delimeter (allow ; inside) CREATE TRIGGER newsCounter AFTER INSERT ON News FOR EACH ROW BEGIN INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News); END; $$ DELIMITER ; -- reset delimeter

CREATE TABLE NewsCategories ( catID int not null auto_increment, catName varchar(32), primary key(catID)); CREATE TABLE News ( newsID int not null auto_increment, catID int not null, title varchar(32) not null, txt blob, primary key(newsID)); CREATE TABLE NewsCount ( newsItemCount int );

DELIMITER $$ CREATE TRIGGER newsCategoryHandler AFTER DELETE ON NewsCategories FOR EACH ROW BEGIN DELETE FROM News WHERE catID=OLD.catID; END; $$ DELIMETER ; -- Note: can reference NEW.attr on insert, update

DELIMITER $$ CREATE TRIGGER newsCounter AFTER INSERT ON News FOR EACH ROW BEGIN DELETE FROM NewsCount; INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News); END; $$

 DROP TRIGGER newsCounter;  SHOW TRIGGERS;  One trigger per event, per table  Can add procedural elements, such as IF statements  See reference 4 (databasedesign- resource) for another example

 Why go to the trouble of extracting logic from your application, putting it into a different format, and placing it on the database server? There are several advantages to doing so. Here is a (incomplete) list of some of the most commonly sited advantages:  MySQL stored procedures can greatly cut down on the amount of traffic going back and forth over your network. (usually FASTER in general than using app program)  Stored procedures can greatly improve the security of your database server. SQL that is executed on the server is not subject to SQL injection attacks.  Stored procedures provide a way to abstract and separate data access routines from the business logic of your application.  Stored procedures allow these routines to be accessed by programs using different platforms and API's, and make your applications more portable.  From source 4 (databasedesign-resource)

 Block structured language similar to Oracle PL/SQL and IBM DB2 SQL  Some folks recommend using MySQL query browser to aid creation, but can be done from command line  Seeing what you have SHOW PROCEDURE STATUS; SHOW PROCEUDRE LIKE ‘%Test%’; SHOW CREATE PROCEDURE myproc;

 Sample DB -- create News table, be sure to be in a 'test' DB CREATE TABLE News (NewsID int auto_increment not null, Title varchar(32), primary key(NewsID))

DELIMITER $$ DROP PROCEDURE IF EXISTS sprocTest $$ CREATE PROCEDURE sprocTest (id int, title varchar(32)) BEGIN -- INSERT NEW RECORD IF PREEXISTING RECORD DOESNT EXIST IF (id = 0) THEN SET id = null; END IF; IF (id IS NOT NULL) AND (EXISTS (SELECT * FROM News WHERE NewsID=id)) THEN UPDATE News SET Title=title WHERE NewsID=id; ELSE INSERT INTO News (Title) VALUES (title); END IF; END $$ DELIMITER ; To call: CALL sprocTest(1,'Some News Title'); -- this will update recordID 1

 Using cursors Let’s us loop on each row returned from a query (the result set) see design-resources link (also on next slide)

stored procedure using cursors in a loop To use: call events(‘11/09’); more detailed tutorial at mysqltutorial.org

 First: Chapter 9 Slides from Elmasri 5 th edition  Some sources for Java access /docs/mysql_java.pdf use-mysql-with-java/ use-mysql-with-java/  Can also access mysql with Perl, PHP, Python, Ruby