Vous êtes sur la page 1sur 5

COMP3278 Introduction to Database Management Systems

Assignment 2 - SQL & Application development


Due Date: 9 Mar, 2018 5:00pm

The schemas are shown below (The underlined attributes represent the primary key of
the relation):

1. Member (memberID: INT(11), name: VARCHAR (30), referrer:


INT(11))
FOREIGN KEY: (referrer) referencing Member(memberID)
2. Driver (memberID: INT(11), accountNO: VARCHAR(30), licence:
INT(11))
FOREIGN KEY: (memberID) referencing Member(memberID)
3. Passenger (memberID: INT(11), creditCardNO: VARCHAR(16))
FOREIGN KEY: (memberID) referencing Member(memberID)
4. Vehicle (carPlate: VARCHAR(8), carType: ENUM('normal',
'commercial','7-seaters'), brand: VARCHAR(30), owner: INT(11))
FOREIGN KEY: (owner) referencing Driver(memberID)
5. Trip (tripID: INT(11), passengerID: INT(11), startDate: DATE,
startTime: TIME, source: VARCHAR(30), destination:
VARCHAR(30), carPlate: VARCHAR(8), fare: DECIMAL(6,1))
FOREIGN KEY: (carPlate) referencing Vehicle(carPlate)
(passengerID) referencing Passenger(memberID)

Requirements
Part 1 [15%] Build the database using MySQL.
a. Using the schemas above, define tables with appropriate constraints. NULL value is
NOT allowed except for Member(referrer). Note that storage engine InnoDB
needs to be specified explicitly since it is not the default.
b. Store the corresponding table definition commands in a sql file named "tables.sql"
(Please include also the building of referential constraints in the .sql file, make sure
that the files can correctly build the necessary tables and constraints when import to
another database).

Questions designed by Mr. Wu Tien-Hsuan for students in HKUCS COMP3278. For other uses, please email: thwu@cs.hku.hk
Part 2 [70%] Answer queries and display results.
Related files in a2.zip: config.php, data.sql, q0.php, q1.php

Please build q1.php to q7.php, q8a.php, q8b.php, q8c.php which solve the
following queries and display the results in a web browser. Sample data is provided in
data.sql. Table names and attribute names must be consistent with the names
provided in relation schemas.

Q0. [sample] Display memberID, name for all members.


• Please fill in the information in config.php. Do NOT submit this file.

Q1. [5%] Display the name and creditCardNO of passengers with creditCardNO
starting with 4384.
• Please fill in your name as instructed in the template file provided.

Q2. [5%] Display the memberID, passenger name of the passengers who have not
taken any trip.

Q3. [5%] Display the memberID, name of user(s) who is both a driver and a
passenger.

Q4. [5%] Display the locations (source/destination) where driver with


memberID = 3 has either picked up passengers from or dropped off passengers at.
Combine the sources and destinations in one row with column name “location”. Each
location should appear only once. List the locations in alphabetical order.

Q5. [10%] For each driver, display the memberID, name, licence and the total
value of fare earned.

Q6. [10%] For each passenger that has taken more than one trip, display the tripID,
passengerID, source, destination, fare of the trips, excluding the trip that
the passenger paid the lowest amount of fare. Order the result first by descending
order of passengerID, then by descending order of tripID if the passengerIDs
are equal.

Questions designed by Mr. Wu Tien-Hsuan for students in HKUCS COMP3278. For other uses, please email: thwu@cs.hku.hk
Q7. [10%] For each referrer, show
• Referrer’s memberID (referrer)
• memberID, name and the total fare of the referee who has spent the most
amount in terms of total fare.
• Order the result by ascending order of referrer
• For referrer whose referees have not taken any trip, you may display the total
fare = 0 (and display any of his/her referee) or ignore such referrer (omit that
row).

Q8a. [5%] Display the drop-down menu showing the names of drivers. Add a submit
button. After you click the submit button, the page will be redirected to q8b.php

Q8b. [5%] Display the passenger name, startDate and startTime of the trip that
the selected driver from q8a has served. Make the name of the passenger as a
hyperlink to q8c.php?driver=x&passenger=y (x is the memberID of the driver and y is
the memberID of the passenger)

Q8c. [10%] Display the passenger name, startDate, startTime, source,


destination, fare of the trips that the driver selected in q8a served passenger
selected in q8b.

Part 3 [15%] Use Google Maps API with MySQL


Related files in a2.zip: config.php, map.sql, map.html, route_info.php,
location_info.php

Questions designed by Mr. Wu Tien-Hsuan for students in HKUCS COMP3278. For other uses, please email: thwu@cs.hku.hk
Modify and submit map.html, route_info.php, location_info.php in order to
complete the tasks in this part. We also provide the file map.sql for you as the data
used in this task. The tables constructed by map.sql are:

Location (id, name, address, lat, lng)


name – The name of the location, which matches source / destination in the Trip
table. You may assume the locations are the same if Location.name = Trip.source;
same applies for destination.
lat, lng – Latitude and longitude.
Note that id is just a key and we will not use it in our application.

Route (tripID, seq, lat, lng) Foreign key: (tripID) referencing Trip(tripID)
In a trip (identified by tripID), the GPS tracker will record some locations and store
them in database. seq serves as the order (sequence) of the records in one trip.
Please import map.sql to your existing database. Tables will be constructed and data
will be inserted if map.sql is imported correctly.
[DON’T use map.sql as a template for part 1. There are some constraints missing.]

Task 1 – map.html [3%]


The code is adapted from
https://developers.google.com/maps/documentation/javascript/mysql-to-maps and
https://developers.google.com/maps/documentation/javascript/shapes. Study the code
together with the instructions on the link provided to see how Google Maps API can
be used with MySQL and PHP.
Make the following changes:
• Locate variable map in function initMap(). Go to Google Maps and find a
suitable coordinate to display Hong Kong. Change center to the coordinate
you found (latitude, longitude).
• Adjust the value of zoom so that HK Island, New Territories, Lantau Island
can be seen clearly.
(Optional) Obtain your api key:
• We have created a default api key for this assignment. You can use your own
key.
• Please refer to
https://developers.google.com/maps/documentation/javascript/adding-a-
google-map#3_api for the steps to obtain api key. Remember to enable Google
Maps JavaScript API service.
• Go to the bottom part of the code (right before </body>) and update the key.

Questions designed by Mr. Wu Tien-Hsuan for students in HKUCS COMP3278. For other uses, please email: thwu@cs.hku.hk
Task 2 – location_info.php[6%]
This code returns the source and the destination of the trip so markers are added to the
map.
Make the following changes:
• $query – Find every column in the Location table for the location that is the
source of trip (having the same name) with tripID = 1.
• $query2 – Find every column in the Location table for the location that is
the destination of trip (having the same name) with tripID = 1.

Task 3 – route_info.php [6%]


This code returns an array of coordinates. A line will be drawn on the map by linking
those coordinates.
Make the following change:
• $query – Get the lat, lng of the route with tripID = 1. Order the results
by ascending order of seq.

Visit map.html and see if the source, destination and route are shown on the map. You
can open the console by pressing F12 (Microsoft Edge, Google Chrome) to see the
error message if there is any.

Hand in
1. Please compress "tables.sql" from part 1, 10 PHP files from part 2, and 3 files from
part 3 into one zip file and use your student number to name it (e.g. 3035123456.zip).
You should have exactly 14 files in the folder.
2. Please submit this zip file through our Moodle system before the deadline.

Important Notes
1. Programs/queries that cannot be run in our system will receive zero mark.
2. Tutorials 2 and 3 contain important information about how to install and use PHP
and MySQL. Please refer to these tutorials if necessary.
3. MySQL does not implement a few SQL keywords that we taught in the lecture.
Please test the queries in MySQL thoroughly before submitting the assignment.
4. For sample query results, you may refer to
http://i.cs.hku.hk/~thwu/c3278/a2/index.html
5. The data that we use to grade your assignment may be different from the sample
data.

Questions designed by Mr. Wu Tien-Hsuan for students in HKUCS COMP3278. For other uses, please email: thwu@cs.hku.hk

Vous aimerez peut-être aussi