CS 352 Database Management Systems TERM PROJECT PROJECT DESIGN REPORT Mar 28, 2011 Airline Company Data Management System Group 43 Ahmet Alp Balkan 20800978 a_balkan@ug Emre Ekmekçi 20800227 e_ekmekci@ug http://code.google.com/p/cs352-project/ 1 Ecem Ünal 20702525 ecem_u@ug TABLE OF CONTENTS 1. Project Introduction & Requirements .................................................................... 3 1.1. Problem Description .......................................................................................................... 3 1.1. Project Requirements ........................................................................................................ 3 1.2. Limitations ............................................................................................................................. 4 2. Database Schema............................................................................................................ 5 2.1. Entities .................................................................................................................................... 5 2.2. Relations ................................................................................................................................. 7 2.3. Views ........................................................................................................................................ 8 2.3.1. Route Listing View ..................................................................................................................... 9 2.4. Checks ................................................................................................................................................. 9 2.5. Indexes ............................................................................................................................................... 9 3. Implementation Details ......................................................................................................10 4. Sample Reports ......................................................................................................................11 Report: Fullness Report of Üpcoming Flights .......................................................................... 11 Report: Monthly Revenue of Airline Company ........................................................................ 11 Report: Flight Passenger Report ................................................................................................... 12 Report: Average Staff Salary Report ............................................................................................ 12 5. User’s Guide & UI Mockups ................................................................................................13 5.1. Login Page ...................................................................................................................................... 13 5.2. Signup Page ................................................................................................................................... 13 5.3. Reservation and Payment System ........................................................................................ 15 5.4. Sales Department Check-in Screen...................................................................................... 18 5.5. City Management ........................................................................................................................ 19 5.5. Airport Management ................................................................................................................. 20 5.6. Fleet (planes) management .................................................................................................... 21 5.7. Route management .................................................................................................................... 22 5.8. Flight Management .................................................................................................................... 23 5.9. Staff Management ....................................................................................................................... 24 Appendix A: ER Diagram of the Project 2 1. Project Introduction & Requirements 1.1. Problem Description In this project, we will design a database management system for airline companies. The database is necessary for airline companies to keep track of their planes, flights, crew and sales/reservations. Both customers and employees of the airline company will use this database system effectively: customers will be able to make reservations to flights and get tickets using the web-based application, sales staff will use the system to make sales to customers, and the executives will use it to manage cities, airports, planes, flights, all types of employees (sales staff and crew) and analyse the current overall situation of the company. 1.1. Project Requirements Our project is about a database management system of an airline company, which holds all the information about flights, bookings, staff and the inventory. The project is supposed to be used by sales department staff, executives and the customers. Üse cases of sales department staff: Make check-ins of on behalf a customer at airport. Add luggage details during check-in. Print out travel pass. Üse cases of executives. Create/delete new cities. Create/delete new airports. Add new planes and remove them. 3 Add new routes to fly and remove them. Edit staff details (salary, type etc.) and remove them. Create new flight with their route, date and decide on flight fare. Cancels or edits the flights. Associates flight crew (pilots, hosts etc.) with flights. See reports about flights. See reports about payments, tickets sold over time. See reports about staff and their salaries. Üse cases of customers over web interface: Create account with name and password etc. Sign in to the system / sign out. List flight between two cities or two airports of given date. Show flights, their departure date, airplane model and fare Make a booking and buy the ticket. 1.2. Limitations Since this project is prototype of a airline company database system, it does not include a few usual requirements in a real-world airline company system. For instance: Ticket fares do not have children/adult difference. Ticket fares do not have economy/business/first class difference. The only payment method is credit card. Seat numbers are not visually located in airplanes. They are just integers. There are no discount operations for customer loyalty tracking. In this system all pilots can fly all kinds of planes. 4 2. Database Schema 2.1. Entities Includes DDL queries for creation of entities. City CREATE TABLE city ( id INTEGER NOT NULL, name VARCHAR(100) NOT NULL, PRIMARY KEY (id) ); Primary Key: id Airport CREATE TABLE airport ( id INTEGER NOT NULL, code VARCHAR(4) NOT NULL, name VARCHAR(100) NOT NULL, city INTEGER NOT NULL, PRIMARY KEY (id), FOREIGN KEY (city) REFERENCES city(id) ON DELETE CASCADE ); Primary Key: id Candidate Key: (id), (code), (name) Foreign key: city on city.id Route CREATE TABLE route ( id INTEGER NOT NULL, departure INTEGER NOT NULL, destination INTEGER NOT NULL CHECK (departure<>destination), duration INTEGER DEFAULT 0, PRIMARY KEY (id), FOREIGN KEY (departure) REFERENCES airport(id) ON DELETE CASCADE, FOREIGN KEY (destination) REFERENCES airport(id) ON DELETE CASCADE ); Primary Key: id Candidate Key: (departure, destination) Foreign Key: departure and destination on airport.id 5 Plane CREATE TABLE plane ( id INTEGER NOT NULL, name VARCHAR(100), capacity INTEGER NOT NULL, model VARCHAR(100), PRIMARY KEY (id) ); Primary Key: id Candidate Key: (id), (name) Ground_Staff CREATE TABLE ground_staff ( id INTEGER NOT NULL, salary FLOAT DEFAULT 0, name VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL, type ENUM('EXEC', 'SALES') DEFAULT 'SALES', date_joined DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE (name) ); Primary Key: id Candidate Key: (id), (name) Flight_Staff CREATE TABLE flight_staff ( id INTEGER NOT NULL, salary FLOAT DEFAULT 0, name VARCHAR(100) NOT NULL, type ENUM(‘PILOT’, ‘HOSTESS’) NOT NULL, date_joined DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE(name) ); Primary Key: id Candidate Key: (id), (name) Customer CREATE TABLE customer ( id INTEGER NOT NULL, name VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL, phone VARCHAR(100) NOT NULL, PRIMARY KEY (id), UNIQUE name ); 6 Primary Key: id Candidate Key: (id), (name) Flight CREATE TABLE flight ( id INTEGER NOT NULL, flight_number VARCHAR(10) NOT NULL, departure_date DATETIME NOT NULL CHECK (departure_date>NOW()), fare FLOAT DEFAULT 0, route INTEGER NOT NULL, plane INTEGER NOT NULL, PRIMARY KEY (id), FOREIGN KEY (route) REFERENCES route(id) ON DELETE CASCADE, FOREIGN KEY (plane) REFERENCES plane(id) ON DELETE CASCADE ); Primary Key: id Candidate Key: (id), (flight_number), (departure_date, route), (departure_date, plane) Foreign key: route on route.id, plane on plane.id Payment CREATE TABLE payment ( transaction_id INTEGER NOT NULL, date DATETIME NOT NULL, amount FLOAT DEFAULT 0, PRIMARY KEY (transaction_id) ); Primary Key: transaction_id Luggage CREATE TABLE luggage ( id INTEGER NOT NULL, weight FLOAT DEFAULT 0, PRIMARY KEY (id) ); Primary Key: id 2.2. Relations Includes DDL statements for creation of relations between entities. Flight Crew CREATE TABLE flight_crew ( flight_id INTEGER NOT NULL, staff_id INTEGER NOT NULL, PRIMARY KEY (flight_id, staff_id), FOREIGN KEY (flight_id) REFERENCES flight(id) ON DELETE CASCADE, FOREIGN KEY (staff_id) REFERENCES flight_staff(id) ON DELETE CASCADE ); 7 Primary Key: (flight_id, staff_id) Foreign key: flight_id on flight.id, staff_id on flight_staff.id Booking CREATE TABLE booking ( id INTEGER NOT NULL, seat INTEGER NOT NULL, customer_id INTEGER NOT NULL, booking_date DATETIME NOT NULL CHECK (booking_date >= NOW()), flight_id INTEGER NOT NULL, payment INTEGER, PRIMARY KEY (id), FOREIGN KEY (customer_id) REFERENCES customer(id) ON DELETE CASCADE, FOREIGN KEY (flight_id) REFERENCES flight(id) ON DELETE CASCADE, FOREIGN KEY (payment) REFERENCES payment(transaction_id) ); Primary Key: id Candidate Key: (customer_id, booking_date), (id) Foreign Key: customer_id on customer.id, flight_id on flight.id, payment on payment.transaction_id. Booking Luggage CREATE TABLE booking_luggage ( booking_id INTEGER NOT NULL, luggage_id INTEGER NOT NULL, PRIMARY KEY (booking_id, luggage_id), FOREIGN KEY (booking_id) REFERENCES booking(id) , FOREIGN KEY (luggage_id) REFERENCES luggage(id) ); Primary key: (booking_id, luggage_id) Foreign key: booking_id on booking.id, luggage_id on luggage.id 8 2.3. Views 2.3.1. Route Listing View Lists departure and destination airports of routes separated by a dash (-) along with their id. Used for representational listings in the GUI. CREATE VIEW route_listing AS (SELECT r.id, CONCAT(f.name, ‘-‘, t.name) AS route_name FROM route r, airport f, airport t WHERE r.departure=f.id AND r.destination=t.id); Example Result SELECT * FROM route_listing; Id 1 2 3 Name Esenboga Airport- Henri Coanda Airport Hezarfen Airport-Sabiha Gokcen Charles de Gaulle Airport-John F. Kennedy Airport 2.4. Checks Route Departure and arrival fields can not be the same. Flight Departure_date field can not be in the past. Booking Booking_date field can not be in the past. Cannot make booking to a past date. 2.5. Indexes city CREATE INDEX city_name ON city(name(5)) USING BTREE route CREATE INDEX route_points ON route(departure,destination) USING HASH; flight CREATE INDEX route_flights ON flight(route) USING HASH; CREATE INDEX route_dates ON flight(flight_date) USING BTREE; #range queries 9 ground_staff CREATE INDEX staff_credentials ON ground_staff(name,passowrd) USING HASH; customer CREATE INDEX customer_credentials ON customer(name,passowrd) USING HASH; 3. Implementation Details In our project we decided to use a MAMP stack, which includes Apache 2 Web Server with mod_php (5.3.5) and MySQL 5.5 and runs on Mac OS X. We used Sequel Pro to maintain database with a GÜI. Our database details: Host: 127.0.0.1 Üser: root Pass: root Database: airline In the frontend, we will be using HTML+CSS and in the backend we will be using PHP. System depends on system time of running machine. Since this project is a proof of concept, in the real implementation there will not be many error checks, therefore the system is just stable enough to work on development environment and prone to errors. 10 4. Sample Reports Report: Fullness Report of Upcoming Flights Shows ratio of flight tickets sold (or reserved) to overall seats in each plane of upcoming flights. Parameters $day: number of future days. Query SELECT flight_number, flight_date, s.capacity as capacity, s.sold as sold FROM flight b, s.sold/s.capacity*100 as percentage (SELECT COUNT(*) as sold from booking b WHERE b.flight_id=b.id) AS s WHERE flight_date BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL $day DAY) Example Result Flight_number Flight_date TK 122 2011-03-28 04:20 TK 124 2011-03-29 00:10 Capacity 100 150 Sold 20 35 Percentage 20 23 Report: Monthly Revenue of Airline Company Shows how much revenue Airline Company has been earned in a year. Parameters $year: given year e.g. 2011 Query SELECT r.m as month, r.sal as salaries, r.sell as sellings, r.sell-r.sal as revenue FROM (SELECT DISTINCT(MONTH(date)) as m, SUM(amount) as sell, gs.total+fs.total as sal, ps.total as sell FROM payment (SELECT SUM(salaty) AS total FROM ground_staff) gs, (SELECT SUM(salary) AS total FROM flight_staff) fs, WHERE YEAR(date)=$date) r ORDER BY month asc; Example Result Month 3 4 5 Salaries 10000 18000 20000 Sellings 20000 35000 43000 11 Revenue 10000 17000 23000 Report: Flight Passenger Report Generates a report shows the passengers along with their seats for a given flight number. Parameters $flight_number: e.g. TK 123 Query SELECT b.seat, c.name FROM flight f, booking b, customer c WHERE f.flight_number=’$flight_number’ and b.flight=f.id and b.customer=c.id ORDER BY b.seat asc; Example Result Seat 3 7 12 13 14 Name Alp Ecem Emre Ayse Ali Report: Average Staff Salary Report Generates average monthly salary for each type of staff (ground & air), orders the group by salary in descending order. Query SELECT * from (SELECT g.type as type, AVG(g.salary) as salary FROM ground_staff GROUP BY g.type UNION SELECT f.type as type, AVG(f.salary) as salary FROM flight_staff GROUP BY f.type) r ORDER BY r.salary Example Result Type PILOT EXEC SALES HOSTESS Salary 22300 22100 8900 7800 12 5. User’s Guide & UI Mockups 5.1. Login Page Users are welcomed with a login page as the home page of airline company website. Users choose their role (Staff or Customer) and then login with their username & password. If they do not have account, they click the link to create one. Fig 1. Login screen. The following SQL queries are used to check credentials. It is crucial to sanitize parameters (?) from malicious characters to prevent SQL injections. SELECT name, password from customer where name=’?’ and password=’?’ Or, SELECT name, password from ground_staff where username=’?’ and password=’?’ 5.2. Signup Page Users register themselves to the systems according to their role. Since this project is just a proof of concept we just allow anyone to register as customer and as well as staff of the company. Users use their name to login to the system. 13 Fig 2. Customer Signup Fig 3. Staff signup. The following SQL statements are used to check existence of users: SELECT COUNT(*) as c from customer where name=’?’ or 14 SELECT COUNT(*) as c from ground_staff where name=’?’ Then if no records exist, INSERT INTO customer (name, password) VALUES (‘?’, ‘?’) or, INSERT INTO ground_staff (name, password, type) VALUES (‘?’, ‘?’, ‘?’) 5.3. Reservation and Payment System When a customer is logged in, we redirect him/her to online reservation system. It lists flights between destinations in a specific date or just lists all the upcoming flights. Fig 4. Online reservation system, flight listing screen. The following SQL statement is used to list airports: SELECT a.id, a.name, c.name as city from airport a, city c where c.id=a.city After this screen users can be redirected to a) all upcoming flights b) results of the search query 15 Fig 5. All upcoming flights In order to list all following flights, we can use such a SQL statement: SELECT f.date, r.duration, dep.name as departure, arr.name as arrival, f.flight_number, f.fare FROM flight f, route r, airport dep, airport arr where f.route=r.id AND r.departure=dep.id AND r.destination=arr.id AND f.date>NOW() ORDER BY f.date ASC; The other option is the show the search results: Fig 6. Flight search results between two airports. 16 The following query is used to find flights between two airports on a given day. SELECT f.date, r.duration, dep.name as departure, arr.name as arrival, f.flight_number, f.fare FROM flight f, route r, airport dep, airport arr where f.route=r.id AND r.departure=dep.id AND r.destination=arr.id AND f.date>NOW() AND DATE(f.date)=DATE(‘?’) and r.departure=’?’ and r.destination=’?’ ORDER BY f.date ASC; After pressing continue in one of these pages, user is redirected to the payment page. Fig 7. Payment page. In payment page (fig 7), customers choose a seat and then make the payment. We do not check that customer can hijack the form and change seat id to a not available one. The following query is used to find which seats are occupied with parameter flight id: SELECT seat FROM booking where flight=? Then here is how the booking is made: START TRANSACTION; INSERT INTO payment (amount, date) VALUES ((SELECT fare from flight where id=?), NOW()); INSERT INTO booking (customer_id, flight_id, seat, booking_date, payment) VALUES (?, ?, ?, NOW(), mysql_insert_id()); COMMIT; After that customer sees a confirmation message. 17 5.4. Sales Department Check-in Screen At the airport, sales staff can check-in users with their seat number, flight id and their luggage. This section is available to only sales managers. Fig 8 illustrates the check-in screen. Fig 8. Check-in screen. After sales staff presses continue, luggage is added to the flight if a booking is found. SQL: SELECT * FROM booking b, flight f where f.flight_number=’?’ AND b.flight=f.id AND b.seat=?; ; then using the result above, for each luggage which is >0 kg BEGIN TRANSACTION; INSERT INTO luggage (weight) VALUES (?); INSERT INTO booking_luggage (booking id, luggage_id) VALUES (?, last_insert_id()); COMMIT; 18 The following screen can print the travel itinerary. Fig 9. Check-in summary. 5.5. City Management Executive staff can create-edit and delete cities using the following screens. Fig 10. City management screen. Query statement to list cities: SELECT * FROM city; Query statement to insert a city (check existence first): SELECT COUNT(*) as c from city where name=’?’; INSERT INTO city (name) values (‘?’); Query statement to update a city: UPDATE city set name=’?’ where id=? Query statement to delete a city: DELETE FROM city where id=? 19 5.5. Airport Management Executive staff can create-edit and delete airport using the following screens. Fig 11. Query statement to list all airports: SELECT a.id, a.code, c.name as city FROM airport a, city c WHERE c.id=a.city; Query statement to list all cities in add screen (2): SELECT * from city; Query statement to insert an airport. INSERT INTO airport (name, code, city) values (‘?’, ‘?’, ?); Query statement to update an airport: UPDATE airport set name=’?’, code=’?’, city=? WHERE id=? Query statement to delete an airport: DELETE FROM airport WHERE id=? 20 5.6. Fleet (planes) management Executive staff can add new planes to the fleet, remove them and edit them using the following screens. (Fig 12) Fig 12. Plane management screen. Query statement to list all planes: SELECT * FROM plane; Query statement to insert an airport. INSERT INTO plane (name, model, capacity) values (‘?’, ‘?’, ?); Query statement to update an airport: UPDATE plane set name=’?’, model=’?’, capacity=? WHERE id=? Query statement to delete a plane: DELETE FROM plane WHERE id=? 21 5.7. Route management Executives can create new routes to fly on, edit them and delete them using management screen (Fig 13) Fig 13. Route management screen. Query statement to list all routes: SELECT r.id, f.name as departure, t.name as destination, r.duration FROM route r, airport f, airport t where r.departure=f.id AND r.destination=t.id; Query statement to list all airports in add or edit screen: SELECT id, name from airport order by name asc; Query statement to insert a route. INSERT INTO route (duration, departure, destination) values (?, ?, ?); Query statement to update a route: UPDATE route set duration=?, departure=?, destination=? WHERE id=? Query statement to delete a route: DELETE FROM route WHERE id=? 22 5.8. Flight Management Executives can arrange new flights on a specific route and on a specific date with a specific price. They also can assign flight crew. Later they can change price of the flight as the flight date approaches using management interface. (fig 14) Fig 14. Flight management screen. Query statement to list all flights: SELECT f.id, f.flight_number, rr.id as route_id, rr.name as route_name, f.date, f.fare, p.name as plane_name FROM flight f, route_listing rr, plane p WHERE f.route=rr.id AND f.plane=p.ids; Query statement to list all pilots and hostesses in add/edit screens. SELECT p.name, p.id from flight_staff p where p.type=’PILOT’; SELECT h.name, h.id from flight_staff h where h.type=’HOSTESS’; Query statement to list routes on add flight or edit flight screen: SELECT * FROM route_listing; Query statement to insert a flight. BEGIN TRANSACTION; INSERT INTO flight (flight_number, departure_date, fare, route, plane) values (‘?’, ?, ?, ?, ?); ; use $f as INSERT INTO INSERT INTO INSERT INTO COMMIT; last_insert_id(); flight_crew (flight_id, staff_id) VALUES ($f, $pilot1); flight_crew (flight_id, staff_id) VALUES ($f, $pilot2); flight_crew (flight_id, staff_id) VALUES ($f, $hostess); 23 Query statement to update a flight: BEGIN TRANSACTION; DELETE FROM flight_crew where flight_id=? INSERT INTO flight_crew (flight_id, staff_id) VALUES ($f, $pilot1); INSERT INTO flight_crew (flight_id, staff_id) VALUES ($f, $pilot2); INSERT INTO flight_crew (flight_id, staff_id) VALUES ($f, $hostess); UPDATE flight set fare=?, departure_date=?, route=? WHERE id=? COMMIT; Query statement to delete a flight: DELETE FROM flight WHERE id=? 5.9. Staff Management Executives can update details of registered staff, including their types and salaries using management screen (fig 15) Fig 15. Staff management screen The following queries are used in staff management logic: 24 Query to list all staff SELECT s.id, s.name, s.salary, s.date_joined, s.type FROM ground_staff UNION SELECT s.id, s.name, s.salary, s.date_joined, s.type FROM flight_staff Query to update a staff member. $tbl is determined on runtime according to the type. UPDATE $tbl set name=’?’, salary=? Where id=? Query statement to delete a staff, $tbl is determined on runtime according to the type. DELETE FROM $tbl WHERE id=? 25