11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 34 • MYSQL VIEWS• It contains DISTINCT, GROUP BY, HAVING, UNION, or UNION ALL.• It contains an outer join.• It contains a nonupdatable view in the FROM clause.• It contains a subquery in the SELECT or FROM clause, and a subquery in the WHEREclause that refers to a table in the FROM clause.• It refers solely to literal values, meaning there are no tables to update.For example, to modify employee Bob Connors’ phone number, you can execute the UPDATE queryagainst the view, like so:UPDATE employee_contact_info_viewSET phone='2125558989' WHERE email='bob@example.com';The term “updatable view” isn’t restricted solely to UPDATE queries; you can also insert new rows viathe view, provided that the view satisfies a few constraints:• The view must contain all the columns in the underlying table that aren’t assigneda default value.• The view columns cannot contain an expression. For example, the view columnCEILING(salary) will render the view uninsertable.Therefore, based on the present view definition, a new employee could not be added using theemployee_contact_info_view view because table columns that are not assigned a default value, such assalary and ssn, are not available to the view.Incorporating Views into Web ApplicationsLike the stored procedure and trigger examples presented in the previous two chapters, incorporatingviews into your web applications is a rather trivial affair. After all, views are virtual tables and can bemanaged much in the same way as a typical <strong>MySQL</strong> table, using SELECT, UPDATE, and DELETE to retrieveand manipulate the content they represent. As an example, execute the employee_contact_info_viewview created earlier in this chapter. To save you the trouble of referring back to the beginning of thechapter, the view creation syntax is repeated here:CREATE VIEW employee_contact_info_view(`First Name`, `Last Name`, `E-mail Address`, `Telephone`) ASSELECT first_name, last_name, email, phoneFROM employees ORDER BY last_name ASC;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!