School of Information Management and Systems

Total Page:16

File Type:pdf, Size:1020Kb

School of Information Management and Systems

IMS1907 Tutorial 11, Summer Semester, 2004/5

Monash University School of Information Management and Systems IMS1907 Database Systems Summer Semester 2004/5 Tutorial 11 – Structured Query Language (SQL)

Tutorial Objectives: - to further your understanding of structured query language and how it is used to write SQL statements used to create databases and tables, populate the tables and query the data contained in those tables - to apply the understanding of SQL to create a simple database using MySQL – an open source DBMS

Tutorial Resources: - Tutorial 11 handout – Structured Query Language (SQL) - IMS1907 Lectures 9, 10, 11 SQL – DDL, DML

Tutorial Task: This week we will develop your understanding of SQL by creating a simple database, populating it, and generating queries against the data. You will create the database used in the lectures that stores information about boats and the sailors that reserve those boats. Your tutor will guide you through the structure of some queries but you should attempt them yourself – they are a useful aid to your understanding and will assist you with the assignment. Use the lecture notes and the MySQL Reference Manual to assist you with the creation of your database. 1. Create a database and name it according to the nature of the data it contains – ‘boats’ or ‘reservations’ or ‘boat_reservations’ or something like that (Note: It is useful when working in the labs to name your databases with your initials first to distinguish it from other databases that may be stored on the same machine eg DG_ boats would be different from RS_boats. It can also be useful to DROP your database once you have completed an exercise so that no one else can see it!)

2. Create the following tables for your database and populate them with the data values shown in the tables. Use SHOW TABLE and SELECT statements to verify that that your tables have been created and populated correctly.

sailor +-----+------+------+-----+ | sid | sname | rating | age | +-----+------+------+-----+ | 22 | Dustin | 7.00 | 45 | | 29 | Brutus | 1.00 | 33 | | 31 | Lubber| 8.00 | 55 | | 32 | Andy | 8.00 | 25 | +-----+------+------+-----+ boat

Page 1 of 3 IMS1907 Tutorial 11, Summer Semester, 2004/5

+------+------+------+------+ | bid | bname | colour | cost | +------+------+------+------+ | 101 | Voyager | blue | 10.00 | | 102 | Voyager | red | 20.00 | | 103 | Clipper | green | 10.00 | | 104 | Shark | red | 30.00 | +------+------+------+------+ reservation +----+------+------+ | bid | sid | day | +----+------+------+ | 101 | 22 | 1998-10-10 | | 102 | 22 | 1998-10-10 | | 102 | 31 | 1998-10-11 | | 103 | 22 | 1998-08-10 | | 103 | 31 | 1998-06-11 | | 104 | 22 | 1998-07-10 | | 104 | 31 | 1998-12-11 | +-----+------+------+

3. Use the ALTER TABLE statement to add a column named ‘cost’ to the ‘boat’ table. This column should be able to store a dollar amount from $0.00 to $999.99. Use SELECT statements to verify that that your table has been modified correctly.

4. Use the UPDATE statement to add the following values for the new column of the records. Use a SELECT * statement to verify that that your table has been updated correctly.

+----+------+ | bid | cost | +----+------+ | 101 | 10.00 | | 102 | 20.00 | | 103 | 10.00 | | 104 | 30.00 | +----+------+

5. Use a SELECT statement to list the costs for each boat. Use a DISTINCT statement to remove any duplicate values from the list.

6. Write a query to list the details of all boats that cost $10 to reserve. Modify the query to list the details of all boats that cost $10 to reserve and are green in colour. Modify the query again to list the details of all boats that cost more than $10 to reserve. 7. Write a query to display the number, name, cost, the GST charge (at a rate of 22%), and cost including GST for each boat.

Page 2 of 3 IMS1907 Tutorial 11, Summer Semester, 2004/5

8. Write a query to display the average age of sailors.

9. Write a query to display the sum of the cost of boat reservations.

10. Write a query to display the total number of reservations.

11. Write a query to display the total number of reservations. Made for each boat ID number

12. Write a query to display the boat ID number, name, and total number of reservations made for each boat.

13. Write a query to display the boat ID number, name, and date of reservations made for each boat.

14. Write the previous query so that the records are in date order.

15. Write a simple query to show the total reservation costs for each boat. Rewrite the simple query to arrange the result in order of the cost. Rewrite the query again using a join to show the name of each boat as well as the total costs.

16. Write a query to display the boat number, boat name, sailor name and the reservation date for each reservation. Play with the way that the results are displayed by ordering on different columns.

17. Add the following record to the boat table using an INSERT statement

+-----+------+------+------+ | bid | bname | colour | cost | +-----+------+------+------+ | 105 | Titanic | ice blue | 50.00 | +-----+------+------+------+ Now write a query using an outer join to list all boats showing the days they have been reserved or NULL if they have not yet been reserved. Now rewrite the query to show just those boats that have not been reserved.

Once you have completed these exercises, make up your own queries and test them see if you get the answers you expected.

Page 3 of 3

Recommended publications