You are on page 1of 4

6.17. Consider the AIRLINE relational database schema shown in Figure 5.8, which was described in Exercise 5.12.

Specify the following queries in relational algebra:

a. For each flight, list the flight number, the departure airport for the first
leg of the flight, and the arrival airport for the last leg of the flight. DEPARTURE = Flight_numberMIN Leg_number (FLIGHT_LEG) Departure_airport = Flight_number,Departure_airport_code (DEPARTURE * FLIGHT_LEG ) ARRIVAL = Flight_numberMAX Leg_number (FLIGHT_LEG) Arrival _airport = Flight_number, Arrival_airport_code (ARRIVAL * FLIGHT_LEG ) RESUL<- Departure_airport * Arrival _airport DEPARTURE = Flight_numberMIN Leg_number (FLIGHT_LEG) ARRIVAL = Flight_numberMAX Leg_number (FLIGHT_LEG)
RESUL<-Flight_number,Departure_airport_code,Arrival_airport_code((DEPARTUREARRIVAL)*AIRPORT)

b. List the flight numbers and weekdays of all flights or flight legs that
depart from Houston Intercontinental Airport (airport code 'IAH') and arrive in Los Angeles International Airport (airport code 'LAX'). DEP_HOUS <- Departure_airport_code = 'IAH'(FLIGHT_LEG) ARR_LA <- Arrival_airport_code = 'LAX'(FLIGHT_LEG) HOUSTOLA <- DEP_HOUS * ARR_LA

RESULT <- Flight_number, Weekdays(HOUSTOLA* FLIGHT)

c. List the flight number, departure airport code, scheduled departure


time, arrival airport code, scheduled arrival time, and weekdays of all flights or flight legs that depart from some airport in the city of Houston and arrive at some airport in the city of Los Angeles. DEP_HOUS <- Departure_airport_code = 'IAH'(FLIGHT_LEG) ARR_LA <- Arrival_airport_code = 'LAX'(FLIGHT_LEG)

HOUSTOLA <- DEP_HOUS * ARR_LA RESULT <- Flight_number, Departure_airport_code, Scheduled_departure_time, Arrival_airport_code,
Scheduled_arrival_time, Weekdays

(HOUSTOLA* FLIGHT)

d. List all fare information for flight number 'CO197'.


RESULT <- Flight_number = 'co197' (FARE)

e. Retrieve the number of available seats for flight number 'CO197' on


'1999-10-09'. INFORM <- Flight_number='CO197' AND Date='1999-10-09' (LEG_INSTANCE) RESULT <- Number_of_available_seats(INFORM) 6.18. Consider the LIBRAY relational database schema shown in Figure 6.14, which is used to keep track of books, borrowers, and book loans. Referential integrity constraints are shown as directed arcs in Figure 6.14, as in the notation of Figure 5.7. Write down relational expressions for the following queries:

a. How many copies of the book titled The Lost Tribe are owned by the
library branch whose name is 'Sharpstown'?

TLT <- Title = 'The Lost Tribe'(BOOK) SHARP <- Branch_name='Sharpstown'(LIBRARY_BRANCH) RESULT <- No-of-copies ( ( TLTSHARP )* ( BOOK_COPIES ) )

b. How many copies of the book titled The Lost Tribe are owned by each
the library branch? TLT <- Title = 'The Lost Tribe'(BOOK)
RESULT <-

Title, Branch-name, No-of-copies ((TLT LIBRARY_BRANCH)*(BOOK_COPIES))

c. Retrieve the name of all borrowers who do not have any books checked
out. NULL_LOAN <- Date_out = 'null'(BOOK_LOANS) RESULT <- Name (NULL_LOAN * BORROWER)

d. For each book that is loaned out from the 'Sharpstown' branch and
whose Due_date is today, retrieve the book title, the borrower's name, and the borrower's address. SHARPS <- Branch_name = 'Sharpstown'(LIBRARY_BRANCH) DUE_TODAY <- Due_date=TODAY(BOOK_LOANS) DUE_BOOK <- ( SHARPS BOOK ) * DUE_TODAY RESULT <- Title, Name, Address (DUE_BOOK * BORROWER)

e. For each library branch, retrieve the branch name and the total number
of books loaned out from that branch.

CNT_LOAN(branched, cnt) <- Branch_id COUNT Book_id(BOOK_LOANS) RESULT <- Branch_name, cnt (CNT_LOAN*LIBRARY_BRANCH)

f. Retrieve the names, addresses, and number of books checked out for all
borrowers who have more than five books checked out. CNT_LOAN(Cno, cnt) <- Card_no COUNT Book_id (BOOK_LOANS) MORE <- cnt>5(CNT_LOAN) RESULT <- Name, Address, cnt ( BORROWER MORE)

g. For each book authored (or coauthored) by Stephen King, retrieve the
title and the number of copies owned by the library branch whose name is Central. CENTRAL <- Branch_name='Central'(LIBRARY_BRANCH) AUTH <- Author_name='stephen king' (BOOK_AUTHORS) STEPHEN_BOOK <- AUTH * BOOK
RESULT <- Title, No_of_copies ((BOOK_COPIES * CENTRAL) * STEPHEN_BOOK)

You might also like