MySQL Activity 2

download MySQL Activity 2

of 17

Transcript of MySQL Activity 2

  • 8/14/2019 MySQL Activity 2

    1/17

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

    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

  • 8/14/2019 MySQL Activity 2

    2/17

    Page 2 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 onyour 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, oryou 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 windowresult 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 allowyou tonavigate the result by scrolling. Its 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 resultset 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 withinrows to update them, enter new rows, or delete rows.

    The contents of a result are searchable. The result area has a Search buttonthat provides access to Search and Replace dialogs. You can look for a givenvalue in the entire result or within specific columns. Searches can be case

  • 8/14/2019 MySQL Activity 2

    3/17

  • 8/14/2019 MySQL Activity 2

    4/17

    Page 4 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 theMySQL Table Editor.

    o The bookmark browser lists those queries that you havebookmarked. You can organize bookmarks hierarchically by

    creating folders and moving, removing, or renaming bookmarks.

    The history browser contains previously issued queries, hierarchicallyorganized by day.

    You can drag queries from the bookmark or history browser to the queryarea 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 statementdisplays syntax information for it from theMySQL 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 SQLstatements. Double-clicking on a function displays the description for it

    from the MySQL Reference Manual. The information appears in a tab inthe 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 windowarea or press Ctrl E from keyboard.

  • 8/14/2019 MySQL Activity 2

    5/17

  • 8/14/2019 MySQL Activity 2

    6/17

    Page 6 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. ClickDelete

    Row(s). (see Figure 15). ClickApply Changes.

    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

    PC004Neo Pentium Celerondesktop pkg 11/23/2009

    PC005 AMD Athlon box 11/23/2009

    PC006 Prolink ADSL Modem Router ea 11/23/2009

    Selec t to de le te rows.

  • 8/14/2019 MySQL Activity 2

    7/17

    Page 7 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

    PC016MS Windows XP licensedcopy 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).

    Figure 16. Changing date format

    Enter new date

    format here .

  • 8/14/2019 MySQL Activity 2

    8/17

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

    Save our spreadsheet as ProductSheet.csv (comma separated values) file. Dialogwindow 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 followingstatement 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 thefolder 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;

  • 8/14/2019 MySQL Activity 2

    9/17

    Page 9 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

    FROM ;

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

    SELECT description, unit

    FROM products;

  • 8/14/2019 MySQL Activity 2

    10/17

    Page 10 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.

  • 8/14/2019 MySQL Activity 2

    11/17

    Page 11 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 statementgoes:

    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

  • 8/14/2019 MySQL Activity 2

    12/17

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

    BETWEEN and AND clause

    You can also filtered out specific dates criteria. BETWEEN and AND clause has thissyntax:

    BETWEEN AND ;

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

  • 8/14/2019 MySQL Activity 2

    13/17

    Page 13 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. ClickCreate New Table. Enter the following data on

    the form editor. ClickApply Changes to complete. Confirm with all the prompt windowsthat will come after. See Figure 24.

    Figure 24. MySQL Table Editor Creating customers Table

  • 8/14/2019 MySQL Activity 2

    14/17

    Page 14 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 MainResultset 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.

    Figure 25. Renaming and Creating Resultset Tabsheets

    Cl ick t o c rea te new resu l t se t t absheet .

    Righ t c l i ck to rename.

  • 8/14/2019 MySQL Activity 2

    15/17

    Page 15 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-30Table 2. Data at CustomerSheet.csv

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

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

    Figure 26. Output for Answer Item 234

    Supply s ta t ement /s here.

  • 8/14/2019 MySQL Activity 2

    16/17

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

    5. Display rows with INDV business type, name arrange in descending order. Store yourresult query at a new resultset tabsheet Answer item 5. See Figure 27.

    Figure 27. Output for Answer Item 5

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

    Figure 28. Output for Answer Item 6

    Supply s ta t ement /s here.

    Supply s ta t ement /s here.

  • 8/14/2019 MySQL Activity 2

    17/17

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

    7. Display products table filtered with date_created on 2009-11-23. Output shouldbe like Figure 29.

    Figure 29. Output for Answer Item 7

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

    Figure 30. Output for Answer Item 8

    Supply s ta t ement /s here.

    Supply s ta t ement /s here.