SlideShare a Scribd company logo
1 of 17
VIEWSAND USER DEFINED FUNCTION
Arun RJ
rjarun08@gmail.com
Facebook Profile
TwitterProfile
www.linkedin.com/arunrj
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
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_name
from 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 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
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 DATA
BEGIN
DECLARE value float;
SET value=45;
IF name='amal' THEN
SET 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 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//
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’);
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.
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 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

More Related Content

What's hot

Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
Vivek Singh
 

What's hot (19)

Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQL
 
Les10
Les10Les10
Les10
 
Mysql quick guide
Mysql quick guideMysql quick guide
Mysql quick guide
 
Les02
Les02Les02
Les02
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Web Developer make the most out of your Database !
Web Developer make the most out of your Database !Web Developer make the most out of your Database !
Web Developer make the most out of your Database !
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Les09
Les09Les09
Les09
 
Data Manipulation(DML) and Transaction Control (TCL)
Data Manipulation(DML) and Transaction Control (TCL)  Data Manipulation(DML) and Transaction Control (TCL)
Data Manipulation(DML) and Transaction Control (TCL)
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
DML Commands
DML CommandsDML Commands
DML Commands
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
 

Viewers also liked (8)

Control structers in c
Control structers in cControl structers in c
Control structers in c
 
Ajax
AjaxAjax
Ajax
 
Dom structure
Dom structure Dom structure
Dom structure
 
Post and get methods
Post and get methodsPost and get methods
Post and get methods
 
Baabtra.com little coder chapter - 7
Baabtra.com little coder   chapter - 7Baabtra.com little coder   chapter - 7
Baabtra.com little coder chapter - 7
 
Ajax
Ajax Ajax
Ajax
 
Json
JsonJson
Json
 
Html5
Html5Html5
Html5
 

Similar to Views and functions

Les08-Oracle
Les08-OracleLes08-Oracle
Les08-Oracle
suman1248
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
Reka
 

Similar to Views and functions (20)

Oracle
OracleOracle
Oracle
 
Chap 7
Chap 7Chap 7
Chap 7
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
A Tour to MySQL Commands
A Tour to MySQL CommandsA Tour to MySQL Commands
A Tour to MySQL Commands
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
 
Les11
Les11Les11
Les11
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Les08-Oracle
Les08-OracleLes08-Oracle
Les08-Oracle
 
Les01
Les01Les01
Les01
 
View
ViewView
View
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sql
SqlSql
Sql
 
Oracle training in hyderabad
Oracle training in hyderabadOracle training in hyderabad
Oracle training in hyderabad
 

More from baabtra.com - No. 1 supplier of quality freshers

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Views and functions

  • 1.
  • 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
  • 10. The define statement… CREATE FUNCTION function_name(parameters) RETURNS(return_data type) { statements; }
  • 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//
  • 12. mysql> select fn_getvalue('amal'); Select fn_getvalue(‘amal’); //function call +---------------------+ | fn_getvalue('amal') | +---------------------+ | 8.5 | +---------------------+ 1 row in set (0.00 sec)
  • 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