CSE 3330 Database Concepts Stored Procedures How to create a user CREATE USER.. http://dev.mysql.com/doc/refman/5.1/en/create-user.html GRANT PRIVILEGE http://dev.mysql.com/doc/refman/5.1/en/grant.html Find list of users select user from mysql.user; • A wealth of information exists in mysql database use mysql; Show tables; Desc user; Stored Procedure • SP is a code written in SQL that is compiled and stored on the DB server. • Used for repetitive tasks. • You can use programming language constructs like variables, loops, assignments, cursors, etc • Pre-compiled => Efficient Background Good background http://code.tutsplus.com/articles/an-introduction-tostored-procedures-in-mysql-5--net-17843 Advantages: - Share logic - Grant users permissions to SP rather than tables - Security - Improved Performance, reduces network traffic Simple SP A stored procedure has a name, a parameter list, and an SQL statement, whi many more SQL statements. There is new syntax for local variables, error handlin and IF conditions. Here is an example of a statement that creates a stored proced CREATE PROCEDURE procedure1 /* name */ (IN parameter1 INTEGER) /* parameters */ BEGIN /* start of block */ DECLARE variable1 CHAR(10); /* variables */ IF parameter1 = 17 THEN /* start of IF */ SET variable1 = 'birds'; /* assignment */ ELSE SET variable1 = 'beasts'; /* assignment */ END IF; /* end of IF */ INSERT INTO table1 VALUES (variable1);/* statement */ END /* end of block */ What I'm going to do is explain in detail all the things you can do with stored pr also get into another new database object, triggers, because there is a tendency to associate triggers with stored procedures. y Stored Procedures Simple SP DELIMITER $$ -- Create a procedure in Oracle. CREATE PROCEDURE hello_world() BEGIN -- Print the phrase and a line return. SELECT 'Hello World!'; END; $$ -- Reset the delimiter back to a semicolon to work again. DELIMITER ; -- Call the procedure. SELECT 'CALL hello_world' AS "Statement"; CALL hello_world(); Simple SP Why do we change the delimiter? DELIMITER ; Calling SP Database changed SP Examples I avoid using a real database that might have important data in it. And now I'm making a simple table to work with. The statements that I use for this are: mysql> CREATE DATABASE db5; Query OK, 1 row affected (0.01 sec) mysql> USE db5; Database changed mysql> CREATE TABLE t (s1 INT); Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO t VALUES (5); Query OK, 1 row affected (0.00 sec) You'll notice that I'm only inserting one row into the table. I want my sample table to be simple. I'm not showing off table manipulation here, I'm showing off stored procedures, and they're complex enough without worrying about big tables. Copyright © 2005, MySQL AB Pag is the same as: SP CREATE PROCEDURE p2 () SELECT CURRENT_DATE, RAND() FROM t // The characteristics clauses have defaults. If I omit them all LANGUAGE SQL NOT DETERMINISTIC SQL SECURITY DEFINE opyright © 2005, MySQL AB Calling SP from PDO http://www.mysqltutorial.org/php-calling-mysql-storedprocedures/ http://www.php.net/manual/en/mysqli.quickstart.storedprocedures.php http://www.joeyrivera.com/2009/using-mysql-storedprocedures-with-php-mysqlmysqlipdo/ Functions Function MUST return a value, Procedure does not have to. Function invoked within an expression, Procedure invoked with Call Looping Constructs http://dev.mysql.com/techresources/articles/mysqlstoredprocedures.pdf#page=21&zoom=auto,0,792 Cursors http://dev.mysql.com/techresources/articles/mysqlstoredprocedures.pdf#page=35&zoom=auto,0,792 How to backup a db MySQL has functions for backing up entire db - includes tables + procedures + functions + .. http://www.thegeekstuff.com/2008/09/backupand-restore-mysql-database-using-mysqldump/ Backup: $ mysqldump -u root -p sugarcrm > sugarcrm.sql Learn to backup your db regularly How to restore a db http://www.thegeekstuff.com/2008/09/backupand-restore-mysql-database-using-mysqldump/ Restore: $ mysql -u root -p sugarcrm < /tmp/sugarcrm.sql Triggers A trigger is a SQL statement that is executed (or “fired”) when another event occurs. For example, a trigger may fire when you insert data into a table, update a table, delete a row, etc. Work through the examples: https://dev.mysql.com/doc/refman/5.0/en/triggersyntax.html