Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted...
-
Upload
virgil-willis -
Category
Documents
-
view
216 -
download
1
Transcript of Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted...
![Page 1: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/1.jpg)
Relational Databases
CPSC 315 – Programming StudioSpring 2012Project 1, Lecture 2
Slides adapted from those used byJeffrey Ullman, via Jennifer Welch
![Page 2: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/2.jpg)
Relational Data Model
Relations are stored in tables e.g. Sponsor(Senator,Bill)
Sponsor
Senator Bill
Smith Tax
Jones Defense
Smith Defense
Adams Commerce
Atrributes
Tuples
![Page 3: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/3.jpg)
Schemas
A relation schema is a relation name and a list of attributes Sponsor(Senator,Bill)
A database is a collection of relationsA database schema is the set of all the relation schemas in the database
![Page 4: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/4.jpg)
Converting from Entity-Relationship Model
ER: Entity set -> relation ER Attributes become Relational
attributes
ER: Relationship -> relation Keys of connected ER entity sets
become Relational attributes
![Page 5: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/5.jpg)
ER Entity Sets
Senator
Name
State
Party
Years
Name Text
Bill
Name
Organization
Lobbyist
![Page 6: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/6.jpg)
Relations
Senator(Name,Party,State,Years)Bill(Name,Text)Lobbyist(Name,Organization)
![Page 7: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/7.jpg)
ER Relationships
Senator
Name
State
Party
Years
Name Text
Bill
SponsoredName
Organization
Lobbyist
Wrote
Contributed
![Page 8: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/8.jpg)
Relations
Sponsored(Senator,Bill)Wrote(Bill,Lobbyist)Contributed(Senator,Lobbyist)
Remember, each of these is expressed as a table (with the columns given by the “parameters”)Notice that columns can refer to “bigger” items, with even more attributes
![Page 9: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/9.jpg)
Combining Relations
Relations can sometimes be combined.Assume a “base” entity set with its relation.If there is a many-to-one relation, then it can be combined with the base entity set.Should not combine many-to-many Would cause redundancy – each of the
many stored
![Page 10: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/10.jpg)
Combining Relations
Example (many-to-one): (Good) Person(Name, Birthdate, Height, Weight,
Eye Color, Hair Color) BornIn(Person,Town) Person(Name, Birthdate, Height, Weight,
Eye Color, Hair Color, Town)
Example(many-to-many): (Bad) Senator(Name, Party, State, Years) Sponsored(Senator, Bill) Senator(Name, Party, State, Years, Bill)
![Page 11: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/11.jpg)
Weak Entity Sets
The relation for a weak entity set must contain all the elements of its keySupporting relationships are usually redundant (unless possibly multi-way)
![Page 12: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/12.jpg)
Weak Entity Set Example
Baseball Player
FirstName
LastName
Number
Position
Nationality Salary
Birthdate
Name
TeamPlaysOn
City
Note arrrow:indicates manyto one.
![Page 13: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/13.jpg)
Weak Entity Set Example
Team(Name, City)Baseball Player(Number, TeamName, First Name, Last Name, Position, Birthdate, Nationality, Salary)
![Page 14: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/14.jpg)
Weak Entity Set Example
Team(Name, City)Baseball Player(Number, TeamName, First Name, Last Name, Position, Birthdate, Nationality, Salary)Note that we don’t need PlaysOn(BaseballPlayer.Number, BaseballPlayer.TeamName, Team.Name)
![Page 15: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/15.jpg)
Weak Entity Set Example
Team(Name, City)Baseball Player(Number, TeamName, First Name, Last Name, Position, Birthdate, Nationality, Salary)Note that we don’t need PlaysOn(BaseballPlayer.Number, BaseballPlayer.TeamName, Team.Name)
Redundant (same)
![Page 16: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/16.jpg)
Weak Entity Set Example
Team(Name, City)Baseball Player(Number, TeamName, First Name, Last Name, Position, Birthdate, Nationality, Salary)Note that we don’t need PlaysOn(BaseballPlayer.Number, BaseballPlayer.Team.Name)
![Page 17: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/17.jpg)
Weak Entity Set Example
Team(Name, City)Baseball Player(Number, TeamName, First Name, Last Name, Position, Birthdate, Nationality, Salary)Note that we don’t need PlaysOn(BaseballPlayer.Number, BaseballPlayer.Team.Name) Already
Included
![Page 18: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/18.jpg)
SubclassesDifferent Options
Different ways to represent subclasses
U.S.Representative District
Elected OfficialName
Party
isa
![Page 19: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/19.jpg)
Object-Oriented Style
One relation for each subset, including all “inherited” attributes
Elected Official
Name Party
Bill Flores Republican
Lloyd Doggett
Democrat
John Adams Federalist
Ron Paul Republican
U.S. Representative
Name Party District
Bill Flores Republican
17
Ron Paul Republican
14
Lloyd Doggett Democrat 25
![Page 20: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/20.jpg)
Entity-Relationship Style
One relation for each subclass (including key)
Elected Official
Name Party
Bill Flores Republican
Lloyd Doggett
Democrat
John Adams Federalist
Ron Paul Republican
U.S. Representative
Name District
Bill Flores 17
Ron Paul 14
Lloyd Doggett 25
![Page 21: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/21.jpg)
Using Nulls Style
One relation total, with nulls for unknown information
Can save space, but problematic if multiple subclasses or lots of NULLs
U.S. Representative
Name Party District
Bill Flores Republican
17
Lloyd Doggett
Democrat 25
John Adams Federalist NULL
Ron Paul Republican
14
![Page 22: Relational Databases CPSC 315 – Programming Studio Spring 2012 Project 1, Lecture 2 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch.](https://reader030.fdocuments.us/reader030/viewer/2022020307/56649f1b5503460f94c31664/html5/thumbnails/22.jpg)
Keys
A Key “functionally determines” all other attributes of the relation Given a relation and a key, there is
only one tuple that corresponds to it
There are subtle differences from an E-R key, which we won’t go into.