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

My sql udf,views

  • 2.
  • 3.
    Disclaimer: This presentationis 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 cantcall a Stored Procedure. • Functions are compiled and executed at run time thus it slower than stored procedure.
  • 6.
    Syntax : creatinga 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 // CREATEFUNCTION 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 USERDEFINED 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 userdefined 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
  • 10.
  • 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_nameAS SELECT column_name(s) FROM table_name WHERE condition;
  • 13.
    pk_int_emp_id vchr_emp_name vchr_company 1James 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 inView • 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 toview 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 DELETEFROM vw_tbl_employee WHERE Employeeid=1; Employeeid EmployeeName 2 John 3 Albert 4 Smith DROP VIEW DROP VIEW viewname; DROP VIEW vw_tbl_employee;
  • 17.
  • 18.
    Want to learnmore 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 (BigBazar 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