CSE 3330 Database Concepts Stored Procedures
How to create a user CREATE USER.. GRANT PRIVILEGE
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 stored-procedures-in-mysql-5--net Advantages: -Share logic -Grant users permissions to SP rather than tables -Security -Improved Performance, reduces network traffic
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 DELIMITER ; Why do we change the delimiter?
Calling SP
SP Examples
SP
Calling SP from PDO procedures/ procedures.php procedures-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 resources/articles/mysql- storedprocedures.pdf#page=21&zoom=auto,0,792
Cursors resources/articles/mysql- storedprocedures.pdf#page=35&zoom=auto,0,792
How to backup a db MySQL has functions for backing up entire db - includes tables + procedures + functions and-restore-mysql-database-using-mysqldump/ Backup: $ mysqldump -u root -p sugarcrm > sugarcrm.sql Learn to backup your db regularly
How to restore a db and-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: syntax.html