+ All Categories
Home > Documents > My sql udf,views

My sql udf,views

Date post: 12-Jul-2015
Category:
Upload: baabtracom-no-1-supplier-of-quality-freshers
View: 457 times
Download: 0 times
Share this document with a friend
20
Transcript
Page 1: My sql udf,views
Page 2: My sql udf,views

Nikhil Dev S.B

[email protected]

www.facebook.com/nikhildevsb

Twitter Profile

www.linkedin.com/nikhildevsb

Typing speed : 24 wpm.

USER DEFINED FUNCTIONS AND VIEWS IN MYSQL

Page 3: My sql udf,views

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: My sql udf,views

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.

Page 5: My sql udf,views

• UDF cant call a Stored Procedure.

• Functions are compiled and executed at run time thus it

slower than stored procedure.

Page 6: My sql udf,views

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

Page 7: My sql udf,views

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 |

+------------------+

Page 8: My sql udf,views

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.

Page 9: My sql udf,views

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

Page 10: My sql udf,views

Views in Mysql

Page 11: My sql udf,views

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.

Page 12: My sql udf,views

Syntax:

CREATE VIEW view_name AS

SELECT column_name(s)

FROM table_name

WHERE condition;

Page 13: My sql udf,views

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

Page 14: My sql udf,views

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

Page 15: My sql udf,views

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

Page 16: My sql udf,views

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;

Page 17: My sql udf,views

Thank you...

Page 18: My sql udf,views

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 19: My sql udf,views

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 20: My sql udf,views

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


Recommended