Java
Java
Procedure -(IN,OUT,INOUT parameter) returns one or more data set as OUT parameters, possible to
use dynamic SQL
Function - has only input parameters, return one value of a given type, cannot use dynamic SQL, can
return data set
Trigger - special kind of stored procedure that automatically executes when an event occurs in the
database server
Stored Procedure
A stored procedure is a set of SQL code which is stored in the database server and can be invoked by
program or trigger or stored procedure itself. The stored procedure is a way to execute may task on
your database server directly, in other words stored procedure is a way to execute your business logic
directly into the database server.
Suppose you have a normal banking system where you want to update interest of a loan of any specific
user. What you will normally do from your program? You will fetch capital, the rate of interest and
duration and calculate interest and then you will go back to the database and save the data. In this
very small example we are going 2 time in database, but if we will use the stored procedure then we
need to write all operation within our stored procedure and we will call it through the program one
time. In this case you will fetch data like capital, rate of interest and duration within database(with
stored procedure) and you will save data after processing. In this case we are only interrupting
database server one time. Thanks to the stored procedure which reduced our server overhead.
1. Multiple applications are running in multiple environment and need to use the same
database. By using stored procedure you can make your business logic independent of
programming language.
2. When security is main concern use of stored procedure is vital. By doing your operation
through the database you can log your all performed action. Banking site is the best
example.
3. If you are using stored procedure then you do not have table access directly which is one
more way to secure the data and transaction.
4. Stored procedure increases performance of your application sometime.
5. If your application is big or your database server on remote system then by using stored
procedure you can decrease the traffic between your database server and application
server.
6. Since stored procedure is written in your database server and application call it sepratly then
the degree of re-usability.
1. Sometime use of stored procedure is bit risky. Store procedure follow “define one use many
time” philosophy. Doing change in stored procedure directly affect your data so it should
always be use with very carefully.
2. Stored procedure are set of sql command form our logic so sometime programmer need to
debug the stored procedure. In mysql stored procedure it is very hard to debug.
3. Managing stored procedure is bit difficult because it does not have any object oriented
paradigm like things.
4. If you use a lot of stored procedures, the memory usage of every connection that is uing
those stored procedures will increase substantially. In addition, if you overuse a large
number of logical operations inside stored procedures, the CPU usage will also incrase
because database server is not well-designed for logical operations.
5. Developing and maintaining stored procedures are often required specialized skill.
Question 01
DROP PROCEDURE IF EXISTS p1;
CREATE
PROCEDURE p1()
Select 'hello test';
Quesion 02
Quesion 03
Quesion 04
Now you can see the output like ‘This is my test program’
Quesion 05
Initialize veriable
Var3 is null – define the reason and correct it
Quesion 06
Quesion 07
NOTE
Quesion 08
The delimiter is the character or string of characters that you'll use to tell the mySQL client that
you've finished typing in an SQL statement. For ages, the delimiter has always been a semicolon .
That, however, causes problems, because, in a stored procedure, one can have many statements, and
each must end with a semicolon.
Quesion 09
create table item(
id varchar(10),
name varchar(100),
price decimal(10,2),
constraint primary key(id)
);
INTO can be used to return the sql value to the procedure variable,
remember one value can be retured to the procedure variable.
Quesion 10
See the different in Q3 and Q4
DROP PROCEDURE IF EXISTS p10;
DELIMITER $$
CREATE
PROCEDURE p10()
BEGIN
select min(price)from item;
select max(price)from item;
select avg(price)from item;
END$$
DELIMITER ;
Quesion 11
Quesion 12
Quesion 13
Quesion 14
Quesion 15
Quesion 16
Quesion 17
END; /* -- Line C */
Line A: Declare a stuats variable that will record the status of our
insert attempt
Line E: Execution will then continue on this line, where we check the
value of the variable and - if the handler has fired - advise the user
that the insert was unsucessful.
Quesion 19
Create table t1(
Id int
);
Create table t2(
Id int
);
Create table t3(
Id int
);
Quesion 20
Solution
Exam Questions
Question 01
Student(sid:varchar(10),name:varchar(100),contact:int)
PK-sid
Course(cid:int, cname:varchar(100),fee:decimal(10,2))
Registration(rid:int,rdate:date,fee:decimal(10,2),cid:int,sid:
varchar(100))
Commision(comid:int,rid:int,commission:decimal(10,2))