0% found this document useful (0 votes)
49 views

Lab 7 - Part2 Is222

The document provides instructions for a lab on SQL and MySQL. Students will create tables, insert data, and write queries to retrieve and manipulate data. The goals are to learn basic SQL commands and gain practice writing SQL statements. Students will complete a series of review questions that involve creating tables, inserting data, joining tables, and using aggregate functions.

Uploaded by

miliana vuli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Lab 7 - Part2 Is222

The document provides instructions for a lab on SQL and MySQL. Students will create tables, insert data, and write queries to retrieve and manipulate data. The goals are to learn basic SQL commands and gain practice writing SQL statements. Students will complete a series of review questions that involve creating tables, inserting data, joining tables, and using aggregate functions.

Uploaded by

miliana vuli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

IS222 Lab7 part 2

Introduction to Structured Query Language

Using MySQL

Approximate Time Required: 2 hours

Objectives:

• To learn basic SQL commands


• To practice writing SQL commands in MySQL

Pre-Lab Preparation:

Before attending this lab, it is a requirement to have done the following:

You should have completed all previous labs


You should have read the lab (this document).
Complete all your readings
You should have looked at the following reference links to familiarize yourself with
the basics of the SQL syntax (for DML/DDL) when working with MySql. You also
can use these during the lab for reference:
1) http://www3.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Beginner.ht
ml
(provides a good summary of pivotal SQL commands and its syntax)
2) https://dev.mysql.com/doc/refman/8.0/en/sql-syntax.html (this link provides
the entire reference manual for describing the syntax for the SQL statements
supported by MySQL. This is the complete guide for you to locate details regarding
the syntax of any type of DDL/DML SQL query supported in MySQL.

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.

Overall Goal of this Tutorial

By the end of this lab, you should know how to write queries using MySQL.

Saving the Queries

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.

8. Do you see all the tables?

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.

2. Insert the following data in 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.

Your results should be the same as shown in the figure below:

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.

8. What is the lowest price of all products? See aggregate functions.

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].

15. What is the average sale for the company?

16. Display the invoice numbers and the invoice totals for all invoices. [HINT: use the
GROUP BY clause].

You might also like