2017-2 COMP3278 Assignment 2 SQL
2017-2 COMP3278 Assignment 2 SQL
2017-2 COMP3278 Assignment 2 SQL
The schemas are shown below (The underlined attributes represent the primary key of
the relation):
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.
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.
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)
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:
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.]
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.
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