Views Lecture 15 Robb T. Koether Hampden-Sydney College Fri, Feb 16, 2018 Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 1 / 28
1 Views 2 Modifying the Base Tables 3 Updating Views 4 Using Triggers to Update Views Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 2 / 28
Outline 1 Views 2 Modifying the Base Tables 3 Updating Views 4 Using Triggers to Update Views Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 3 / 28
Views A view is a virtual table constructed from physical tables. A view can be used as a table (select, insert, delete, update), but it does not exist as a physical table in the database. It is not the same as a temporary table, which does exist in the database. A query on the view must be interpreted as a query on the base tables from which the query is constructed. At times, this can lead to significant complications. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 4 / 28
Views Views are useful for Constructed tables, such as joins, that are used frequently. For security, to hide information that a user is not authorized to see. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 5 / 28
Creating a View Creating a View CREATE VIEW view_name AS select_statement; The view is defined to be those tuples that are returned by the SELECT statement. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 6 / 28
Creating a View Creating a View CREATE VIEW emp_w_dep AS SELECT fname, lname, ssn, salary, dep_name FROM employees WHERE ssn IN (SELECT ssn FROM dependents) For example, we might want to create a view of those employees who have dependents. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 7 / 28
Creating a View Creating a View SELECT * FROM emp_w_dep; +----------+---------+-----------+-----------+------------+ fname lname ssn salary bdate +----------+---------+-----------+-----------+------------+ James Green 246813579 100000.00 1974-02-15 Jennifer Wallace 321549876 50000.00 1985-12-02 Frank Gilbert 369147258 30000.00 1966-08-21 Joyce English 456789012 25000.00 1983-05-07 Richard Johnson 531978642 35000.00 1955-03-17 John Kohler 789012345 40000.00 1966-11-24 Raymond Jones 963418527 80000.00 1974-08-30 +----------+---------+-----------+-----------+------------+ Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 8 / 28
Views as Tables Views as Tables SHOW TABLES; +-------------------+ Tables_in_company +-------------------+ departments dependents employees projects emp_w_dep works_on +-------------------+ Views are included in the list of tables. We can use SHOW CREATE VIEW to display he details of a view. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 9 / 28
Selecting From a View Selecting From a View SELECT fname, lname FROM emp_w_dep WHERE salary > 50000; +---------+-------+ fname lname +---------+-------+ James Green Raymond Jones +---------+-------+ There is never a problem with selecting from a view. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 10 / 28
Selecting From a View Selecting From a View SELECT fname, lname, dep_name FROM emp_w_dep NATURAL JOIN dependents; +----------+---------+----------+ fname lname dep_name +----------+---------+----------+ James Green Debbie James Green Sarah Jennifer Wallace Fred Jennifer Wallace Jimmy Jennifer Wallace Susie Frank Gilbert Laura Frank Gilbert Rachel Joyce English Bobby Richard Johnson Patty John Kohler Sharon Raymond Jones Donna +----------+---------+----------+ There is never a problem with selecting from a view. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 11 / 28
A View with Two Base Tables A View with Two Base Tables CREATE VIEW emp_and_dep AS SELECT fname, lname, ssn, salary, dep_name FROM employees NATURAL JOIN dependents; We can create a view from more than one base table joined together. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 12 / 28
A View with Two Base Tables A View with Two Base Tables CREATE VIEW emp_and_dep AS SELECT fname, lname, ssn, salary, dep_name FROM employees NATURAL JOIN dependents; We can create a view from more than one base table joined together. Other than the particular fields included, how would the contents of this view differ from the contents of emp_w_dep? Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 12 / 28
A View with Two Base Tables A View with Two Base Tables SELECT * FROM emp_and_dep; +----------+---------+-----------+-----------+----------+ fname lname ssn salary dep_name +----------+---------+-----------+-----------+----------+ James Green 246813579 100000.00 Debbie James Green 246813579 100000.00 Sarah Jennifer Wallace 321549876 50000.00 Fred Jennifer Wallace 321549876 50000.00 Jimmy Jennifer Wallace 321549876 50000.00 Susie Frank Gilbert 369147258 30000.00 Laura Frank Gilbert 369147258 30000.00 Rachel Joyce English 456789012 25000.00 Bobby Richard Johnson 531978642 35000.00 Patty John Kohler 789012345 40000.00 Sharon Raymond Jones 963418527 80000.00 Donna +----------+---------+-----------+-----------+----------+ Each employee appears once for each dependent. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 13 / 28
Outline 1 Views 2 Modifying the Base Tables 3 Updating Views 4 Using Triggers to Update Views Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 14 / 28
Selecting From a View Modifying the Base Tables UPDATE employees SET fname = Michael WHERE fname = Frank ; SELECT * FROM emp_w_dep; +----------+---------+-----------+-----------+------------+ fname lname ssn salary bdate +----------+---------+-----------+-----------+------------+ James Green 246813579 100000.00 1974-02-15 Jennifer Wallace 321549876 50000.00 1985-12-02 Michael Gilbert 369147258 30000.00 1966-08-21 Joyce English 456789012 25000.00 1983-05-07 Richard Johnson 531978642 35000.00 1955-03-17 John Kohler 789012345 40000.00 1966-11-24 Raymond Jones 963418527 80000.00 1974-08-30 +----------+---------+-----------+-----------+------------+ If the base tables are changed, then the view will change automatically. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 15 / 28
Selecting From a View Modifying the Base Tables UPDATE dependents SET dep_name = "Mary" WHERE dep_name = "Patty"; SELECT * FROM emp_and_dep; +----------+---------+-----------+-----------+----------+ fname lname ssn salary dep_name +----------+---------+-----------+-----------+----------+ James Green 246813579 100000.00 Debbie James Green 246813579 100000.00 Sarah Jennifer Wallace 321549876 50000.00 Fred Jennifer Wallace 321549876 50000.00 Jimmy Jennifer Wallace 321549876 50000.00 Susie Frank Gilbert 369147258 30000.00 Laura Frank Gilbert 369147258 30000.00 Rachel Joyce English 456789012 25000.00 Bobby Richard Johnson 531978642 35000.00 Mary John Kohler 789012345 40000.00 Sharon Raymond Jones 963418527 80000.00 Donna +----------+---------+-----------+-----------+----------+ If the base tables are changed, then the view will change automatically. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 16 / 28
Outline 1 Views 2 Modifying the Base Tables 3 Updating Views 4 Using Triggers to Update Views Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 17 / 28
Updating Views What happens to the base tables if we modify the view? In the previous example, What happens if we insert the tuple ( Brian, Jacobson, 888776666, 40000.00, 1990-04-19 ) into emp_w_dep? What happens if we update that tuple? What happens if we delete that tuple? Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 18 / 28
Updating Views Updating a View INSERT INTO emp_w_dep VALUES( Brian, Jacobson, 888776666, 40000.00, 1990-04-19 ); Query OK, 1 row affected, 1 warning (0.00 sec) SHOW WARNINGS; +---------+------+--------------------------------------------------------------------------------- Level Code Message +---------+------+--------------------------------------------------------------------------------- Warning 1423 Field of view company.emp_w_dep underlying table doesn t have a default value +---------+------+--------------------------------------------------------------------------------- SELECT * FROM employees WHERE ssn = 888776666 ; +-------+----------+-----------+------------+------+----------+------+ fname lname ssn bdate sex salary dept +-------+----------+-----------+------------+------+----------+------+ Brian Jacobson 888776666 1990-04-19 NULL 40000.00 0 +-------+----------+-----------+------------+------+----------+------+ Also, update and delete the tuple. In general insertions, deletions, and updates are permitted provided they affect only one base table. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 19 / 28
Updating Views The emp_w_dep view is based on only one table (or was it?). What would happen if we added or deleted dependents? Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 20 / 28
Updating Views Inserting Into a Base Table INSERT INTO dependents VALUES( 123456789, Tiffany, F, 2013-02-18 ); SELECT * FROM emp_w_dep; +----------+---------+-----------+-----------+------------+ fname lname ssn salary bdate +----------+---------+-----------+-----------+------------+ Alice Smith 123456789 35000.00 1968-05-22 James Green 246813579 100000.00 1974-02-15 Jennifer Wallace 321549876 50000.00 1985-12-02 Michael Gilbert 369147258 30000.00 1966-08-21 Joyce English 456789012 25000.00 1983-05-07 Richard Johnson 531978642 35000.00 1955-03-17 John Kohler 789012345 40000.00 1966-11-24 Raymond Jones 963418527 80000.00 1974-08-30 +----------+---------+-----------+-----------+------------+ Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 21 / 28
Updating Views Deleting From a Base Table DELETE FROM dependents WHERE ssn = 123456789 ; SELECT * FROM emp_w_dep; +----------+---------+-----------+-----------+------------+ fname lname ssn salary bdate +----------+---------+-----------+-----------+------------+ James Green 246813579 100000.00 1974-02-15 Jennifer Wallace 321549876 50000.00 1985-12-02 Michael Gilbert 369147258 30000.00 1966-08-21 Joyce English 456789012 25000.00 1983-05-07 Richard Johnson 531978642 35000.00 1955-03-17 John Kohler 789012345 40000.00 1966-11-24 Raymond Jones 963418527 80000.00 1974-08-30 +----------+---------+-----------+-----------+------------+ Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 22 / 28
Views and Joins Updating views is more difficult when the view is based on more than one table. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 23 / 28
Views and Joins Creating a View CREATE VIEW emp_hrs AS SELECT fname, lname, proj_name, hours FROM employees NATURAL JOIN works_on; NATURAL JOIN projects This view is based on the join of three tables. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 24 / 28
Views and Joins Creating a View CREATE VIEW emp_hrs AS SELECT fname, lname, proj_name, hours FROM employees NATURAL JOIN works_on; NATURAL JOIN projects This view is based on the join of three tables. Uh-oh. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 24 / 28
Views and Joins Creating a View SELECT * FROM emp_hrs; +----------+---------+-----------------+-------+ fname lname proj_name hours +----------+---------+-----------------+-------+ Alice Smith Payroll 40.0 John Kohler Payroll 40.0 Raymond Jones Payroll 20.0 Jennifer Wallace Investment 40.0 Ernest Roth Investment 40.0 Raymond Jones Investment 20.0 Barbara Brown Clothing 20.0 Michael Gilbert Clothing 40.0 Joyce English Clothing 20.0 Amy Ford Clothing 30.0 Barbara Brown Office Supplies 10.0 Joyce English Office Supplies 20.0 Amy Ford Office Supplies 10.0 Barbara Brown Housewares 10.0 Richard Johnson Housewares 40.0 +----------+---------+-----------------+-------+ Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 25 / 28
Inserting Into a View Inserting Into a View INSERT INTO emp_hrs VALUES( Jason, Simpson, Clothing, 25.0); ERROR 1394 (HY000): Can not insert into join view company.emp_hrs without fields list INSERT INTO emp_hrs(fname, lname, proj_name, hours) VALUES( Jason, Simpson, Clothing, 25.0); ERROR 1393 (HY000): Can not modify more than one base table through a join view company.emp_hrs Try to insert into the view. Try updating or deleting. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 26 / 28
Outline 1 Views 2 Modifying the Base Tables 3 Updating Views 4 Using Triggers to Update Views Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 27 / 28
Inserting Into a View Inserting Into a View CREATE TRIGGER add_emp_dep AFTER INSERT ON emp_w_dep FOR EACH ROW INSERT INTO dependents VALUES(NEW.ssn,, null, null); ERROR 1347 (HY000): company.emp_w_dep is not BASE TABLE Normally, modifying a view would have been handled by triggers that modify the base tables in the appropriate way. Unfortunately, MySQL does not support triggers on views. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, 2018 28 / 28