You are on page 1of 17

Laboratory Activity 2

SELECT Statements
using MySQL Query Browser
Objectives

At the end of the activity, student is expected to perform/identify the following:

 Identify the different areas of Query Browser Main Window;


 Edit, insert, delete rows;
 Upload data from spreadsheet;
 Insert, update and delete tables.
 Use WHERE, BETWEEN and AND CLAUSE;
 Create table using MySQL Table Editor;
 Create and rename Resultset area.

Launch MySQL Query Browser

To start MySQL Query Browser application, click Start | Programs | MySQL | MySQL
Query Browser.

Figure 11. Launching of 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.

Figure 12. Connecting to server

Query Browser Main Window

The main window consists of the following:

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.

Query Area Object


Browser

Result Area

Information
Browser

Figure 13. Different Areas of Query 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:

o Right-click in the browser to create a new database or table.


o Right-click on a database name to drop the database.
o Right-click on a table name to drop the table or edit it with the
MySQL Table Editor.
o The bookmark browser lists those queries that you have
bookmarked. You can organize bookmarks hierarchically by
creating folders and moving, removing, or renaming bookmarks.

 The history browser contains previously issued queries, hierarchically


organized by day.
 You can drag queries from the bookmark or history browser to the query
area for re-execution. Double-clicking a query also enters it into the query
area.

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.

Insert, Edit and Delete Rows


We will use the existing my_store database that we have created at Activity 1. Enter the
statement on the query area:

SELECT * FROM products;

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.

 Click Edit tab at the bottom of result area.


 Click second row of ProductID beneath PC002. Enter PC001.
 Press Tab key from keyboard to proceed to Description column. Enter
Compaq laptop computer.
 Press again tab key to succeeding columns and enter ea at unit; 2009-11-
23 at date_created.
 Click Apply Changes tab. New row is now inserted. Your screen should
somewhat have the same output at Figure 14.

Execute button

Edit and Apply Changes


buttons

Figure 14. New row insertion

We insert two new rows. Enter the following data and apply changes:

 PC100, Continuous form, rm, 2009-12-01


 PC101, Light Pen, ea, 2009-12-01.

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.

Select to delete rows.

Figure 15. Right clicking mouse to delete rows.

Uploading Data from Spreadsheet


Enter the following data on a spreadsheet. MS Excel application was used in this example.
Save this spreadsheet as ProductSheet.

PC003 Acer Pentium Dual Core ea 11/23/2009


Neo Pentium Celeron
PC004 desktop pkg 11/23/2009
PC005 AMD Athlon box 11/23/2009
PC006 Prolink ADSL Modem Router ea 11/23/2009

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

Let us adjust the date format of our dates.

 Click the entire column where dates are entered.


 Click Format | Cells | Custom
 Under Type enter : yyyy-mm-dd. Click OK. (see Figure 16).

Enter new date


format here.

Figure 16. Changing date format

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.

Figure 17. Save spreadsheet as ProductSheet.csv

Go back to your MySQL Query Browser window. Issue and execute the following
statement on the query area:

LOAD DATA INFILE


'C:/Documents and Settings/jerry/My Documents/ProductSheet.csv'
INTO TABLE products
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';

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:

SELECT * FROM products;

Page 8 of 17 http://ThrivingAndLiving.blogspot.com
Your output should somewhat the same as Figure 18.

Figure 18. Successful data uploading from spreadsheet

Display Specific Column/s from Table


You can select specific columns only by following SELECT syntax:

SELECT <column name1, column name2….>


FROM <table name>;

If we want to display only the description and unit data from table products, we write
the following:

SELECT description, unit


FROM products;

Page 9 of 17 http://ThrivingAndLiving.blogspot.com
Displaying it on ascending or alphabetical order, we use this statement instead:

SELECT description, unit


FROM products
ORDER BY description ASC;

We have the following output at Figure 19.

Figure 19. Issue ASC for Ascending; DESC for Descending.

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';

Figure 20 produces the output of this statement:

Figure 20. The use of WHERE clause

Because we want ordered information, enhance it with this statement (see Figure 21).

SELECT description, unit


FROM products
WHERE unit = 'ea'
ORDER BY description ASC;

Figure 21. ORDER BY and ASC clause to arrange it alphabetically

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:

BETWEEN <starting value> AND <ending value>;

Let us display description, unit and date_created rows which have


date_created between 2009-11-19 and 2009-11-23. Our statement should go like this:

SELECT description, unit, date_created


FROM products
WHERE date_created
BETWEEN '2009-11-19' AND '2009-11-23';

Figure 22 has this result.

Figure 22. Applying BETWEEN and AND clause

Applying the ORDER BY <column name> DESC, we have an output on Figure 23.

SELECT description, unit, date_created


FROM products
WHERE date_created
BETWEEN '2009-11-19' AND '2009-11-23'
ORDER BY date_created DESC;

Page 12 of 17 http://ThrivingAndLiving.blogspot.com
Figure 23. Date_created arranged in descending order

Create New Table using MySQL Table Editor

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.

Figure 24. MySQL Table Editor Creating customers Table

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.

Right click to rename.


Click to create new resultset tabsheet.

Figure 25. Renaming and Creating Resultset Tabsheets

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

2. Launch your MySQL Query Browser and load CustomerSheet.csv at customers


table.
3. Display the content of customers table.
4. Rename Resultset tab with Answer Item 234. Your output should be like Figure 26.

Supply statement/s here.

Figure 26. Output for Answer Item 234

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.

Supply statement/s here.

Figure 27. Output for Answer Item 5

6. Display products like Figure 28 with criterion of all unit with pkg data. Tab sheet
should be named Answer Item 6.

Supply statement/s here.

Figure 28. Output for 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.

Supply statement/s here.

Figure 29. Output for Answer Item 7

8. Determine the criteria set and display customers with the following format at Figure 30.

Supply statement/s here.

Figure 30. Output for Answer Item 8

Page 17 of 17 http://ThrivingAndLiving.blogspot.com

You might also like