MySQL Activity 2
MySQL Activity 2
SELECT Statements
using MySQL Query Browser
Objectives
To start MySQL Query Browser application, click Start | Programs | MySQL | MySQL
Query Browser.
Page 1 of 17 http://ThrivingAndLiving.blogspot.com
Supply localhost at server host; root at username. We will use the existing
database that we did on Activity 1: my_store. Enter the password you have designated on
your server. See Figure 12.
Query Area. You can enter queries manually by typing them into the query area, or
you can construct queries graphically by using the mouse to select tables, columns, or query
components.
Result Area. When you execute a query, its results appear in the query window
result area. This area provides flexible result display and has the following characteristics:
For results that do not fit within the display area, scroll bars appear that allow
you tonavigate the result by scrolling. It’s also possible to toggle the result set
display to use the entire query window.
If a result set is selected from a single table that has a primary key, the result
set can be edited to modify the original table. (There is an Edit button in the
result area that enables editing.) You can modify individual values within
rows to update them, enter new rows, or delete rows.
The contents of a result are searchable. The result area has a Search button
that provides access to Search and Replace dialogs. You can look for a given
value in the entire result or within specific columns. Searches can be case
Page 2 of 17 http://ThrivingAndLiving.blogspot.com
sensitive or not, and they can be based on whole word or partial word
matching.
A result is placed within the current tab of the result area, and each successive
queryoverwrites the tab contents. To prevent this, you can create additional
tabs for displaying multiple results and then switch between then.
Result Area
Information
Browser
Object Browser. The area for the Object Browser provides access to databases and
queries:
The database browser (schemata browser) displays a hierarchical view of
your databases. It lists each database, with the default database name
highlighted so that you can tell at a glance which one is current. The
default database is the one used for references to tables and routines that
are not qualified with a database name.
The display for any database can be expanded to show the tables
and stored routines within the database. Likewise, expanding a table
display shows its columns and expanding a routine’s display shows its
parameters. Double-clicking a database name selects it as the default
database. Double-clicking a table name enters a SELECT * FROM
table_name statement in the query area.
Page 3 of 17 http://ThrivingAndLiving.blogspot.com
Right-clicking in the database browser brings up a menu for
additional capabilities:
Information Browser. The area for the Information Browser provides access to
documentation, query parameters, and current-transaction information:
The syntax browser lists SQL statements. Double-clicking on a statement
displays syntax information for it from the MySQL Reference Manual. The
information appears in a tab in the result area.
The function browser lists the built-in functions that you can use in SQL
statements. Double-clicking on a function displays the description for it
from the MySQL Reference Manual. The information appears in a tab in
the result area.
The parameter browser displays query parameters.
The transaction browser shows the statements that are part of the current
transaction.
To execute the statement, either click execute button at the top of the window
area or press Ctrl E from keyboard.
Page 4 of 17 http://ThrivingAndLiving.blogspot.com
Let us insert row for the data we have deleted at Activity 1.
Execute button
We insert two new rows. Enter the following data and apply changes:
Page 5 of 17 http://ThrivingAndLiving.blogspot.com
Delete these two new rows.
Go to PC100 and multiple select this row and PC101 row by pressing SHIFT ↓
(arrow down key).
Right click mouse within the blue area of the selected rows. Click Delete
Row(s). (see Figure 15).
Click Apply Changes.
Page 6 of 17 http://ThrivingAndLiving.blogspot.com
PC007 Samsung 22 LCD Monitor ea 11/23/2009
PC008 Creative Optical Mouse ea 11/23/2009
PC009 Headphone with Mic pc 11/23/2009
PC010 IBM Think Pad pkg 11/23/2009
PC011 USB cable mtr 11/23/2009
PC012 Extension cord mtr 11/22/2009
PC013 HP 910 deskjet printer ea 11/21/2009
PC014 Glade deodorizer can 11/20/2009
PC015 Manhattan Web Camera pc 11/19/2009
MS Windows XP licensed
PC016 copy pkg 11/25/2009
PC017 HP 900 Ink ea 11/26/2009
PC018 LG 80gb HDD ea 11/27/2009
Table 1. Data in ProductSheet.csv
Page 7 of 17 http://ThrivingAndLiving.blogspot.com
Save our spreadsheet as ProductSheet.csv (comma separated values) file. Dialog
window of Figure 17 shows how to select a .csv file format.
Go back to your MySQL Query Browser window. Issue and execute the following
statement on the query area:
Note: You should change the folder name on the above statement. Change it with the
folder location where you saved your Product.csv file. Use / instead of \ as separator
between subfolders/file.
List the content of the table by issuing the statement to check if you uploaded the data
successfully:
Page 8 of 17 http://ThrivingAndLiving.blogspot.com
Your output should somewhat the same as Figure 18.
If we want to display only the description and unit data from table products, we write
the following:
Page 9 of 17 http://ThrivingAndLiving.blogspot.com
Displaying it on ascending or alphabetical order, we use this statement instead:
Page 10 of 17 http://ThrivingAndLiving.blogspot.com
Using WHERE clause to Display on Specific Rows
WHERE clause is used to filter out data based from given criterion. Where condition/s
should follow after the word WHERE.
Suppose we want to determine which rows have ‘ea’ on its unit. So, our statement
goes:
SELECT description, unit
FROM products
WHERE unit='ea';
Because we want ordered information, enhance it with this statement (see Figure 21).
Page 11 of 17 http://ThrivingAndLiving.blogspot.com
BETWEEN and AND clause
You can also filtered out specific dates criteria. BETWEEN and AND clause has this
syntax:
Applying the ORDER BY <column name> DESC, we have an output on Figure 23.
Page 12 of 17 http://ThrivingAndLiving.blogspot.com
Figure 23. Date_created arranged in descending order
Design a new table under my_store. Name the table as customers. Launch the MySQL Table
Editor by right clicking my_store. Click Create New Table. Enter the following data on
the form editor. Click Apply Changes to complete. Confirm with all the prompt windows
that will come after. See Figure 24.
Page 13 of 17 http://ThrivingAndLiving.blogspot.com
Rename/Create New ResultSet Tabsheet
We can rename our result tabsheet by right clicking the existing Resultset 1. Supply Main
Resultset as a new name.
Creating a new resultSet tabsheet is done by clicking the icon before the tabsheet.
Apply same procedure in renaming it. See Figure 25.
Page 14 of 17 http://ThrivingAndLiving.blogspot.com
CHECK YOUR UNDERSTANDING
Do the following tasks:
1. Create spreadsheet CustomerSheet and enter the following data. Save this using CSV
file format.
C0001 John Smith 717 S Minaret Ave Turlock CA 95330 INDV 2009-11-01
C0002 Maria Santos 790 11th Ave New York NY 10019 INDV 2009-11-05
C0003 Apex Corp 527 Detroit St Hammond IN 46320 CORP 2009-11-05
C0004 Lourdes Cruz 123 Dollar St Long Beach CA 95611 INDV 2009-11-07
C0005 Petro Max LLC CRESCENT OAK Irvine CA 92618 CORP 2009-11-10
C0006 Dax Allan Lamb WEBB AVE Bronx NY 10468 INDV 2009-11-10
C0007 Rea Kline COUNTRY CT RUSSELLVILLE AL 35654 INDV 2009-11-10
C0008 Francia Hollis BOX 837 VOLCANO HI 96785 INDV 2009-11-10
C0009 NetSurf Limited AVERILL PKWY SANDY OR 97055 CORP 2009-11-15
C0010 Grace Celestine MISSION ST SAN FRANCISCO CA 94112 INDV 2009-11-15
C0011 Amor Esperanza DUMOND PL CORAM NY 11727 INDV 2009-11-30
Table 2. Data at CustomerSheet.csv
Page 15 of 17 http://ThrivingAndLiving.blogspot.com
5. Display rows with INDV business type, name arrange in descending order. Store your
result query at a new resultset tabsheet Answer item 5. See Figure 27.
6. Display products like Figure 28 with criterion of all unit with pkg data. Tab sheet
should be named Answer Item 6.
Page 16 of 17 http://ThrivingAndLiving.blogspot.com
7. Display products table filtered with date_created on 2009-11-23. Output should
be like Figure 29.
8. Determine the criteria set and display customers with the following format at Figure 30.
Page 17 of 17 http://ThrivingAndLiving.blogspot.com