Lab 7 - Part2 Is222
Lab 7 - Part2 Is222
Using MySQL
Objectives:
Pre-Lab Preparation:
Note:
After the lab and the lectures from the previous week, you should be aware of the
following: -
Connecting to your MySql account
1
Issuing basic commands in MySql (over the Shell), using your database,
seeing table structures/data and the tables in your db, etc
The fundamental syntax for creating tables and DML queries such as
select ,insert etc including making selections based on certain conditions,
and the concat function etc (as discussed in the lecture)
All other query related elements discussed in the lectures from the
previous week
How to access your database via phpMyAdmin
What will you be doing?
In this lab, you are going to practice writing SQL statements. You should attempt the
Review Questions from the Introduction to Structured Query Language (SQL) chapter.
By the end of this lab, you should know how to write queries using MySQL.
You cannot save your queries in MySQL! Create a text file (using Notepad or Notepad++) and first
type your queries in the text file. I suggest that you use Notepad++ and save the text with the
extension .sql. You can copy and paste the queries from your text file into the putty window/phpmy
admin sql tab.
The following is the database structure that should result from you creating all your tables. Please
note that you cannot see the following schema in MySQL.
2
Task 1: Create your database tables
1. Download and save the text files, DBLab7_part2.sql file from Moodle. The file is
available together with your lab manual.
2. Login to the genesis server using the putty software or phpmyadmin
3. Now login to your MySQL database.
4. Open DBLab7_part2.sql file in Notepad++ examine the code and replace the
database name with your respective database name
5. Import the file DBLab7_part2.sql into phpmyadmin under your selected databased
using the import button and then clicking on the browse to locate and select the
file once the file is selected click the Go button
3
6. Alternatively, you can drag and drop your file into phpmyadmin.
7. You should get a message indicating that the import is successful.
Task 2: Questions
Now write the SQL commands in MySQL to do the following. Please note that type all
commands in a text file using Notepad++ and save the file as Lab7_part2.sql. You cannot
save your queries in MySQL.
1. Write the SQL statement that would be used to create a table the same as the
Customer table, and the name of the new table will be Customer2.
NOTE:
In the remaining exercises, you are going to use the CUSTOMER table and NOT the
CUSTOMER2 table
4
3. Write a query to display the list of all customers. You should display all the columns of
the customer table.
4. Write a query to select the customer code and the customer’s name from the
customer table. See the result below:
5. Generate a listing of all invoices that were created for the customer with CUS_CODE
as 10011. The result is shown in the figure below:
6. Generate a listing of invoice numbers of all invoices that were created on or before
16th January 2004.
5
NOTE:
When comparing dates in MS Access you will need to put the date values within #, for
example #22-Feb-06#. But in MySQL you will use single quotes and the following
format, e.g., ‘2006-02-22’.
7. Generate a list of product codes, descriptions, and prices of all products (use Product
table) that has the price between 15 and 25 dollars.
9. List all customers whose the first name starts with the letter ‘A.’ Use the LIKE operator
for the query.
10. Generate a listing of invoice numbers, the invoice date, the codes and names of
customers. You result should as follows:
HINT: See the lecture notes on how to join tables together. In this exercise, you need
to join the INVOICE and CUSTOMER tables.
11. Generate a list of customer code, first name, and last name from the customer table.
Sort the list by customer’s last name in decreasing order. [Use the ORDER BY clause]
12. Generate a list of customer code, first name, and last name from the customer table.
Sort the list by customer’s last name in increasing order.
6
13. Display the line totals of each line in invoice 1003.
14. What is the total sale the company has made so far? [Use the SUM aggregate function].
16. Display the invoice numbers and the invoice totals for all invoices. [HINT: use the
GROUP BY clause].