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

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

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

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

Insertions, Deletions, and Updates

Relational Databases

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

PHP Queries and HTML Forms Lecture 23

Relational Algebra Part I. CS 377: Database Systems

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

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

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

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

Part 1 on Table Function

Relational Algebra & Calculus. CS 377: Database Systems

MySQL Creating a Database Lecture 3

CIS611 Lab Assignment 1 SS Chung

The Critical-Path Algorithm

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

Minimal Spanning Trees

Solving Recursive Sequences by Iteration

XQuery FLOWR Expressions Lecture 35

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

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

Scheduling and Digraphs

The Pairwise-Comparison Method

Introduction to Databases

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

Introduction to Databases

Scope and Parameter Passing

Relational Databases Lecture 2

LR Parsing - Conflicts

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

Relational Calculus: 1

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

Database design process

Friends and Unary Operators

Session Active Databases (2+3 of 3)

DTDs and XML Attributes

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

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

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

The Decreasing-Time Algorithm

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

The Traveling Salesman Problem Brute Force Method

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

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

ECE 650 Systems Programming & Engineering. Spring 2018

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

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

Programming Languages

Sampling Distribution Examples Sections 15.4, 15.5

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

Implementing Linked Lists

Rotations and Translations

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

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

Relational Algebra 1

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

XML and AJAX Lecture 28

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

The Plurality-with-Elimination Method

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

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

Displaying Distributions - Quantitative Variables

Chapter 6 - Part II The Relational Algebra and Calculus

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Recursive Descent Parsers

Magnification and Minification

Scope and Parameter Passing

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Database Technology. Topic 3: SQL. Olaf Hartig.

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Coefficient of Determination

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

Course Notes on Relational Algebra

The Relational Algebra

XQuery Constructors and Joins Lecture 36

DEPARTMENT DNAME DNUMBER MGRNAME MGRSTARTDATE

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

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Density Curves Sections

CSC 742 Database Management Systems

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

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

CS5300 Database Systems

Recognition of Tokens

Function Definition Syntax Tree

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

Street-Routing Problems

CS430 Final March 14, 2005

Programming Languages

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

Building the Abstract Syntax Trees

LR Parsing - The Items

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Ambient and Diffuse Light

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

Transcription:

Views Lecture 15 Section 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), but it does not exist as a physical table 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 Mon, Feb 18, 2013 4 / 27

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 Mon, Feb 18, 2013 5 / 27

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 Mon, Feb 18, 2013 6 / 27

Creating a View Creating a View CREATE VIEW emp_w_dep AS SELECT fname, lname, ssn, salary, bdate 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 Mon, Feb 18, 2013 7 / 27

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 Mon, Feb 18, 2013 8 / 27

Views as Tables Views as Tables SHOW 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 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 Mon, Feb 18, 2013 10 / 27

Selecting From a View Selecting From a View SELECT fname, lname, dep_name FROM emp_w_dep AS e, dependents AS d WHERE 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 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 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 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 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 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 Mon, Feb 18, 2013 18 / 27

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 Mon, Feb 18, 2013 19 / 27

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 Mon, Feb 18, 2013 20 / 27

Views and Joins Creating a View CREATE VIEW emp_hrs AS SELECT fname, lname, pname, hours FROM employees NATURAL JOIN projects NATURAL JOIN works; This view is based on the join of three tables. Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 21 / 27

Views and Joins Creating a View CREATE VIEW emp_hrs AS SELECT fname, lname, pname, hours FROM employees NATURAL JOIN projects NATURAL 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 View SELECT * 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 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, pname, 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 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 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 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 Assignment Read Section 5.3. Robb T. Koether (Hampden-Sydney College) Views Mon, Feb 18, 2013 27 / 27