SlideShare a Scribd company logo
My sql udf,views
NikhilDevS.B
nikhildevsb@gmail.com
www.facebook.com/nikhildevsb
TwitterProfile
www.linkedin.com/nikhildevsb
Typing speed: 24 wpm.
USER DEFINED FUNCTIONS AND VIEWS
IN MYSQL
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
User Defined functions (UDF)
• It is the SQL routines used to encapsulate program logic.
• A function is a set of codes written to perform certain
task.
• UDF cant call a Stored Procedure.
• Functions are compiled and executed at run time thus it
slower than stored procedure.
Syntax : creating a UDF
DELIMITER //
CREATE FUNCTION function_name (parameter datatype) RETURNS datatype
BEGIN
<statements>
............
............
END //
DELIMITER ;
Syntax : Calling a function
SELECT function_name(parameter);
E.g. :
DELIMITER //
CREATE FUNCTION fun_checkNum(int_x int) RETURNS varchar(20) NO SQL
BEGIN
DECLARE numtype varchar(25);
IF(int_x<0) THEN
SET numtype='Negative Number';
ELSE
SET numtype='Positive Number';
END IF;
RETURN numtype;
END //
DELIMITER;
//Calling Function and output :
mysql> SELECT fun_checkNum(-1);
+------------------+
| fun_checkNum(-1) |
+------------------+
| Negative Number |
+------------------+
PECULIARITIES OF USER DEFINED FUNCTIONS
• UDF are compiled and Executed at run time.
• UDF must return a value.
• UDF can only have input parameters.
• UDF can be called from stored procedure.
• A UDF can call another UDF.
• UDF allows only SELECT command in it, no other DML,DDL,DCL
commands.
Problem1:
Write a user defined function to return how many jobs applied by
a user.
DELIMITER //
CREATE FUNCTION fun_jobsApplied(getID int) RETURNS int READS SQL DATA
BEGIN
DECLARE count int;
SELECT int_jobs_applied into count FROM tbl_jobs where int_id=getID;
RETURN count;
END //
DELIMITER ;
SELECT fun_jobsApplied(1); //calling udf, it returns number of applied jobs
corresponding to id=1
Views in Mysql
Views
• In SQL, a VIEW is a virtual relation based on the result
set of a SELECT statement.
• A view can be considered as a stored query or a virtual table.
• 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.
• View Doesn't take any storage space.
Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
pk_int_emp_id vchr_emp_name vchr_company
1 James Dell
2 John Sony
3 Albert Hp
CREATE VIEW vw_tbl_employee AS
SELECT pk_int_emp_id , vchr_emp_name FROM tbl_employee;
SELECT * FROM vw_tbl_employee; //to see all content of view
E.g. VIEW
Creating a view for below table
pk_int_emp_id vchr_emp_name
1 James
2 John
3 Albert
Renaming Attributes in View
• Sometime, we might want to distinguish attributes by giving the
different name from names in table .
CREATE VIEW vw_tbl_employee (Employeeid,EmployeeName) AS
SELECT pk_int_emp_id , vchr_emp_name FROM tbl_employee;
SELECT * FROM vw_tbl_employee;
Employeeid EmployeeName
1 James
2 John
3 Albert
Insert values to view
We can insert value into view as same as that of table
INSERT INTO vw_tbl_employee (Employeename) VALUES
(‘Smith’);
Employeeid EmployeeName
1 James
2 John
3 Albert
4 Smith
Delete from view
DELETE FROM vw_tbl_employee WHERE Employeeid=1;
Employeeid EmployeeName
2 John
3 Albert
4 Smith
DROP VIEW
DROP VIEW viewname;
DROP VIEW vw_tbl_employee;
Thank you...
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

PDF
Exercise in alv
PPTX
Design patterns in web testing automation with WebDriver
ODP
Spec & Test Helper
PPTX
Javascript void
PDF
Creating a comp
PPTX
CUCUMBER - Making BDD Fun
PDF
Binding,context mapping,navigation exercise
PDF
Creating messages
Exercise in alv
Design patterns in web testing automation with WebDriver
Spec & Test Helper
Javascript void
Creating a comp
CUCUMBER - Making BDD Fun
Binding,context mapping,navigation exercise
Creating messages

Viewers also liked (10)

PDF
Part 13 function dan user defined function
PDF
Understand when to use user defined functions in sql server tech-republic
PPTX
PL/SQL User-Defined Functions in the Read World
PDF
R hive tutorial - apply functions and map reduce
PPTX
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
PPTX
SQL-on-Hadoop Tutorial
PDF
Hadoop Administration pdf
PPTX
Hadoop Hive Tutorial | Hive Fundamentals | Hive Architecture
PDF
Hive Quick Start Tutorial
Part 13 function dan user defined function
Understand when to use user defined functions in sql server tech-republic
PL/SQL User-Defined Functions in the Read World
R hive tutorial - apply functions and map reduce
U-SQL User-Defined Operators (UDOs) (SQLBits 2016)
SQL-on-Hadoop Tutorial
Hadoop Administration pdf
Hadoop Hive Tutorial | Hive Fundamentals | Hive Architecture
Hive Quick Start Tutorial
Ad

Similar to My sql udf,views (20)

PPTX
PPTX
Udf&views in sql...by thanveer melayi
PPT
PPT
Creating other schema objects
PPTX
Chapter 4 functions, views, indexing
PPT
Sql views
PPTX
Mysql creating stored function
PPT
PPT
PPT
PPTX
Web Developer make the most out of your Database !
PPT
chap 9 dbms.ppt
PPT
Creating Views - oracle database
PDF
Sql views, stored procedure, functions
PPT
Les12[1]Creating Views
PPT
lecture13.ppt
PPT
e computer notes - Creating views
PPTX
Testing Database Changes
Udf&views in sql...by thanveer melayi
Creating other schema objects
Chapter 4 functions, views, indexing
Sql views
Mysql creating stored function
Web Developer make the most out of your Database !
chap 9 dbms.ppt
Creating Views - oracle database
Sql views, stored procedure, functions
Les12[1]Creating Views
lecture13.ppt
e computer notes - Creating views
Testing Database Changes
Ad

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

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

My sql udf,views

  • 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. User Defined functions (UDF) • It is the SQL routines used to encapsulate program logic. • A function is a set of codes written to perform certain task.
  • 5. • UDF cant call a Stored Procedure. • Functions are compiled and executed at run time thus it slower than stored procedure.
  • 6. Syntax : creating a UDF DELIMITER // CREATE FUNCTION function_name (parameter datatype) RETURNS datatype BEGIN <statements> ............ ............ END // DELIMITER ; Syntax : Calling a function SELECT function_name(parameter);
  • 7. E.g. : DELIMITER // CREATE FUNCTION fun_checkNum(int_x int) RETURNS varchar(20) NO SQL BEGIN DECLARE numtype varchar(25); IF(int_x<0) THEN SET numtype='Negative Number'; ELSE SET numtype='Positive Number'; END IF; RETURN numtype; END // DELIMITER; //Calling Function and output : mysql> SELECT fun_checkNum(-1); +------------------+ | fun_checkNum(-1) | +------------------+ | Negative Number | +------------------+
  • 8. PECULIARITIES OF USER DEFINED FUNCTIONS • UDF are compiled and Executed at run time. • UDF must return a value. • UDF can only have input parameters. • UDF can be called from stored procedure. • A UDF can call another UDF. • UDF allows only SELECT command in it, no other DML,DDL,DCL commands.
  • 9. Problem1: Write a user defined function to return how many jobs applied by a user. DELIMITER // CREATE FUNCTION fun_jobsApplied(getID int) RETURNS int READS SQL DATA BEGIN DECLARE count int; SELECT int_jobs_applied into count FROM tbl_jobs where int_id=getID; RETURN count; END // DELIMITER ; SELECT fun_jobsApplied(1); //calling udf, it returns number of applied jobs corresponding to id=1
  • 11. Views • In SQL, a VIEW is a virtual relation based on the result set of a SELECT statement. • A view can be considered as a stored query or a virtual table. • 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. • View Doesn't take any storage space.
  • 12. Syntax: CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition;
  • 13. pk_int_emp_id vchr_emp_name vchr_company 1 James Dell 2 John Sony 3 Albert Hp CREATE VIEW vw_tbl_employee AS SELECT pk_int_emp_id , vchr_emp_name FROM tbl_employee; SELECT * FROM vw_tbl_employee; //to see all content of view E.g. VIEW Creating a view for below table pk_int_emp_id vchr_emp_name 1 James 2 John 3 Albert
  • 14. Renaming Attributes in View • Sometime, we might want to distinguish attributes by giving the different name from names in table . CREATE VIEW vw_tbl_employee (Employeeid,EmployeeName) AS SELECT pk_int_emp_id , vchr_emp_name FROM tbl_employee; SELECT * FROM vw_tbl_employee; Employeeid EmployeeName 1 James 2 John 3 Albert
  • 15. Insert values to view We can insert value into view as same as that of table INSERT INTO vw_tbl_employee (Employeename) VALUES (‘Smith’); Employeeid EmployeeName 1 James 2 John 3 Albert 4 Smith
  • 16. Delete from view DELETE FROM vw_tbl_employee WHERE Employeeid=1; Employeeid EmployeeName 2 John 3 Albert 4 Smith DROP VIEW DROP VIEW viewname; DROP VIEW vw_tbl_employee;
  • 18. 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.
  • 19. 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
  • 20. 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