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.

CHAPTER 34 • MYSQL VIEWSwww.it-ebooks.infoModifying a ViewAn existing view can be modified using the ALTER VIEW statement. Its prototype follows:ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}][DEFINER = { user | CURRENT_USER }][SQL SECURITY { DEFINER | INVOKER }]VIEW view_name [(column_list)]AS select_statement[WITH [CASCADED | LOCAL] CHECK OPTION]For example, to modify employee_contact_info_view by changing the SELECT statement to retrieveonly the first name, last name, and telephone number, just execute the following command:ALTER VIEW employee_contact_info_view(`First Name`, `Last Name`, `Telephone`) ASSELECT first_name, last_name, phoneFROM employees ORDER BY last_name ASC;Deleting a ViewDeleting an existing view is accomplished with the DROP VIEW statement. Its prototype looks like this:DROP VIEW [IF EXISTS]view_name [, view_name]...[RESTRICT | CASCADE]For instance, to delete the employee_contact_info_view view, execute the following command:DROP VIEW employee_contact_info_view;Including the IF EXISTS keywords will cause <strong>MySQL</strong> to suppress an error if an attempt is made todelete a view that doesn’t exist. At the time of publication, the RESTRICT and CASCADE keywords areignored, although presumably they will be representative of new features in a future release.Updating ViewsThe utility of views isn’t restricted solely to abstracting a query against which a user can execute SELECTstatements. Views can also act as an interface from which the underlying tables can be updated. Forexample, suppose that an office assistant is tasked with updating key columns in a table consisting ofemployee contact information. The assistant should be able to view and modify only the employee’s firstname, last name, e-mail address, and telephone number; he should certainly be prevented from viewingor manipulating the SSN and salary. The view employee_contact_info_view, created earlier in thischapter, will satisfy both conditions by acting as both an updatable and selectable view. A view is notupdatable if its query meets any of the following conditions:• It contains an aggregate function such as SUM().• Its algorithm is set to TEMPTABLE.668

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

Saved successfully!

Ooh no, something went wrong!