{"id":271,"date":"2018-07-13T07:14:19","date_gmt":"2018-07-13T07:14:19","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=271"},"modified":"2018-08-13T05:46:51","modified_gmt":"2018-08-13T05:46:51","slug":"triggers-in-mysql-contd","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/triggers-in-mysql-contd\/","title":{"rendered":"Triggers in MySQL (contd.)"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Database Triggers are the database objects which reside in system catalog. The triggers are special Type\u00a0 \u00a0of procedures which can be called implicitly.<\/p>\r\n<p style=\"text-align: justify\">\u2022 Each trigger is associated with a table which can be activated on any DML statement like (Insert ,update or delete).<\/p>\r\n&nbsp;\r\n\r\nImplementation of Triggers:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the previous module, we have already seen the introduction and partial implementation of Triggers. Hence , let us continue with remaining types of Triggers<\/p>\r\n&nbsp;\r\n\r\nTo have a quick recap of tables we have used :\r\n\r\n&nbsp;\r\n\r\nEmp Table\r\n\r\nEmp_log table\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-274 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128.png\" alt=\"\" width=\"637\" height=\"333\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">You will require one LOG table to see the effect of Trigger execution. Structure of LOG table is as follows: (empid, name , salary , action, upd_date)<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-275 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129.png\" alt=\"\" width=\"666\" height=\"358\" \/>\r\n\r\n&nbsp;\r\n\r\nExample of <strong>Before Delete Trigger<\/strong>\r\n\r\n&nbsp;\r\n\r\nUse \u2018test\u2019;\r\n\r\nCreate trigger emp_Bdel before delete on emp for each row\r\n\r\nBegin\r\n\r\nInsert into emp_log values(old.empid, old.name, old.salary, \u2018Delete\u2019 , now(),user());\r\n\r\nEnd;\r\n\r\n&nbsp;\r\n\r\nCompile and Run the Trigger\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-276 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130.png\" alt=\"\" width=\"642\" height=\"370\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After writing the code for the above trigger you can test it by deleting a row from emp table and you will notice the corresponding effect in emp_log table.<\/p>\r\n&nbsp;\r\n\r\nSo, First view records in emp_log\r\n\r\n&nbsp;\r\n\r\nSelect * from emp_log;\r\n\r\nThen :\r\n\r\nDelete from emp where emp_id=7;\r\n\r\n&nbsp;\r\n\r\nAnd now, again see the corresponding effect in emp_log table.\r\n\r\n&nbsp;\r\n\r\nIn all the examples previously we saw, After Triggers first and then Before Triggers\r\n\r\nBut , Delete is the exception and there is a reason behind it.\r\n\r\nIn After Delete, there\u2019s is nothing much to maintain. The only thing you can maintain is User Log , i.e.\r\n\r\nwho deleted the record and what was the old value.\r\n\r\n&nbsp;\r\n\r\n<strong>After Delete Trigger<\/strong>\r\n\r\n&nbsp;\r\n\r\nExample of <strong>After Delete Trigger<\/strong>\r\n\r\n&nbsp;\r\n\r\nUse \u2018test\u2019;\r\n\r\nCreate trigger emp_Adel after delete on emp for each row\r\n\r\nBegin\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Insert into emp_log1 values(user(), concat(\u2018Delete employee Salary , Name : \u2018, old.name, \u2018OLD salary :\u2019, old.salary) );<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">End;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nCompile and Run the Trigger\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-277 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131.png\" alt=\"\" width=\"592\" height=\"205\" \/>\r\n\r\n&nbsp;\r\n\r\nYou can test the trigger by deleting a row and check the effect in emp_log1 table.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After writing the code for the above trigger you can test it by deleting a row from emp table and you will notice the corresponding effect in emp_log1 and emp_log both tables.<\/p>\r\n&nbsp;\r\n\r\nSo, First view records in emp_log1\r\n\r\n&nbsp;\r\n\r\nSelect * from emp_log1;\r\n\r\nThen :\r\n\r\nDelete from emp where emp_id=6;\r\n\r\nAnd now, again see the corresponding effect in emp_log1 and emp_log table.\r\n\r\n&nbsp;\r\n\r\n<strong>Restrictions on Triggers<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 Return statement is not permitted in triggers\r\n\r\n\u2022 Trigger cache does not detect metadata of the objects\r\n\r\n\u2022 Triggers cannot be activated by referential integrities(foreign key actions)\r\n\r\n\u2022 Triggers do not work for system tables\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">To delete the trigger<\/strong>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u2022 Drop trigger &lt;triggername&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">To view triggers<\/strong>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u2022 Show triggers<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-278 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132.png\" alt=\"\" width=\"614\" height=\"623\" \/>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Views in MySQL<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">What are views<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Views are the logical tables which do not exist physically<\/p>\r\n<p style=\"text-align: justify\">\u2022 They are type of queries which are stored and can be referred to at a later stage<\/p>\r\n<p style=\"text-align: justify\">\u2022 Views will not store any data but just display the data from the existing tables<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0 Some views are updateable, for views to be updateable there should be 1:1 relationship between view\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0and underlying table(s)<\/p>\r\n<p style=\"text-align: justify\">\u2022 The use of complex queries might also make view non-updateable<\/p>\r\n<p style=\"text-align: justify\"><\/p>\r\n<strong>Advantages &amp; Limitations of Views<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 Advantages\r\n\r\n\u2022 Simplification of Complex Queries\r\n\r\n\u2022 Selective columns can be displayed\/hidden from the underlying table\r\n\r\n\u2022 Security\r\n\r\n\u2022 Calculated\/Generated Columns\r\n\r\n\u2022 Backward Compatibility\r\n\r\n\u2022 No Data Storage required\r\n\r\n\u2022 Limitation\r\n\r\n\u2022 Performance Considerations\r\n\r\n\u2022 Dependency on tables\r\n\r\n\u2022 Syntax\r\n\r\n&nbsp;\r\n\r\nCreate [OR REPLACE]\r\n\r\n[ALGORITHM = {UNDIFENED |MERGER| TEMPTABLE} ]\r\n\r\nDEFINER = {USER | CURRENT_USER}]\r\n\r\n[SQL SECURITY { DEFINER | INVOKER }]\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">VIEW view_name [(column_list)]\u00a0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">AS select_statement\u00a0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">[WITH [CASCADED |LOCAL] CHECK OPTION]<\/span>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-279 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133.png\" alt=\"\" width=\"502\" height=\"308\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nYou can observe ,the Create View command in the navigator pane.\r\n\r\n&nbsp;\r\n\r\n<strong>Creating a View :<\/strong>\r\n\r\n<strong>Create view \u2018new_view\u2018 as<\/strong>\r\n\r\n<strong>\u00a0Select name,designation from emp;<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-280 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134.png\" alt=\"\" width=\"452\" height=\"241\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">It can be observed that on applying the view, how the compiler has added the algorithm and aliases.<\/p>\r\nTo test ,\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\">Select * from new_view;<\/p>\r\n<p style=\"text-align: center\">Update new_view set designation = \u2018\u00c7lerk\u2019 where name = \u2018raj\u2019<\/p>\r\nAnd, observe the corresponding effects in the respective view and table.\r\n\r\n&nbsp;\r\n\r\n<strong>Views with aggregate functions<\/strong>\r\n\r\n&nbsp;\r\n\r\nCreate view \u2018Maxsal\u2019 AS\r\n\r\nSelect Designation , max(salary) from emp\r\n\r\nGroup by designation;\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-281 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135.png\" alt=\"\" width=\"588\" height=\"342\" \/>\r\n\r\n&nbsp;\r\n\r\nTo Test:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">It displays you, designation wise maximum salary. If you also want to view the name of the employee with maximum salary, you can add name in the query.<\/p>\r\n&nbsp;\r\n\r\nBut, you can\u2019t insert in those types of views.\r\n\r\n&nbsp;\r\n\r\n<strong>Updateable Views<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 A view is updateable only if\r\n\r\n\u2022 The select statement refers to only one table\r\n\r\n\u2022 The select statement is not using any aggregate functions\r\n\r\n\u2022 The select statement has not used Distinct\r\n\r\n\u2022 The select statement is not referring to other views which are read-only\r\n\r\n&nbsp;\r\n\r\n<strong>Views from Multiple Tables<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Views can be created from multiple tables also.<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Create View \u2018empdept\u2019 As<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Select e.empid,e.name, e.salary , e.designation,<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">d.deptid, d.dname , d.location<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">from emp e, dept d<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">where e.deptid = d.deptid;<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-282 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136.png\" alt=\"\" width=\"594\" height=\"372\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>To view :<\/strong>\r\n\r\n&nbsp;\r\n\r\nSelect * from empdept;\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-283 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137.png\" alt=\"\" width=\"559\" height=\"582\" \/>\r\n\r\n<\/div>\r\n<strong>Additional Reading:<\/strong>\r\n1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers\r\n2) MySQL documentation : dev.mysql.com\/doc\/","rendered":"<div>\n<p>&nbsp;<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Database Triggers are the database objects which reside in system catalog. The triggers are special Type\u00a0 \u00a0of procedures which can be called implicitly.<\/p>\n<p style=\"text-align: justify\">\u2022 Each trigger is associated with a table which can be activated on any DML statement like (Insert ,update or delete).<\/p>\n<p>&nbsp;<\/p>\n<p>Implementation of Triggers:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the previous module, we have already seen the introduction and partial implementation of Triggers. Hence , let us continue with remaining types of Triggers<\/p>\n<p>&nbsp;<\/p>\n<p>To have a quick recap of tables we have used :<\/p>\n<p>&nbsp;<\/p>\n<p>Emp Table<\/p>\n<p>Emp_log table<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-274 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128.png\" alt=\"\" width=\"637\" height=\"333\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128.png 637w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128-300x157.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128-225x118.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-128-350x183.png 350w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">You will require one LOG table to see the effect of Trigger execution. Structure of LOG table is as follows: (empid, name , salary , action, upd_date)<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-275 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129.png\" alt=\"\" width=\"666\" height=\"358\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129.png 666w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129-300x161.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129-225x121.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-129-350x188.png 350w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Example of <strong>Before Delete Trigger<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Use \u2018test\u2019;<\/p>\n<p>Create trigger emp_Bdel before delete on emp for each row<\/p>\n<p>Begin<\/p>\n<p>Insert into emp_log values(old.empid, old.name, old.salary, \u2018Delete\u2019 , now(),user());<\/p>\n<p>End;<\/p>\n<p>&nbsp;<\/p>\n<p>Compile and Run the Trigger<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-276 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130.png\" alt=\"\" width=\"642\" height=\"370\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130.png 642w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130-300x173.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130-225x130.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-130-350x202.png 350w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After writing the code for the above trigger you can test it by deleting a row from emp table and you will notice the corresponding effect in emp_log table.<\/p>\n<p>&nbsp;<\/p>\n<p>So, First view records in emp_log<\/p>\n<p>&nbsp;<\/p>\n<p>Select * from emp_log;<\/p>\n<p>Then :<\/p>\n<p>Delete from emp where emp_id=7;<\/p>\n<p>&nbsp;<\/p>\n<p>And now, again see the corresponding effect in emp_log table.<\/p>\n<p>&nbsp;<\/p>\n<p>In all the examples previously we saw, After Triggers first and then Before Triggers<\/p>\n<p>But , Delete is the exception and there is a reason behind it.<\/p>\n<p>In After Delete, there\u2019s is nothing much to maintain. The only thing you can maintain is User Log , i.e.<\/p>\n<p>who deleted the record and what was the old value.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>After Delete Trigger<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Example of <strong>After Delete Trigger<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Use \u2018test\u2019;<\/p>\n<p>Create trigger emp_Adel after delete on emp for each row<\/p>\n<p>Begin<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Insert into emp_log1 values(user(), concat(\u2018Delete employee Salary , Name : \u2018, old.name, \u2018OLD salary :\u2019, old.salary) );<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">End;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Compile and Run the Trigger<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-277 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131.png\" alt=\"\" width=\"592\" height=\"205\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131.png 592w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131-300x104.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131-225x78.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-131-350x121.png 350w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You can test the trigger by deleting a row and check the effect in emp_log1 table.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After writing the code for the above trigger you can test it by deleting a row from emp table and you will notice the corresponding effect in emp_log1 and emp_log both tables.<\/p>\n<p>&nbsp;<\/p>\n<p>So, First view records in emp_log1<\/p>\n<p>&nbsp;<\/p>\n<p>Select * from emp_log1;<\/p>\n<p>Then :<\/p>\n<p>Delete from emp where emp_id=6;<\/p>\n<p>And now, again see the corresponding effect in emp_log1 and emp_log table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Restrictions on Triggers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Return statement is not permitted in triggers<\/p>\n<p>\u2022 Trigger cache does not detect metadata of the objects<\/p>\n<p>\u2022 Triggers cannot be activated by referential integrities(foreign key actions)<\/p>\n<p>\u2022 Triggers do not work for system tables<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">To delete the trigger<\/strong><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u2022 Drop trigger &lt;triggername&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u2022\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">To view triggers<\/strong><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u2022 Show triggers<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-278 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132.png\" alt=\"\" width=\"614\" height=\"623\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132.png 614w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132-296x300.png 296w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132-65x66.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132-225x228.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-132-350x355.png 350w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Views in MySQL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">What are views<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Views are the logical tables which do not exist physically<\/p>\n<p style=\"text-align: justify\">\u2022 They are type of queries which are stored and can be referred to at a later stage<\/p>\n<p style=\"text-align: justify\">\u2022 Views will not store any data but just display the data from the existing tables<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0 Some views are updateable, for views to be updateable there should be 1:1 relationship between view\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0and underlying table(s)<\/p>\n<p style=\"text-align: justify\">\u2022 The use of complex queries might also make view non-updateable<\/p>\n<p style=\"text-align: justify\">\n<p><strong>Advantages &amp; Limitations of Views<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Advantages<\/p>\n<p>\u2022 Simplification of Complex Queries<\/p>\n<p>\u2022 Selective columns can be displayed\/hidden from the underlying table<\/p>\n<p>\u2022 Security<\/p>\n<p>\u2022 Calculated\/Generated Columns<\/p>\n<p>\u2022 Backward Compatibility<\/p>\n<p>\u2022 No Data Storage required<\/p>\n<p>\u2022 Limitation<\/p>\n<p>\u2022 Performance Considerations<\/p>\n<p>\u2022 Dependency on tables<\/p>\n<p>\u2022 Syntax<\/p>\n<p>&nbsp;<\/p>\n<p>Create [OR REPLACE]<\/p>\n<p>[ALGORITHM = {UNDIFENED |MERGER| TEMPTABLE} ]<\/p>\n<p>DEFINER = {USER | CURRENT_USER}]<\/p>\n<p>[SQL SECURITY { DEFINER | INVOKER }]<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">VIEW view_name [(column_list)]\u00a0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">AS select_statement\u00a0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">[WITH [CASCADED |LOCAL] CHECK OPTION]<\/span><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-279 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133.png\" alt=\"\" width=\"502\" height=\"308\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133.png 502w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133-300x184.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133-225x138.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-133-350x215.png 350w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>You can observe ,the Create View command in the navigator pane.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Creating a View :<\/strong><\/p>\n<p><strong>Create view \u2018new_view\u2018 as<\/strong><\/p>\n<p><strong>\u00a0Select name,designation from emp;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-280 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134.png\" alt=\"\" width=\"452\" height=\"241\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134.png 452w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134-300x160.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134-225x120.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-134-350x187.png 350w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">It can be observed that on applying the view, how the compiler has added the algorithm and aliases.<\/p>\n<p>To test ,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\">Select * from new_view;<\/p>\n<p style=\"text-align: center\">Update new_view set designation = \u2018\u00c7lerk\u2019 where name = \u2018raj\u2019<\/p>\n<p>And, observe the corresponding effects in the respective view and table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Views with aggregate functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Create view \u2018Maxsal\u2019 AS<\/p>\n<p>Select Designation , max(salary) from emp<\/p>\n<p>Group by designation;<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-281 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135.png\" alt=\"\" width=\"588\" height=\"342\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135.png 588w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135-300x174.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135-225x131.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-135-350x204.png 350w\" sizes=\"auto, (max-width: 588px) 100vw, 588px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>To Test:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">It displays you, designation wise maximum salary. If you also want to view the name of the employee with maximum salary, you can add name in the query.<\/p>\n<p>&nbsp;<\/p>\n<p>But, you can\u2019t insert in those types of views.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Updateable Views<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 A view is updateable only if<\/p>\n<p>\u2022 The select statement refers to only one table<\/p>\n<p>\u2022 The select statement is not using any aggregate functions<\/p>\n<p>\u2022 The select statement has not used Distinct<\/p>\n<p>\u2022 The select statement is not referring to other views which are read-only<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Views from Multiple Tables<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Views can be created from multiple tables also.<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Create View \u2018empdept\u2019 As<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Select e.empid,e.name, e.salary , e.designation,<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">d.deptid, d.dname , d.location<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">from emp e, dept d<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">where e.deptid = d.deptid;<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-282 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136.png\" alt=\"\" width=\"594\" height=\"372\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136.png 594w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136-300x188.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136-225x141.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-136-350x219.png 350w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>To view :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Select * from empdept;<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-283 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137.png\" alt=\"\" width=\"559\" height=\"582\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137.png 559w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137-288x300.png 288w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137-65x68.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137-225x234.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-137-350x364.png 350w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/p>\n<\/div>\n<p><strong>Additional Reading:<\/strong><br \/>\n1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<br \/>\n2) MySQL documentation : dev.mysql.com\/doc\/<\/p>\n","protected":false},"author":3,"menu_order":29,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["miss-bhumika-shah"],"pb_section_license":""},"chapter-type":[],"contributor":[60],"license":[],"class_list":["post-271","chapter","type-chapter","status-publish","hentry","contributor-miss-bhumika-shah"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":4,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/271\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/271\/revisions\/435"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/271\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=271"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=271"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}