Table of Contents for
Mastering phpMyAdmin 3.4 for Effective MySQL Management

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering phpMyAdmin 3.4 for Effective MySQL Management by Marc Delisle Published by Packt Publishing, 2012
  1. Cover
  2. Mastering phpMyAdmin 3.4 for Effective MySQL Management
  3. Mastering phpMyAdmin 3.4 for Effective MySQL Management
  4. Credits
  5. About the Author
  6. About the Reviewers
  7. www.PacktPub.com
  8. Preface
  9. What you need for this book
  10. Who this book is for
  11. Conventions
  12. Reader feedback
  13. Customer support
  14. 1. Getting Started with phpMyAdmin
  15. What is phpMyAdmin?
  16. Installing phpMyAdmin
  17. Configuring phpMyAdmin
  18. Installing phpMyAdmin configuration storage
  19. Upgrading phpMyAdmin
  20. Summary
  21. 2. Configuring Authentication and Security
  22. Securing phpMyAdmin
  23. Summary
  24. 3. Over Viewing the Interface
  25. Customizing general settings
  26. Character sets and collations
  27. Navigation panel
  28. Main panel
  29. User preferences
  30. Query window
  31. Summary
  32. 4. Creating and Browsing Tables
  33. Creating our first table
  34. Inserting data manually
  35. Browse mode
  36. Profiling queries
  37. Creating an additional table
  38. Summary
  39. 5. Changing Data and Structure
  40. Changing table structure
  41. Summary
  42. 6. Exporting Structure and Data (Backup)
  43. Exporting a database
  44. Exporting a table
  45. Exporting selectively
  46. Exporting multiple databases
  47. Saving the export file on the server
  48. Memory limits
  49. Summary
  50. 7. Importing Structure and Data
  51. Importing SQL files
  52. Importing CSV files
  53. Importing other formats
  54. Reading files from a web server upload directory
  55. Displaying an upload progress bar
  56. Summary
  57. 8. Searching Data
  58. Performing a complete database search
  59. Stopping an errant query
  60. Summary
  61. 9. Performing Table and Database Operations
  62. Changing table attributes
  63. Emptying or deleting a table
  64. Renaming, moving, and copying tables
  65. Performing other table operations
  66. Multi-table operations
  67. Database operations
  68. Summary
  69. 10. Benefiting from the Relational System
  70. Defining relations with the relation view
  71. Defining relations with the Designer
  72. Benefiting from the defined relations
  73. Column commenting
  74. Summary
  75. 11. Entering SQL Statements
  76. The Query window
  77. Multi-statement queries
  78. Pretty printing (syntax highlighting)
  79. The SQL Validator
  80. Summary
  81. 12. Generating Multi-table Queries
  82. Exploring column criteria
  83. Generating automatic joins (internal relations)
  84. Executing the query
  85. The visual builder
  86. Summary
  87. 13. Synchronizing Data and Supporting Replication
  88. Supporting MySQL replication
  89. Summary
  90. 14. Using Query Bookmarks
  91. Creating bookmarks
  92. Recalling bookmarks from the bookmarks list
  93. Passing a parameter to a bookmark
  94. Summary
  95. 15. Documenting the System
  96. Generating relational schemas
  97. Summary
  98. 16. Transforming Data using MIME
  99. Enabling transformations
  100. Examples of transformations
  101. Summary
  102. 17. Supporting Features Added in MySQL 5
  103. Supporting routines—stored procedures and functions
  104. Executing code with triggers
  105. Using information_schema
  106. Partitioning
  107. Exploring the event scheduler
  108. Summary
  109. 18. Tracking Changes
  110. Prerequisites
  111. Principles
  112. Initiating tracking for one table
  113. Testing the tracking mechanism
  114. Determining tracking status
  115. Structure snapshot
  116. Exporting a version
  117. Creating a new version
  118. Deleting tracking information
  119. Summary
  120. 19. Administrating the MySQL Server
  121. Database information
  122. Server information
  123. Summary
  124. A. Troubleshooting and Support
  125. Seeking support
  126. Contributing to the project

Chapter 8. Searching Data

In this chapter, we present mechanisms that can be used to find the data we are looking for, instead of just browsing tables page-by-page and sorting them. In Search mode, application developers can look for data in ways not expected by the interface they are building—adjusting and sometimes repairing data. This chapter covers single-table searches and entire database searches. Chapter 12 is a complement to this chapter and presents examples of searches involving multiple tables at once.

Single-table searches

This section describes the Search page where a single-table search is available. Searching in just one table is effective only in the situation where a single table regroups all the data on which we want to search. If the data is scattered in many tables, a database search should be launched instead, and this is covered later in the chapter.

Entering the search page

The Search page can be accessed by clicking on the Search link in the Table view. This has been done here for the book table:

Entering the search page

The most commonly used section of the Search interface (query by example) is the one immediately displayed, whereas other dialogs are hidden in a slider that can be activated by the Options link (more on these dialogs later in this chapter).

Searching criteria by column—query by example

The main use of the Search panel is to enter criteria for some columns so as to retrieve only the data we are interested in. This is called query by example because we give an example of what we are looking for. Our first retrieval will concern finding the book with ISBN 1-234567-89-0. We simply enter this value in the isbn box and set the Operator field to =.

Searching criteria by column—query by example

Clicking on Go gives these results (shown partially in the following screenshot):

Searching criteria by column—query by example

This is a standard results page. If the results ran in pages, we could navigate through them, and edit and delete data for the subset we have chosen during the process. Another feature of phpMyAdmin is that the columns used as the criteria are highlighted by changing the border color of the columns to better reflect their importance on the results page.

It isn't necessary to specify that the isbn column be displayed even though this is the column in which we search. We could have selected only the title column for display (refer to the Selecting the columns to be displayed section) and chosen the isbn column as a criterion.

Searching for empty / non-empty values

Two handy operators are present in the operator's list when the column has a character type such as CHAR, VARCHAR, or TEXT:

  • = ''
  • != ''

Those are the ones to use when you want to search for an empty (= '') or not empty (!= '') value in some column. Normally, typing nothing in a column's Value field means that this column does not participate in the search process. However, with one of these operators, this column is included in the generated search query.

Note

Please do not confuse this method with searching for a NULL value, which is quite different. Indeed, a NULL value (refer to http://en.wikipedia.org/wiki/Null_(SQL) for a more complete explanation) is a special value that conveys that some information is missing in this column.

Producing reports with Print view

We see the Print view and Print view (with full texts) links on the results page. These links produce a more formal report of the results (without the navigation interface) directly to the printer. In our case, using Print view would produce the following:

Producing reports with Print view

This report contains information about the server, database, time of generation, version of phpMyAdmin, version of MySQL, and generated SQL query. The other link, Print view (with full texts), would print the contents of the TEXT columns in their entirety.

Searching with wildcard characters

Let us assume we are looking for something less precise—all books with "cinema" in their title. First, we go back to the search page. For this type of search, we will use SQL's LIKE operator. This operator accepts wildcard characters—the % character (which matches any number of characters) and the underscore (_) character (which matches a single character). Thus we can use %cinema% to let phpMyAdmin find any substring that matches the word "cinema". If we left out both wildcard characters, we would get exact matches with only that single word.

This substring matching is easier to access, being part of the Operator drop-down list. We only have to enter the word cinema and use the operator LIKE %...% to perform that match. We should avoid using this form of the LIKE operator on big tables (comprising of thousands of rows), as MySQL does not use an index for data retrieval in this case, leading to wait times that depend on the server hardware and its current load. This is why this operator is not the default one in the drop-down list, even though this method of search is commonly used on smaller tables.

The following screenshot shows how we ask for a search on cinema with the LIKE %...% operator:

Searching with wildcard characters

Note

The LIKE operator can be used for other types of wildcard searches, for example History%, which would search for this word at the beginning of a title. As the expression does not start with a wildcard character, MySQL will try to use an index if it finds one that speeds up data retrieval. For more details about MySQL's use of indexes, please refer to http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html.

Using either of these methods of performing the query produces the following results:

Searching with wildcard characters

The % and _ wildcard characters may be repeated in a search expression; for example, histo__ (two underscores) would match history whereas histo% would match history and historian. The MySQL manual gives more examples at http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html.

Case sensitivity and search

In the previous example, we could have replaced "cinema" with "CINEMA" and achieved similar results. The reason is that the collation of the title column is latin1_swedish_ci. This collation comes from the collation set, by default, at database creation unless the server's default collation has been changed (refer to http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html). Here, ci means that comparisons are done in a case-insensitive way. Please refer to http://dev.mysql.com/doc/refman/5.1/en/case-sensitivity.html for more details.

Combining criteria

We can use multiple criteria for the same query (for example, to find all the English books of more than 300 pages). There are more comparative choices in Operator because the page_count column is numeric, as shown in the following screenshot:

Combining criteria

Search options

The Options slider reveals additional panels to further refine the search process.

Selecting the columns to be displayed

In the Options slider, a Select columns panel facilitates selection of the columns to be displayed in the results. All columns are selected by default, but we can Ctrl + Click on other columns to make the necessary selections. Mac users would use Command + Click to select/unselect the columns.

Following are the columns of interest in this example:

Selecting the columns to be displayed

We can also specify the number of rows per page in the textbox next to the column selection. The Add search conditions box will be explained in the Applying a WHERE clause section, which will follow shortly.

Ordering the results

The Display order dialog permits the specification of an initial sorting order for the results to come. In this dialog, a drop-down menu contains all the table's columns; it's up to us to select the one on which we want to sort. By default, the sorting will be in Ascending order, but a choice of Descending order is also available.

It should be noted that on the results page, we can change the sort order using the techniques explained in Chapter 4.

Applying a WHERE clause

Sometimes, we may want to enter a search condition that is not offered in the Function list of the query by example section. The list cannot contain every possible variation in the language. Let us say we want to find all the English or French books using the IN clause. To do this, we can use the Add search conditions section.

Applying a WHERE clause

Note

The complete search expression is generated by combining the search conditions and other criteria (entered in the query by example lines) with a logical AND operator.

We could have a more complex list of search conditions that would be entered in the same textbox, possibly with brackets and operators such as AND or OR.

A Documentation link points to the MySQL manual where we can see a huge choice of available functions. (Each function is applicable to a specific column type.)

Avoiding repeated results

The normal behavior of the SELECT statement is to fetch all entries corresponding to the criteria, even if some entries are repeated. Sometimes, we may want to avoid getting the same results more than once. For example, if we want to know in which cities we have clients, displaying each city name once would be enough. Here, we want to know in which languages our books are written. In the Select columns dialog, we choose just the language column, and we check DISTINCT, as shown in the following screenshot:

Avoiding repeated results

Clicking on Go produces a results page where we see en just once; without the DISTINCT option, the row containing en would have appeared three times.

If we select more than one column (for example author_id and language) and mark the DISTINCT option, we will now see two lines in the results as there are two books in English (but from different authors). Results are still not repeated.