CS 480: Database Systems Lecture 5 January 25, 2013
Recap Relational Model Set-oriented Model Set of Attributes form a relation scheme Tuples is an assignment of values to each attribute Set of Tuples form a relation Set of relations form the relational database.
From ER-diagrams to Relational Model For each (non-dominated, non-specialization) entity set E with attribute set {A1,A2,…,An} we will have a relation scheme E(A1,A2,…,An). The key of this relation E is the same key of the entity set E. Book calls non-dominated STRONG
Non-dominated Entity Sets: Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary)
Non-dominated Entity Sets: Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary)
Dominated (Weak) Entity Sets Let A be a dominated entity set and let B be the dominating entity set. Let the key of B consist of {b1,b2,…,bn}. Also let A have attributes {a1,a2,…,am}. Then we will have a relation scheme A(a1,a2,…,am, b1,b2,…,bn). The key of relation A will consist of any key attributes of the entity set A and all of {b1,b2,…,bn}. Weak because it doesn’t necessarily have a key on its own.
Dominated Entity Sets: Take-off()
Dominated Entity Sets: Take-off(date) Will contain all attributes of Take-off.
Dominated Entity Sets: Take-off(date, number) Will contain all key attributes of the dominating entity set.
Dominated Entity Sets: Take-off(date, number) Keys will be all the keys of the dominating entity (Flight) and keys if any of the dominated entity (Take-off).
Relationship Sets Each relationship set R will become a relation scheme R. The keys of all the participating relationships will be part of the relation scheme R. Also, any attributes that the entity set R had will be part of the relation R.
Relationship Sets: Reservation()
Relationship Sets: Reservation()
Relationship Sets: Reservation(name, address, Take-off.date) All keys of participating relationships.
Relationship Sets: Reservation(name, address, Take-off.date) All keys of participating relationships. Are these all the keys of the participating entities?
Reservation(name, address, Take-off.date, Flight.number) Relationship Sets: Reservation(name, address, Take-off.date, Flight.number) All keys of participating relationships. Look past the diagram Are these all the keys of the participating entities? It’s missing the Flight.number, which is part of the Take-off key.
Reservation(name, address, Take-off.date, Flight.number, Relationship Sets: Reservation(name, address, Take-off.date, Flight.number, date, class, seat#) All attributes of the relationship itself. Look past the diagram
From Relationships to Relations The keys for the relation R will depend on the cardinality constraints. Many-to-many – All keys of the participating entity sets will form part of the key of the relation R. One-to-one – Choose the key of either of the participating entity sets as the key for R. One-to-many – Choose the key of the many side. Ask why at least for the one-to-one… Also for the one-to-many…
Reservation(name, address, Take-off.date, Flight.number, Relationship Sets: Reservation(name, address, Take-off.date, Flight.number, date, class, seat#) Take-off-OF-Flight( Take-off.date, Flight.number) Take-off-BY-Plane(plane-id, Plane-TYPE(plane-id, manufacturer, number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number) DISCUSS PILOT WHAT ABOUT N-ary RELATIONSHIPS? All many’s then all keys form part. Some one’s then the keys for all the many’s form part of the key.
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-OF-Flight(Take-off.date, Flight.number) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Plane-TYPE(plane-id, manufacturer, number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)
Weak Entity Sets Don’t have sufficient attributes to form a key. Dominated Entity Sets (Take-off) and Specialized Entity Sets (Pilot) are examples. Redundancy will exist if we give a relation to the relationship and the weak entity set.
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, Flight.number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-OF-Flight(Take-off.date, Flight.number) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Plane-TYPE(plane-id, manufacturer, number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-OF-Flight(Take-off.date, Flight.number) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Plane-TYPE(plane-id, manufacturer, number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Plane-TYPE(plane-id, manufacturer, number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)
Combination of Schemas An entity set E in a relationship R has full participation if every element of E participates in at least one relationship in R. For example: Plane(plane-id) Plane-Model(manufacturer, number, capacity) Plane-TYPE(plane-id, manufacturer, number) Every plane has a plane-model, then every plane participates in the TYPE relationship. Plane and Plane-Type can be combined into one relation Plane(plane-id,manufacturer,number) This can be done for many-to-one relationships. Combine the “many” relation and the relationship. Understand WHY it can be done.
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, Flight.number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Plane-TYPE(plane-id, manufacturer, number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id,manufacturer,number) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, Flight.number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)
Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id,manufacturer,number) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, Flight.number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number) WHAT ARE THESE? Foreign Keys What are the implications of them and what’s important about them? Adds a constraint to the database. Referential Integrity… If a passenger changes his name then it should change also on the RESERVATION table…
Final Airline Relational Schema Passenger(name, address, phone#,frequent-flyer#) Flight(number, source, destination, departure-time, arrival-time) Plane(plane-id,manufacturer,number) Plane-Model(manufacturer, number, capacity) Employee(ss#, name, address, salary) Take-off(date, Flight.number) Reservation(name, address,Take-off.date, Flight.number, date, class, seat#) Take-off-BY-Plane(plane-id, Take-off.date, Flight.number) Pilot(SS#, flight-hours) Can-fly(SS#, manufacturer, number)