You are on page 1of 4

A. Stored Procedure Definition A stored procedure is a segment of declarative SQL statements stored inside the database catalog.

. Example CREATE PROCEDURE `var_proc` (IN paramstr VARCHAR(20)) BEGIN DECLARE a, b INT DEFAULT 5; DECLARE str VARCHAR(50); DECLARE today TIMESTAMP DEFAULT CURRENT_DATE; DECLARE v1, v2, v3 TINYINT; INSERT INTO table1 VALUES (a); SET str = 'I am a string'; SELECT CONCAT(str,paramstr), today FROM table2 WHERE b >=5; END Advantages 1. MySQL stored procedures are compiled on demand. After compiling a stored procedure, MySQL puts it to a cache. And MySQL maintains its own stored procedure cache for every single connection. If an application uses a stored procedure multiple times in a single connection, the compiled version is used, otherwise the stored procedure works like a query. 2. Stored procedures helps reduce the traffic between application and database server because instead of sending multiple lengthy SQL statements, the application has to send only name and parameters of the stored procedure. Disadvantages 1. Increased load on the database server most of the work is done on the server side, and less on the client side. 2. Migrating to a different database management system (DB2, SQL Server, etc) may potentially be more difficult. 3. It is difficult to debug stored procedures. Security 1. Stored procedures are secure. Database administrator can grant appropriate permissions to applications that access stored procedures in the database without giving any permission on the underlying database tables.

B. Function Definition A user-defined function (UDF) is a way to extend MySQL with a new function that works like a native (built-in) MySQL function such as ABS() or CONCAT(). Example CREATE FUNCTION hello_world(addressee TEXT) RETURNS TEXT BEGIN DECLARE strlen INT; SET strlen = LENGTH(addressee); RETURN CONCAT('Hello ', addressee, ' - your parameter has ', strlen, ' characters'); END; C. Difference between SP & Function Stored Procedure Doesn't need to return values, but can return value. Stored as a pseudo-code in database i.e. compiled form. Can affect the state of database using commit etc. Cannnot be invoked from SQL statements e.g. SELECT. Procedures are mainly used to process the tasks

Function Should return atleast one output parameter.Can return more than one parameter using OUT argument. Parsed and compiled at runtime. Cannot affect the state of database. Can be invoked from SQL statement e.g. SELECT. Functions are mainly used to compute values.

D. Trigger Definition A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update. For example, we can install a stored procedure that is triggered each time a record is deleted from a transaction table and that invoke stored procedure automatically deletes the corresponding customer from a customer table when all his transactions are deleted. Example CREATE TRIGGER upd_check BEFORE UPDATE ON account FOR EACH ROW BEGIN IF NEW.amount < 0 THEN SET NEW.amount = 0;

ELSEIF NEW.amount > 100 THEN SET NEW.amount = 100; END IF; END; Advantages 1. Trigger is automatic. Triggers are not invoked from the Application end rather it will be invoked from the Database Management System. 2. Triggers are used to identify changes (like insert,update or delete) in database tables and captured in temp tables seperately. Disadvantages 1. Triggers run every time when the database fields are updated and it is overhead on system. It makes system run slower. 2. It is easy to forget about triggers and if there is no documentation it will be difficult to figure out for new developers for their existence. Using triggers to block malicious code: an example CREATE TRIGGER my_users_bu BEFORE UPDATE ON my_users FOR EACH ROW BEGIN IF (NEW.username='admin') THEN SELECT 0 INTO @admin_error FROM `Cannot modify admin data!`; END IF; END

E. Cursor Definition In mysql cursor is a kind of loop facility given to traverse in the result of sql one by one. By using cursor in mysql we can operate on every result of the record. Cursor are supported in stored procedure and function only. By using MySql cursor we can loop through every row of the result set. MySQL cursor is read only, non-scrollable and asensitive. Example CREATE PROCEDURE build_email_list (INOUT email_list varchar(4000)) BEGIN DECLARE v_finished INTEGER DEFAULT 0; DECLARE v_email varchar(100) DEFAULT ""; DEClARE email_cursor CURSOR FOR SELECT email FROM employees; DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;

OPEN email_cursor; get_email: LOOP FETCH email_cursor INTO v_email; IF v_finished = 1 THEN LEAVE get_email; END IF; SET email_list = CONCAT(v_email,";",email_list); END LOOP get_email; CLOSE email_cursor; END Advantages 1. Cursors are best used when you want each row or more than one row one by one. 2. Its efficient because with Cursor we are doing operations so there is no need to write complex queries(like joins) Disadvantages 1. Cursor is faster than a while loop but it create more overhead in database. 2. Cursor fetching one by one data from database so if data is more, its take more execution time.