DBMS Exp9

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Jay Patel: 60004210114 SE

Database Management System

Experiment No-9

Topic: College Management System

Aim: Write Functions, cursor and procedure.


Theory: A procedure (often called a stored procedure) is a subroutine like a subprogram in a
regular computing language, stored in database. A procedure has a name, a parameter list,
and SQL statement(s). All most all relational database system supports stored procedure,
MySQL 5 introduce stored procedure.MySQL 5.6 supports "routines" and there are two kinds
of routines : stored procedures which you call, or functions whose return values you use in
other SQL statements the same way that you use pre-installed MySQL functions like pi().
The major difference is that UDFs can be used like any other expression within SQL
statements, whereas stored procedures must be invoked using the CALL statement

Create a procedure:

CREATE [DEFINER = { user | CURRENT_USER }]


PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
proc_parameter: [ IN | OUT | INOUT ] param_name type
type:
Any valid MySQL data type
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA
| MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
routine_body:

Pick a Delimiter :The delimiter is the character or string of characters which is used to
complete an SQL statement. By default we use semicolon (;) as a delimiter. But this causes
problem in stored procedure because a procedure can have many statements, and everyone
must end with a semicolon. So for your delimiter, pick a string which is rarely occur within
statement or within procedure. Here we have used double dollar sign i.e. $$.You can use
whatever you want. To resume using ";" as a delimiter later, say "DELIMITER ; $$". See
here how to change the delimiter :

mysql> DELIMITER $$ ;
Now the default DELIMITER is "$$". Let execute a simple SQL command :

mysql> SELECT * FROM user $$


Example: Simple MySQL Procedure

Here we have created a simple procedure called library_data(), when we will execute the
procedure it will display all the data from "library" tables.

mysql> CREATE PROCEDURE library_data()

-> select * from library;//

Call a procedure

The CALL statement is used to invoke a procedure that is stored in a DATABASE. Here is
the syntax:

mysql> CALL library_data();

-> //
MySQL Procedure : Parameter IN example

In the following procedure, we have used a IN parameter no (type integer) which accept a
number from the user. Within the body of the procedure, there is a SELECT statement which
rows from library table which has no of books greater than the parameter given in the
procedure :

mysql> CREATE PROCEDURE books(IN no INT)

-> BEGIN

-> select * from library where no_ofbooks > no;

-> END

-> //

Query OK, 0 rows affected (0.13 sec)

Call a procedure

The CALL statement is used to invoke a procedure that is stored in a DATABASE. Here is
the syntax:

mysql> CALL library_data();

-> //
MySQL Procedure : Parameter OUT example

The following example shows a simple stored procedure that uses an OUT parameter. Within
the procedure MySQL MAX() function retrieves maximum no of books from library table for
a particular book.

mysql> CREATE PROCEDURE max_books(OUT max_books INT)

-> BEGIN

-> select max(no_ofbooks) INTO max_books from library;

-> END

-> //

Query OK, 0 rows affected (0.12 sec)

Calling the procedure:

In the body of the procedure, the parameter will get the maximum number of books from the
library table and return it to the max_books with return type as int and while calling the
procedure it the result is stored in @B variable which will further displayed.

mysql> CALL max_books(@B);//

Query OK, 1 row affected (0.10 sec)


MySQL Procedure : Parameter INOUT example

The following example shows a simple stored procedure that uses an INOUT parameter and
an IN parameter. In this procedure it will the count of staff having salary greater than the
salary pass in the parameter as salary and it will return total count as total variable which.
While calling the procedure it will pass the salary and it will store the result in the @T

variable

mysql> CREATE PROCEDURE count_salary(INOUT total INT , IN salary INT)

-> BEGIN

-> select count(salary) INTO


total from staff where
staff.salary > salary;

-> END

-> //

Query OK, 0 rows affected (0.10 sec)


Calling the procedure:

Now, while calling the procedure 1 parameter is passed salary and result is stored in the
variable @T

mysql> CALL count_salary(@T,30000)//

Query OK, 1 row affected (0.10 sec)


MYSQL: Creating stored function:
The CREATE FUNCTION statement is used for creating a stored function and user-defined
functions. A stored function is a set of SQL statements that perform some operation and
return a single value.
Just like Mysql in-built function, it can be called from within a Mysql statement.
By default, the stored function is associated with the default database.
The CREATE FUNCTION statement require CREATE ROUTINE database privilege.
Syntax:
The syntax for CREATE FUNCTION statement in Mysql is:

CREATE FUNCTION function_name(func_parameter1, func_parameter2, ..)


RETURN datatype [characteristics]
func_body

Example:

mysql> CREATE FUNCTION calculate_age(dob date)RETURNS INT DETERMINISTIC

-> BEGIN

-> DECLARE temp DATE;

-> select current_date() into temp;

-> RETURN year(temp) - year(dob);

-> END

-> //

Query OK, 0 rows affected (0.17 sec)


Calling of above function:

mysql> select Student_id , Name , DOB , calculate_age(DOB) as 'age' from student//

MySQL: Cursors

A database cursor is a control structure that enables traversal over the records in a database.
Cursors are used by database programmers to process individual rows returned by database
system queries. Cursors enable manipulation of whole result sets at once. In this scenario, a
cursor enables the rows in a result set to be processed sequentially. In SQL procedures, a
cursor makes it possible to define a result set (a set of data rows) and perform complex logic
on a row by row basis. By using the same mechanics, an SQL procedure can also define a
result set and return it directly to the caller of the SQL procedure or to a client application.

MySQL supports cursors inside stored programs. The syntax is as in embedded SQL. Cursors
have these properties :
- Asensitive: The server may or may not make a copy of its result table
- Read only: Not updatable
- Nonscrollable: Can be traversed only in one direction and cannot skip rows
To use cursors in MySQL procedures, you need to do the following :
- Declare a cursor.
- Open a cursor.
- Fetch the data into variables.
- Close the cursor when done.

Declare a cursor:

The following statement declares a cursor and associates it with a SELECT statement that
retrieves the rows to be traversed by the cursor.

DECLARE cursor_name
CURSOR FOR select_statement
Open a cursor:

The following statement opens a previously declared cursor.

OPEN cursor_name
Fetch the data into variables :

This statement fetches the next row for the SELECT statement associated with the specified
cursor (which must be open) and advances the cursor pointer. If a row exists, the fetched
columns are stored in the named variables. The number of columns retrieved by the SELECT
statement must match the number of output variables specified in the FETCH statement.

FETCH [[NEXT] FROM] cursor_name


INTO var_name [, var_name] ...
Close the cursor when done :

This statement closes a previously opened cursor. An error occurs if the cursor is not open.

CLOSE cursor_name

Conclusion: Procedure and functions are implemented with cursor.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy