Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1 Midterm Regulations Midterm is Feb. 7th (11:00am – 12:30pm) Covers until before the Views You answer in the same exam paper Closed-Book, One sheet is allowed for your notes 2 Topics Covered in Midterm Entity-Relationship Model & ERD Relational Model Relational Algebra SQL Commands DDL: Creating tables DML: inserts, updates, deletes, querying 3 Relational Algebra & SQL 4 Relational Algebra: Key Points Understand the meaning of each operator and what it does, E.g., Selection (σc): selects certain tuples based on a condition (all columns) Projection (πk): selects certain columns (All tuples) Join (⋈C): joins tuples from one relation with another relation based on a condition Practice how to combine these operators to answer queries Performance rules: Apply selection earlier whenever possible Use joins instead of Cartesian product Avoid un-necessary joins 5 SQL: Key Points Understand the Select clauses and their order of execution Practice the grouping and aggregation Practice the nested queries optional SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; 6 Exercise (Warships) ShipClass(name, type, country, numGuns, designYear, weight) WarShips(shipName, className, builtYear) Missions(missionName, date) Results(shipName, missionName, status) Ships having the same design belong to the same class. The class information is stored in “ShipClass”. The ship information is stored in “WarShips” Each ship may participate in multiple missions. The mission information is stored in “Missions” Each ship in a mission has a status as either ‘damaged’, ‘sunk’, or ‘ok’. This information is stored in “Results” 7 Query 1 For classes with at least 10 guns, report the class name and country; 8 Query 2 List the warShips built after 1932 9 Query 3 Suppose there was an agreement prohibiting war-ships heavier than 40,000 tons. Report the ship names and built year of ships violating this agreement. 10 Query 4 Continue from the previous query, Find the ship(s) that violate the agreement the least (i.e., the lightest ships violating the agreement) 11 Query 5 In the mission of “Sun Rising”, find the name and weight of the ships involved 12 Query 6 Report ship name for ships that were ‘ok’ in one mission, but later ‘sunk’ in another. 13 Query 7 Report the names of ships damaged in the mission of “Sun Rising”. 14 Query 8 Report each shipName and the number of missions it participated in (including 0 participation) 15 ER & Relational Models 16 Key Points Remember the notations used in ERD Mapping rules from ERD to relational model, E.g., Relationships cardinalities (1-1, 1-M, M-M) Entity sets and weak entity sets Primitive, derived, and composite attributes M-M relationship separate table 1-M relationship take the key from the one-side to the many-side 1-1 relationship take the key from either sides to the other side ISA relationship Many choices depending on its type Remember to indicate the integrity constraints, most important are: Primary keys, Foreign keys 17 Airline Application We have a set of planes, each plane can make many flights. Each plane has properties such as: ID (unique identifier), model, capacity, year in which it is built, and weight Each flight has a departure city, arrival city, and it can make transit in many other cities. Also each flight has the departure date, arrival date, and the transit date in each of the transit cities (assume the ‘date’ captures both the date and exact time). Each flight has some properties such as: FlightNum (unique identifier), number of transit stops, number of passengers For each city, we have an CityID (unique identifier), name, and country 18 Question 1 Design an ER diagram for the given application. The diagram must include: The entity sets, attributes, primary keys The relationships with the correct cardinalities State any assumptions that you make and affect your design 19 Question 2 Extend your ERD in the previous question to capture the following additional requirements: Each flight has one captain, one assistant captain, and between 2 to 4 flight attendants Each of the captain, assistant captain, and attendants have a common set of properties such as: EmployeeID (unique identifier), name, salary, and DoB The same person can be captain on some flights and assistant captain on other flights (basically same person can have different roles (captain or assistant captain) in different flights). 20 Question 3 Map the ERD from the previous question (Question 2) to the corresponding relational model Provide the CREATE TABLE commands with the field names and appropriate data types Identify the primary keys and foreign keys in the relations 21