17

Views and functions

Embed Size (px)

Citation preview

Page 1: Views and functions
Page 2: Views and functions

VIEWS AND USER DEFINED FUNCTION

Arun RJ

[email protected]

Facebook Profile

Twitter Profile

www.linkedin.com/arun rj

Page 3: Views and functions

Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial document of baabtra.com – Mentoring Partner

Page 4: Views and functions

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.

Page 5: Views and functions

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

Page 6: Views and functions

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

Page 7: Views and functions

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

Page 8: Views and functions

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

Page 9: Views and functions

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

Page 10: Views and functions

The define statement…

CREATE FUNCTION function_name(parameters) RETURNS(return_data type){statements;}

Page 11: Views and functions

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//

Page 12: Views and functions

mysql> select fn_getvalue('amal');

Select fn_getvalue(‘amal’); //function call

+---------------------+| fn_getvalue('amal') |

+---------------------+| 8.5 |

+---------------------+1 row in set (0.00 sec)

Page 13: Views and functions

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//

Page 14: Views and functions

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’);

Page 15: Views and functions

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.

Page 16: Views and functions

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

Page 17: Views and functions

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