Introduction to mysql part 4


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to mysql part 4

  1. 1. Introduction To DBMS and MySQL UDF, Views, Indexing Week 6 – Day2
  2. 2. User Defined functions [ UDF ]
  3. 3. User Defined functions UDF • A function is a subprogram written to perform certain computations • It can be used with sql select queries same like we use any other scalar or aggregate functions that we discussed earlier (eg: sum(), Avg() etc)
  4. 4. Example • Write a function that returns Holiday or working day depending upon the day of week. If it is Saturday or Sunday it should display holiday or else working day DELIMITER // CREATE FUNCTION fun_getDAy(x datetime) RETURNS VARCHAR(15) BEGIN DECLARE dayname varchar(15) DEFAULT ‘HOLIDAY’; IF(WEEKDAY(x)<5) THEN SET dayname=‘WORKING DAY’; END IF; RETURN dayname; END // DELIMITER ;
  5. 5. Difference Between SP and UDF • Functions are compiled and executed at run time thus pretty slower than stored procedure • UDF can’t perform DML operations like insert, update, delete etc but the same can do from SP • UDF cant call a Stored Procedure or another UDF inside a UDF • UDF must return a value • Procedueres are compiled and stored . So at run time no need of compilation required which makes pretty much faster • SP can perform all DDL,DML and DCL operations • Stored procedure can call a UDF or another stored procedure inside it • Stored procedure may or may not return any values
  6. 6. Views
  7. 7. Views • A view is a customized representation of data from one or more tables. The table that the view is referencing are known as base tables. • A view can be considered as a stored query or a virtual table • View Doesn't take any storage space
  8. 8. Example • Creating a view – Create View view_teacher as select * from tbl_teacher where teacher_name like „t%‟ • Using a View –We can query against a view as we do in a table –Select * from view_teacher where teacher_id=1
  9. 9. Advantages of View • Data Security – No need to give permission on the table. infact a view can be created, having only selected number of columns in its definition. So user will only be able to see those columns • Simplicity • A very complicated query can be saved as a view definition. When needed can be called by its view name • Removes Dependency • Can be very helpful to remove the dependency from the underlying tables. Suppose a view is created joining several tables. After some time, there are some changes on the table, so only definition of view can be
  10. 10. Indexing
  11. 11. Indexing • A database index is a data structure that improves the speed of operations in a table. • Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. • INSERT and UPDATE statements take more time on tables having indexes where as SELECT statements become fast on those tables. The reason is that while doing insert
  12. 12. Indexing • Syntax – CREATE INDEX index_name ON table_name ( column1, column2,...); • Example – CREATE INDEX AUTHOR_INDEX ON tutorials_tbl (tutorial_author)
  13. 13. Points to be noted • In Mysql whenever you create a table with primary key, the same column will be indexed • So you don‟t have to explicitly create an index for primary key column • Simply below also a way of indexing a table – ALTER TABLE testalter_tbl ADD PRIMARY
  14. 14. Questions? “A good question deserve a good grade…”
  15. 15. End of day