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_numberξMIN Leg_number (FLIGHT_LEG) Departure_airport = πFlight_number,Departure_airport_code (DEPARTURE * FLIGHT_LEG ) ARRIVAL = Flight_numberξMAX Leg_number (FLIGHT_LEG) Arrival _airport = πFlight_number, Arrival_airport_code (ARRIVAL * FLIGHT_LEG ) RESUL<- Departure_airport * Arrival _airport حل أخر DEPARTURE = Flight_numberξMIN Leg_number (FLIGHT_LEG) ARRIVAL = Flight_numberξMAX Leg_number (FLIGHT_LEG) RESUL<-πFlight_number,Departure_airport_code,Arrival_airport_code((DEPARTURE×ARRIVAL)*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 ( ( TLT×SHARP )* ( 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)