Instructor: Mohamed Eltabakh

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Database Systems More SQL Database Design -- More SQL1.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
1 The Relational Model Instructor: Mohamed Eltabakh
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Revision Instructor: Mohamed Eltabakh 1.
Final Exam Revision Instructor: Mohamed Eltabakh 1.
1 Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh
Advanced SQL: Triggers & Assertions
Announcements Midterm is Nov. 22 (8:15am – 9:45am) Covers until the end of Normalization You answer in the same exam paper Closed-Book, One sheet is allowed.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
CMPT 258 Database Systems Midterm Review. Regarding the Exam Oct 15 Thursday Close book Cheat sheet (1 side of an A4 paper)
Quiz (20 mins) Instructor: Mohamed Eltabakh 1.
Logical Database Design and the Relational Model.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
CS3431: C-Term Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh
FEN Introduction to the database field: The development process Seminar: Introduction to relational databases Development process: Analyse.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
ER Diagrams and Relational Model CS 174a (Winter 2015)
Chapter 2: Entity-Relationship Model
COP Introduction to Database Structures
Contents Design Process Modeling Constraints E-R Diagram Design Issues
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Translating ER Schema to Relational Model
Let try to identify the conectivity of these entity relationship
Entity-Relationship Model
Tuning Transact-SQL Queries
COP Introduction to Database Structures
EER Model – Chapter
Chapter 5: Logical Database Design and the Relational Model
Entity-Relationship Model
Chapter 2: Entity-Relationship Model
Tables and Their Characteristics
Chapter -3- Data Modeling Using the Entity-Relationship Model
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Payroll Management System
Lecture # 13 (After 1st Exam)
Translation of ER-diagram into Relational Schema
Entity-Relationship Model and Diagrams (continued)
CS 174: Server-Side Web Programming February 12 Class Meeting
From ER to Relational Model
Instructor: Mohamed Eltabakh
Data Modelling Introduction
Database Fundamentals
Functional Dependencies and Normalization
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
Session 2 Welcome: The seventh learning sequence
The Relational Model Textbook /7/2018.
Chapter 6: Entity-Relationship Model
Weak Entity Sets An entity set that does not have a primary key is referred to as a weak entity set. The existence of a weak entity set depends on the.
Chengyu Sun California State University, Los Angeles
Database Design: Relational Model
Chapter 7: Entity-Relationship Model
Mapping an ERD to a Relational Database
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Manipulating Data Lesson 3.
Instructor: Mohamed Eltabakh
Presentation transcript:

Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

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

Topics Covered in Midterm Entity-Relationship Model & ERD Relational Model Relational Algebra SQL Commands DDL: Creating tables DML: inserts, updates, deletes, querying

Relational Algebra & SQL

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

SQL: Key Points Understand the Select clauses and their order of execution Practice the grouping and aggregation Understand when to use “Where” or “Having” Practice the nested queries SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; optional

Exercise (War Ships) 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”

Query 1 πname,country (σnumGuns >= 10(ShipClass)) For classes with at least 10 guns, report the class name and country; Select name, country From ShipClass Where numGuns >= 10; πname,country (σnumGuns >= 10(ShipClass))

Query 2 σbuiltYear > 1932(WarShips) List the warShips built after 1932 σbuiltYear > 1932(WarShips) Select * From Warships Where builtYear >1932;

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. πshipName,builtYear (σweight > 40,000(ShipClass) ⋈name=className WarShips) Select shipName, builtYear From Warships, ShipClass Where name= className And weight > 40,000;

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) R1 minW<-min(weight) (σweight > 40,000(ShipClass)) Result  πshipName, weight (R1 ⋈minW=weight ShipClass ⋈name=className WarShips) Inner select: Select min(weight) From ShipClass Where weight > 40,000

Query 4 (Cont’d) Continue from the previous query, Find the ship(s) that violate the agreement the least (i.e., the lightest ships violating the agreement) Select shipName From Warships, ShipClass Where name= className And weight = ( Select min(weight) From ShipClass Where weight > 40,000);

Query 5 In the mission of “Sun Rising”, find the name and weight of the ships involved R1 πshipName(σmissionName = ‘Sun Rising’(Results)) Result  πR1.shipName, weight (R1 ⋈R1.shipName=Warships.shipName WarShips ⋈className=name ShipClass)

Query 5 (Cont’d) In the mission of “Sun Rising”, find the name and weight of the ships involved Select W.shipName, S.weight From Results R, Warships W, ShipClass S Where R.shipName= W.shipName And W.className = S.name And R.missionName = ‘Sun Rising’;

Query 6 πshipName(σstatus = ‘sunk’(Results)) ∩ Report ship name for ships that were ‘ok’ in one mission, but later ‘sunk’ in another. Select shipName From Results Where status = ‘ok’ Intersect Where status = ‘sunk’; πshipName(σstatus = ‘sunk’(Results)) ∩ πshipName(σstatus = ‘ok’(Results))

Query 7 Report the names of ships damaged in the mission of “Sun Rising”. πshipName (σmissionName= ‘Sun Rising’ AND status=‘damaged’ (Results)) Select shipName From Results Where missionName= ‘Sun Rising’ And status = ‘damaged’;

Query 8 R1  shipName, cnt <-count(*) (Results) Report each shipName and the number of missions it participated in (including 0 participation) R1  shipName, cnt <-count(*) (Results) R2  πshipName, cnt <- 0 (πshipName(Warships) - πshipName(R1)) Result  R1 U R2

Query 8 (Cont’d) Report each shipName and the number of missions it participated in (including 0 participation) Select shipName, 0 as cnt From WarShips Where shipName not in (Select shipName from Results) Union Select shipName, count(*) as cnt From Results Group by shipName;

ER & Relational Models

Key Points Remember the notations used in ERD Relationships cardinalities (1-1, 1-M, M-M) Entity sets and weak entity sets Primitive, derived, and composite attributes Mapping rules from ERD to relational model, E.g., 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

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

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

Assumption: The entire flight is done by a single plane FNum NumStops Num Passengers Plane year ID model capacity weight makes arrival Arrival date departure date transit date City CityID country name Assumption: The entire flight is done by a single plane

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).

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

Relational Model Create Table Employee ( ID: int Primary Key, name: varchar(100), DOB: int, Salary: int, JobTitle: varchar(100) check JobTitle in (‘Captain’, ‘Attendant)); Create Table Plane ( ID: int Primary Key, Year: int, Capacity: int, Weight: int, Model: varchar(50)); Create Table City( ID: int Primary Key, Name: varchar(100), Country: varchar(100)); Create Table Flight( FNum: varchar(8) Primary Key, NumStops: int, NumPassengers: int, PlaneID: int Foreign Key References Plane(ID), ArrivalCityID: int Foreign Key References City(CityID), DepartCityID: int Foreign Key References City(CityID), ArrivalDate: date, DepartDate: date, CaptainID: int Foreign Key References Employee (ID), AssistID: int Foreign Key References Employee (ID));

Relational Model (Cont’d) Create Table Transit ( FNum: int Foreign Key References Flight(FNum), CityID: int Foreign Key References City(CityID), TransitDate: date, Primary Key (Fnum, CityID)); Create Table Flight_Attendant( FNum: int Foreign Key References Flight(FNum), AttendantID: int Foreign Key References Employee(ID), Primary Key (Fnum, EmpID));

Relational Model (Cont’d) Assumptions: The ISA relationship is modeled using one table “Employee” Whether the employee is Captain or Attendant is modeled using a new attribute “JobTitle” Business Constraints: Attributes CaptainID and AssistID in “Flight” must correspond to employee with job title “Captain” Attribute AttendantID in “Flight_Attendant” must correspond to employee with job title “Attendant” Attribute NumPassengers in “Flight” must be less than or equal to attribute Capacity in “Plane”