logging in or signing up Plsql aSGuest58691 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 107 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: August 04, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript MySQL Lectures Notes : MySQL Lectures Notes Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008 Sources : Sources dev.mysql.com www.mysqltutorial.org www.digitalpropulsion.org www.databasedesign-resource.com/ mysql-triggers.html Trigger Example : Trigger Example DELIMITER $$ -- set delimeter (allow ; inside) CREATE TRIGGER newsCounterAFTER INSERT ON NewsFOR EACH ROW BEGIN INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News);END;$$ DELIMITER ; -- reset delimeter Sample DB : Sample DB 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 ); Trigger Example : Trigger Example DELIMITER $$CREATE TRIGGER newsCategoryHandlerAFTER DELETE ON NewsCategoriesFOR EACH ROW BEGIN DELETE FROM News WHERE catID=OLD.catID;END;$$ DELIMETER ; -- Note: can reference NEW.attr on insert, update Trigger Example : Trigger Example DELIMITER $$CREATE TRIGGER newsCounterAFTER INSERT ON NewsFOR EACH ROW BEGIN DELETE FROM NewsCount; INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News);END; $$ Trigger Statements : Trigger Statements 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 Stored Procedures : Stored Procedures 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) Stored Procedures : Stored Procedures 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; Stored Procedures : Stored Procedures Sample DB -- create News table, be sure to be in a 'test' DBCREATE TABLE News (NewsID int auto_increment not null, Title varchar(32), primary key(NewsID)) Stored Procedures : Stored Procedures 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 Stored Procedures : Stored Procedures Using cursors Let’s us loop on each row returned from a query (the result set) see design-resources link (also on next slide) Slide 13: stored procedure using cursors in a loop To use: call events(‘11/09’); more detailed tutorial at mysqltutorial.org Program DB Access : Program DB Access First: Chapter 9 Slides 18-37 from Elmasri 5th edition Some sources for Java access http://www.kitebird.com/articles/jdbc.html http://www.cs.ucdavis.edu/~devanbu/teaching/160/docs/mysql_java.pdf http://www.romow.com/computer-blog/how-to-use-mysql-with-java/ Can also access mysql with Perl, PHP, Python, Ruby You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Plsql aSGuest58691 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 107 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: August 04, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript MySQL Lectures Notes : MySQL Lectures Notes Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008 Sources : Sources dev.mysql.com www.mysqltutorial.org www.digitalpropulsion.org www.databasedesign-resource.com/ mysql-triggers.html Trigger Example : Trigger Example DELIMITER $$ -- set delimeter (allow ; inside) CREATE TRIGGER newsCounterAFTER INSERT ON NewsFOR EACH ROW BEGIN INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News);END;$$ DELIMITER ; -- reset delimeter Sample DB : Sample DB 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 ); Trigger Example : Trigger Example DELIMITER $$CREATE TRIGGER newsCategoryHandlerAFTER DELETE ON NewsCategoriesFOR EACH ROW BEGIN DELETE FROM News WHERE catID=OLD.catID;END;$$ DELIMETER ; -- Note: can reference NEW.attr on insert, update Trigger Example : Trigger Example DELIMITER $$CREATE TRIGGER newsCounterAFTER INSERT ON NewsFOR EACH ROW BEGIN DELETE FROM NewsCount; INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News);END; $$ Trigger Statements : Trigger Statements 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 Stored Procedures : Stored Procedures 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) Stored Procedures : Stored Procedures 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; Stored Procedures : Stored Procedures Sample DB -- create News table, be sure to be in a 'test' DBCREATE TABLE News (NewsID int auto_increment not null, Title varchar(32), primary key(NewsID)) Stored Procedures : Stored Procedures 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 Stored Procedures : Stored Procedures Using cursors Let’s us loop on each row returned from a query (the result set) see design-resources link (also on next slide) Slide 13: stored procedure using cursors in a loop To use: call events(‘11/09’); more detailed tutorial at mysqltutorial.org Program DB Access : Program DB Access First: Chapter 9 Slides 18-37 from Elmasri 5th edition Some sources for Java access http://www.kitebird.com/articles/jdbc.html http://www.cs.ucdavis.edu/~devanbu/teaching/160/docs/mysql_java.pdf http://www.romow.com/computer-blog/how-to-use-mysql-with-java/ Can also access mysql with Perl, PHP, Python, Ruby