Date post: | 12-Jul-2015 |
Category: |
Documents |
Upload: | baabtracom-no-1-supplier-of-quality-freshers |
View: | 457 times |
Download: | 0 times |
Nikhil Dev S.B
www.facebook.com/nikhildevsb
Twitter Profile
www.linkedin.com/nikhildevsb
Typing speed : 24 wpm.
USER DEFINED FUNCTIONS AND VIEWS IN MYSQL
Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial 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
BEGINDECLARE 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 ASSELECT 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) ASSELECT 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 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