ViewsLecture 15Section 5.3
Robb T. Koether
Hampden-Sydney College
Mon, Feb 18, 2013
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 1 / 27
1 Views
2 Modifying the Base Tables
3 Updating Views
4 Using Triggers to Update Views
5 Assignment
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 2 / 27
Outline
1 Views
2 Modifying the Base Tables
3 Updating Views
4 Using Triggers to Update Views
5 Assignment
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 3 / 27
Views
A view is a virtual table constructed from physical tables.A view can be used as a table (select, insert, delete, update), butit does not exist as a physical table in the database.A query on the view must be interpreted as a query on the basetables from which the query is constructed.At times, this can lead to significant complications.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 4 / 27
Views
Views are useful forConstructed 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 Mon, Feb 18, 2013 5 / 27
Creating a View
Creating a ViewCREATE VIEW view_name AS select_statement;
The view is defined to be those tuples that are returned by theSELECT statement.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 6 / 27
Creating a View
Creating a ViewCREATE VIEW emp_w_dep ASSELECT fname, lname, ssn, salary, bdateFROM employeesWHERE ssn IN(SELECT ssn FROM dependents)
For example, we might want to create a view of those employeeswho have dependents.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 7 / 27
Creating a View
Creating a ViewSELECT * 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 Mon, Feb 18, 2013 8 / 27
Views as Tables
Views as TablesSHOW TABLES;+-------------------+| Tables_in_company |+-------------------+| departments || dependents || employees || projects || emp_w_dep || works |+-------------------+
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 Mon, Feb 18, 2013 9 / 27
Selecting From a View
Selecting From a ViewSELECT fname, lnameFROM emp_w_depWHERE 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 Mon, Feb 18, 2013 10 / 27
Selecting From a View
Selecting From a ViewSELECT fname, lname, dep_nameFROM emp_w_dep AS e, dependents AS dWHERE e.ssn = d.ssn;+----------+---------+----------+| 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 Mon, Feb 18, 2013 11 / 27
Outline
1 Views
2 Modifying the Base Tables
3 Updating Views
4 Using Triggers to Update Views
5 Assignment
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 12 / 27
Selecting From a View
Modifying the Base TablesUPDATE employeesSET 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 changeautomatically.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 13 / 27
Outline
1 Views
2 Modifying the Base Tables
3 Updating Views
4 Using Triggers to Update Views
5 Assignment
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 14 / 27
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 Mon, Feb 18, 2013 15 / 27
Updating Views
Updating a ViewINSERT INTO emp_w_depVALUES(’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 permittedprovided they affect only one base table.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 16 / 27
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 Mon, Feb 18, 2013 17 / 27
Updating Views
Inserting Into a Base TableINSERT INTO dependentsVALUES(’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 Mon, Feb 18, 2013 18 / 27
Updating Views
Deleting From a Base TableDELETE FROM dependentsWHERE 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 Mon, Feb 18, 2013 19 / 27
Views and Joins
Updating views is more difficult when the view is based on morethan one table.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 20 / 27
Views and Joins
Creating a ViewCREATE VIEW emp_hrs ASSELECT fname, lname, pname, hoursFROM employeesNATURAL JOIN projectsNATURAL JOIN works;
This view is based on the join of three tables.
Uh-oh.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 21 / 27
Views and Joins
Creating a ViewCREATE VIEW emp_hrs ASSELECT fname, lname, pname, hoursFROM employeesNATURAL JOIN projectsNATURAL JOIN works;
This view is based on the join of three tables.Uh-oh.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 21 / 27
Views and Joins
Creating a ViewSELECT * FROM emp_hrs;+----------+---------+-----------------+-------+| fname | lname | pname | 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 Mon, Feb 18, 2013 22 / 27
Inserting Into a View
Inserting Into a ViewINSERT 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, pname, hours)VALUES(’Jason’, ’Simpson’, ’Clothing’, 25.0);ERROR 1393 (HY000): Can not modify more than one base table througha join view ’company.emp_hrs’
Try to insert into the view.Try updating or deleting.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 23 / 27
Outline
1 Views
2 Modifying the Base Tables
3 Updating Views
4 Using Triggers to Update Views
5 Assignment
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 24 / 27
Inserting Into a View
Inserting Into a ViewCREATE TRIGGER add_emp_depAFTER INSERT ON emp_w_depFOR EACH ROWINSERT INTO dependentsVALUES(NEW.ssn, ’’, null, null);
ERROR 1347 (HY000): ’company.emp_w_dep’ is not BASE TABLE
Normally, modifying a view would have been handled by triggersthat modify the base tables in the appropriate way.Unfortunately, MySQL does not support triggers on views.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 25 / 27
Outline
1 Views
2 Modifying the Base Tables
3 Updating Views
4 Using Triggers to Update Views
5 Assignment
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 26 / 27
Assignment
AssignmentRead Section 5.3.
Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 27 / 27