11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 34 • MYSQL VIEWSwww.it-ebooks.infoIntroducing ViewsAlso known as a virtual table, a view consists of a set of rows that is returned if a particular query isexecuted. A view isn’t a copy of the data represented by the query, but rather it simplifies the way inwhich that data can be retrieved by making the query available via an alias.Views can be quite advantageous for a number of reasons:• Simplicity: Certain data resources are subject to retrieval on a frequent basis. Forinstance, associating a client with a particular invoice occurs quite often in acustomer relationship-management application. Therefore, it might beconvenient to create a view called get_client_name, saving you the hassle ofrepeatedly querying multiple tables to retrieve this information.• Security: As previously mentioned, there may be situations in which you’ll want tomake quite certain some information is inaccessible to third parties, such as theSSNs and salaries of employees. A view offers a practical solution to implementthis safeguard.• Maintainability: Just as an object-oriented class abstracts underlying data andbehavior, a view abstracts the gory details of a query. Such abstraction can bequite beneficial in instances where that query must later be changed to reflectmodifications to the schema.Now that you have a better understanding of how views can be an important part of yourdevelopment strategy, it’s time to learn more about <strong>MySQL</strong>’s view support.<strong>MySQL</strong>’s View SupportTo the <strong>MySQL</strong> community’s great delight, views were integrated into the <strong>MySQL</strong> distribution as ofversion 5.0. In this section, you’ll learn how to create, execute, modify, and delete views.Creating and Executing ViewsCreating a view is accomplished with the CREATE VIEW statement. Its prototype follows:CREATE[OR REPLACE][ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED }][DEFINER = { user | CURRENT_USER }][SQL SECURITY { DEFINER | INVOKER }]VIEW view_name [(column_list)]AS select_statement[WITH [CASCADED | LOCAL] CHECK OPTION]Throughout the course of this section, the CREATE VIEW syntax in its entirety will be introduced;however, for now let’s begin with a simple example. Suppose your database consists of a table calledemployees, which contains information about each employee. The table creation syntax looks like this:660

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

Saved successfully!

Ooh no, something went wrong!