Top Banner
PRACTICAL 6 Topic Preview/Review Note to tutors- please ensure that you are using the ERD notation that is used in the textbook. Also for this particular tut/prac, feel free to write any part of answers on the whiteboard and discuss after students have had some time to attempt the questions themselves. The students should be reminded that (in theory) the tutorial questions should be completed PRIOR to coming to class, and the practical questions should be completed DURING class time. Please explain OUTER JOIN (page 176, or 168 for the 5th edition) when dealing with relational algebra (for Q1 (e). ) Please also explain GROUP BY clause in the SELECT statement in more details when dealing with the relevant questions. Operation Symbol Projection Selection Renaming Union Intersection Assignment Operation Symbol Cartesian product Join Left outer join Right outer join Full outer join Semijoin 1. Describe the relations that would be produced by the following relational algebra operations: a. hotelNo ( price>50 (Room)) c. hotelName (Hotel Hotel.hotelNo=Room.hotelNo ( price>50 (Room)) e. Hotel Hotel.hotelNo=Room.hotelNo ( price>50 (Room)) f. (Optional) guestName,hotelNo (Booking Booking.guestNo=Guest.guestNo Guest) hotelNo ( city='London' (Hotel)) Check the database schemas at page 166 (or 158 for the 5th edition). Part (a): Π hotelNo price>50 (Room)) This will produce a relation with a single attribute (hotelNo) giving the number of those hotels with a room price greater than £50. Part (c): Π hotelName (Hotel Hotel.hotelNo=Room.hotelNo price>50 (Room)) This will produce a join of Hotel and those tuples of Room with a price greater than £50. Essentially this will produce a relation containing all hotel names with a room price above £50.
12

PRACTICAL 6 - staff.cdms.westernsydney.edu.au

Oct 24, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

PRACTICAL 6  

Topic Preview/Review

Note to tutors- please ensure that you are using the ERD notation that is used in thetextbook. Also for this particular tut/prac, feel free to write any part of answers on thewhiteboard and discuss after students have had some time to attempt the questionsthemselves. The students should be reminded that (in theory) the tutorial questionsshould be completed PRIOR to coming to class, and the practical questions should becompleted DURING class time.

Please explain OUTER JOIN (page 176, or 168 for the 5th edition) when dealingwith relational algebra (for Q1 (e). ) Please also explain GROUP BY clause in theSELECT statement in more details when dealing with the relevant questions.

Operation Symbol ProjectionSelectionRenaming

UnionIntersectionAssignment

Operation Symbol Cartesian product

JoinLeft outer join

Right outer joinFull outer join

Semijoin

1. Describe the relations that would be produced by the following relational algebra operations:a. hotelNo( price>50(Room))

c. hotelName (Hotel Hotel.hotelNo=Room.hotelNo ( price>50(Room))e. Hotel Hotel.hotelNo=Room.hotelNo ( price>50(Room))f. (Optional)

guestName,hotelNo (Booking Booking.guestNo=Guest.guestNo Guest) hotelNo(

city='London'(Hotel))

Check the database schemas at page 166 (or 158 for the 5th edition).

Part (a): ΠhotelNo(σprice>50(Room))

This will produce a relation with a single attribute (hotelNo) giving the number of those hotelswith a room price greater than £50.

Part (c): ΠhotelName(Hotel Hotel.hotelNo=Room.hotelNo(σprice>50(Room))

This will produce a join of Hotel and those tuples of Room with a price greater than £50.Essentially this will produce a relation containing all hotel names with a room price above £50.

Page 2: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

Part (e): Hotel Hotel.hotelNo=Room.hotelNo(σprice>50(Room))

This will produce a (semi) join of Hotel and those tuples of Room with a price greater than £50.Only those Hotel attributes will be listed. Essentially this will produce a relation containing allthe details of all hotels with a room price above £50.

Part (f):

ΠguestName,hotelNo(Booking Booking.guestNo=Guest.guestNoGuest)÷ΠhotelNo(σcity='London'(Hotel))

This will produce a relation containing the names of all guests who have booked all hotels inLondon. We note that ÷ operation can be expressed in terms of some basic operations (see page177, or 169 for the 5th edition).

2. Generate the relational algebra for the following queries:a. List all hotels.b. List all single rooms with a price below £20 per night.c. List the names and cities of all guests.

3. What are the advantages and disadvantages of SQL?AdvantagesSatisfies ideals for database language(Relatively) Easy to learnPortabilitySQL standard existsBoth interactive and embedded accessCan be used by specialist and non-specialist.

DisadvantagesLack of orthogonality - many different ways to express some queriesLanguage is becoming enormous (SQL-92 is 6 times larger than predecessor)Handling of nulls in aggregate functionsResult tables are not strictly relational - can contain duplicate tuples, imposes an orderingon both columns and rows.Impedance mismatch - mixing programming paradigms with embedded access (see alsopage 272, or 264 for the 5th edition).

4. When two tables join together, they essentially become a combined larger table. In order that thejoining of the two tables makes sense, typically those two tables will have one or more attributes incommon, and the joining is largely done through equating the values of the respective commonattributes. Give an example of two entities that are related to each other, and derive the correspondingtwo tables linked via a foreign key. Show the SQL that would create all the two tables there.

5. Draw the ERD and RD for the database in the previous question.6. Assume table Album and table Song in the above question contain the records specified by the SQL

script below

INSERT INTO Album VALUES (1, 'Thriller', '1982-11-30'); INSERT INTO Album VALUES (2, 'Arrival', '1976-10-11'); INSERT INTO Song VALUES (1, 'Beat it', 1); INSERT INTO Song VALUES (2, 'Dancing Queen', 2); INSERT INTO Song VALUES (3, 'The girl is mine', 1);

Formulate a query in SQL that would list all the songs that are contained in an album published since1980. Which songs will the query display?

Page 3: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

Practical QuestionsThese practical exercises use the hotel tables that were constructed in earlier prac sessions, initiallydescribed at the end of Chapter 4 of the textbook on page 166 (or 158 for the 5th edition). Assume allprices quoted are in British Sterling Pounds (£).

7. Tutors please discuss briefly the use of SELECT statement, as those in the slides for lecture 5 (SQL -Data Manipulation Language).

8. Write SQL statements to create all foreign key relationships to the Booking and Room entities. Addthe following data to the hotel database.

Hotel:

hotelNo hotelName city5431 Ritz London5551 Marriott London3451 Marriott Paris1122 Riverside Berlin1342 Imperial London7782 Triumph Paris

Room: roomNo hotelNo type price

910 5551 Double 255915 5551 Family 345448 7782 Double 156110 1122 Double 39910 1342 Single 27448 1122 Suite 115

Guest:

guestNo guestName guestAddress9878987 Jackson Sydney, Australia4445555 Lewis London, England7777777 Luu Auckland, New Zealand3434343 Hamy London, England1000001 Jerry London, England9998888 Tony London, England

Booking:

hotelNo guestNo dateFrom dateTo roomNo1122 9878987 23-Feb-20 28-Feb-20 1101122 7777777 2-Mar-20 10-Mar-20 1101342 9878987 3-Mar-20 9105551 3434343 2-Mar-20 10-Mar-20 915

9. Write an SQL query -- using the SELECT statement described in lectures -- to list full details of allhotels in London.

10. Write an SQL query to list all the names and addresses of guests who live in London, alphabeticallyordered by name.

Page 4: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

11. Use UPDATE statement to change the guest name Luu to Luise in the Guest table (note: Strings arecase sensitive). For SQL syntax, you may check the following links.

http://www.w3schools.com/sql/sql_update.asphttp://www.techonthenet.com/sql/update.php

12. Use the DELETE statement to remove guest Jerry from the Guest table.http://www.techonthenet.com/sql/delete.phphttp://www.w3schools.com/sql/sql_delete.asp

13. Write an SQL query to list bookings for which no dateTo has been specified.14. Optional: SQL Server Visual Track 3 - Simple Queries

In this part we will "visually" query about the records/data of the individual tables, and update ordelete a record. We will also show how to fetch the SQL scripts to achieve these visually designedtasks.

a. We first assume that we have already created the tables Hotel, Room, Guest and Booking,and have already filled these tables with the data specified earlier. Right click the iconNew Query as in

to make sure both Query Designer and SQL Editor are checked.

Page 5: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

b. We will first display all the records of table Hotel. Click the icon New Query to get

Then click top menu item Query to select Design Query in Editor.... When the screen

pops up, select Hotel because that is the table we want to query,

Page 6: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

then click Add and then Close to reach

Page 7: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

c. Select the field/s you wish to query (along with potential filters),

then you will see the equivalent SQL at the bottom. Click OK to accept the query designand to reach

Page 8: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

Now click ! Execute to get the query results

d. We can also modify the Query Designer by modifying the SQL script inside. Forinstance, once we reached (earlier on)

we can also edit the SQL script there to include a clause WHERE city='London' as in

Page 9: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

Right click on the SQL pane and select Verify SQL Syntax, and you will see that the extraclause is automatically reflected on the Query Designer as in

Page 10: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

Click OK and then ! Execute to obtain the results

e. To update or delete a record in a table such as Hotel, simply right click on the tablename in the Object Explorer pane and select Edit Top 200 Rows and update or deleterecords there

f. For the (previous) query "Write an SQL query to list all the names and addresses ofguests who live in London, alphabetically ordered by name", we can also do it with theQuery Designer. We can simply set the Query Designer to the following form

Page 11: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

to get the query result as

15. The above Visual Track 3 shows that one can let the SQL Server to generate SQL queries fromour visual design, and this will be handy for anyone who may be having difficulty in formulatingthe queries in SQL directly. This should be possible for not too complicated queries, and therefore formost of our queries in this unit.

Additional Exercises

α. For the Hotel database (of 4 tables) in this practical, do the following database queries in SQL. Foreach query, show the query result in a (readable) screen shot along with the SQL statement. Before

Page 12: PRACTICAL 6 - staff.cdms.westernsydney.edu.au

finalising your answers, refresh your database by reloading prac6-sql-data.sql, if necessary, sothat the results are uniform across the board.

a. List the hotel names for all the hotels in the city Paris.

b. List all the hotel numbers for all those hotels which contain (at least) a room of Doubletype of a price greater than or equal to £150 per night.

c. For the Guest table, use an SQL statement to change all the address London, England toLondon, UK. Then list all the records in the Guest table.

SELECT * FROM Guest; UPDATE (the rest of line is your code) SELECT * FROM Guest;

d. For the Booking table, use an SQL statement to delete all the records that are null eitherin dateFrom or dateTo or both. Then list all the remaining records in the Booking table.

SELECT * FROM Booking; DELETE (the rest of line is your code) SELECT * FROM Booking;

β. Describe the relation, in business terms rather than in any database terminology, that is produced bythe following relational algebra operation

hotelName, price (Hotel Hotel.hotelNo=Room.hotelNo ( type='Double'(Room))

γ. For the query "list all the distinctive hotel names for all the hotels in the city Paris", write it as arelational algebra.

ACKNOWLEDGEMENT: This document may have included excerpts from the prescribed textbook by Thomas Connolly and Carolyn Begg, and isalso based on an earlier delivery some years back at WSU by Dr Eshan Vossough who may have in turn made use of other contributors' work.

Compiled and typeset by Zhuhan Jiang @2021.