2. VIEWSAND USER DEFINED FUNCTION
Arun RJ
rjarun08@gmail.com
Facebook Profile
TwitterProfile
www.linkedin.com/arunrj
3. Disclaimer: This presentation is prepared by trainees of
baabtra.com as a part of mentoring program. This is not
official document of baabtra.com – Mentoring Partner
4. VIEWS
A view is a virtual table.
In SQL, a view is a virtual table based on the result-set of an SQL
statement.
A view contains rows and columns, just like a real table. The
fields in a view are fields from one or more real tables in the
database.
5. SYNTAX OF VIEW
CREATE VIEW view_name AS SELECT column_name(s)
FROM table_name WHERE condition
Example…
Int_std_id Vchr_std_name Int_std_age Vchr_std_city
1 amal 12 calicut
2 arun 13 cochin
3 rahul 13 kollam
4 raju 12 kottayam
6. Create view vw_tbl_std as select int_std_id,vchr_std_name
from tbl_std;
Int_std_id Vchr_std_name
1 amal
2 arun
3 rahul
4 raju
7. You can modify the definition of a view by the following syntax
alter view view_name as select coloumn name from table name;
Example…
alter view vw_tbl as select std_age from tbl_std;
the above query will alter the previous view as below
std_age
12
13
13
12
8. User Defined Functions In mysql
A function is a group of statements that executes
upon request
SQL provides many built in function and allow us to
define user defined function
A request to execute a function is called a function
call
When a function is called it should pass arguments
that specify data upon which the function perform
its computation
9. 1. User defined functions can pass input parameters
2. user defined functions are compiled and executed at run
time,so it is slower than stored procedure
3. user defined functions can’t return deterministic values like
getdate()
4. stored procedure cannot be called from inside a user defined
function where as a stored procedure can call user defined
function and can call another stored procedure inside it
11. Example…
DELIMITER //
CREATE FUNCTION fn_getvalue( name varchar(20)) RETURNS
float DETERMINISTIC READS SQL DATA
BEGIN
DECLARE value float;
SET value=45;
IF name='amal' THEN
SET value=8.5;
END IF;
RETURN value;
END//
13. Example…
delimiter //
CREATE FUNCTION fn_getdeliveryprice2
(p_country Varchar(50), p_city Varchar(50)) RETURNS
Float DETERMINISTIC READS SQL DATA
BEGIN
DECLARE v_price Float;
SET v_price = 45;
IF p_country = 'iceland' THEN
IF p_city='warhan'
THEN SET v_price=8.15;
ELSE SET v_price=7.15;
END IF;
END IF;
RETURN v_price;
END//
14. Problem-create a user defined function to find no of jobs applied by a
person
DELIMITER //
CREATE FUNCTION fn_no_of_job3(vchr_a varchar(100)) RETURNS int
DETERMINISTIC READS SQL DATA
BEGIN
DECLARE s int;
select int_no_of_jobs_applied into s from tbl_jobs2 where
vchr_user_name=vchr_a;
RETURN s;
END //
DELIMITER//
select fn_no_of_job3(‘amal’);
15. Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
16. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
17. Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us