View
117
Download
0
Embed Size (px)
Citation preview
VIEWS AND USER DEFINED FUNCTION
Arun RJ
Facebook Profile
Twitter Profile
www.linkedin.com/arun rj
Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial document of baabtra.com – Mentoring Partner
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.
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
Create view vw_tbl_std as select int_std_id,vchr_std_namefrom tbl_std;
Int_std_id Vchr_std_name
1 amal
2 arun
3 rahul
4 raju
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
User Defined Functions In mysql
A function is a group of statements that executes upon requestSQL provides many built in function and allow us to define user defined functionA request to execute a function is called a function callWhen a function is called it should pass arguments that specify data upon which the function perform its computation
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
The define statement…
CREATE FUNCTION function_name(parameters) RETURNS(return_data type){statements;}
Example…
DELIMITER //CREATE FUNCTION fn_getvalue( name varchar(20)) RETURNS float DETERMINISTIC READS SQL DATABEGINDECLARE value float;SET value=45;IF name='amal' THENSET value=8.5;END IF;RETURN value;END//
mysql> select fn_getvalue('amal');
Select fn_getvalue(‘amal’); //function call
+---------------------+| fn_getvalue('amal') |
+---------------------+| 8.5 |
+---------------------+1 row in set (0.00 sec)
Example…delimiter //CREATE FUNCTION fn_getdeliveryprice2
(p_country Varchar(50), p_city Varchar(50)) RETURNS Float DETERMINISTIC READS SQL DATABEGIN DECLARE v_price Float; SET v_price = 45; IF p_country = 'iceland' THENIF p_city='warhan'THEN SET v_price=8.15; ELSE SET v_price=7.15;END IF; END IF; RETURN v_price; END//
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 intDETERMINISTIC READS SQL DATABEGINDECLARE 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’);
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 designedto make a quick, good software professional out of anybody who holds a passion for coding.
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
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode
Start up VillageEranakulam,Kerala, India.
Email: [email protected]
Contact Us