Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Similar documents
Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Relational Databases

Insertions, Deletions, and Updates

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

PHP Queries and HTML Forms Lecture 23

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Relational Algebra Part I. CS 377: Database Systems

Functional Dependencies and Normal Forms

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Relational Algebra & Calculus. CS 377: Database Systems

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Functional Dependencies and Normal Forms

XQuery FLOWR Expressions Lecture 35

Part 1 on Table Function

LR Parsing - Conflicts

The Critical-Path Algorithm

CIS611 Lab Assignment 1 SS Chung

Friends and Unary Operators

Minimal Spanning Trees

Relational Databases Lecture 2

Relational Calculus: 1

Solving Recursive Sequences by Iteration

MySQL Creating a Database Lecture 3

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The Traveling Salesman Problem Brute Force Method

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

KINGDOM OF SAUDI ARABIA-JAZAN UNIVERSITY COLLEGE OF COMPUTER SCIENCE & INFORMATION SYSTEMS 221 INFS 3 DATABASE SYSTEMS-1 REVIEW QUESTIONS

Introduction to Databases

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Class Construct Part 1

Some different database system architectures. (a) Shared nothing architecture.

The Plurality-with-Elimination Method

Magnification and Minification

Introduction to Databases

Scope and Parameter Passing

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Database design process

Scheduling and Digraphs

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

CS430 Final March 14, 2005

Session Active Databases (2+3 of 3)

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Relational Algebra

Programming Languages

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Sampling Distribution Examples Sections 15.4, 15.5

Implementing Linked Lists

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Rotations and Translations

LR Parsing - The Items

The Graphics Pipeline

Scope and Parameter Passing

What is an algebra? A formal system of manipulation of symbols to deal with general statements of relations.

Query 2: Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry, Bellaire, TX Wallace 291 Berry, Bellaire, TX

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

DTDs and XML Attributes

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

List Iterator Implementation

ECE 650 Systems Programming & Engineering. Spring 2018

Relational Algebra 1

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

XML and AJAX Lecture 28

Chapter 6 - Part II The Relational Algebra and Calculus

Displaying Distributions - Quantitative Variables

Database Technology. Topic 3: SQL. Olaf Hartig.

Recursive Descent Parsers

The CYK Parsing Algorithm

Database Systems ER Model. A.R. Hurson 323 CS Building

The Coefficient of Determination

Recursive Linked Lists

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Density Curves Sections

The x86 Architecture

Basic PHP Lecture 17

Function Definition Syntax Tree

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Programming Languages

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Street-Routing Problems

Course Notes on Relational Algebra

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Building the Abstract Syntax Trees

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

Dynamic Allocation of Memory

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

The Graphics Pipeline

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Transcription:

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