Download presentation
Presentation is loading. Please wait.
1
CPSC-310 Database Systems
Professor Jianer Chen Room 315C HRBB Lecture #2 Source: modification of slides by Jeffrey Ullman of Stanford U.
2
Outline of Course Representing things by tables E-R model (Ch. 4)
Good table structures Functional Dependencies (Ch. 3) Basic operations on relations Relational Algebra (Ch. 2) Storage management (Chs ) SQL languages in DDL/DML (Ch. 6) Query processing (Chs ) More on SQL (Chs. 7-9) Transition processing (Chs )
3
The Entity/Relationship Model
The E/R model is an intuitive (graphical) model used to sketch database designs, (where entity = object).
4
The Entity/Relationship Model
The E/R model is an intuitive (graphical) model used to sketch database designs, (where entity = object). Properties of entities and how entities connect. Not how to operate on entities.
5
The Entity/Relationship Model
The E/R model is an intuitive (graphical) model used to sketch database designs, (where entity = object). Properties of entities and how entities connect. Not how to operate on entities. E/R designs are given by pictures (entity/relationship diagrams).
6
The Entity/Relationship Model
The E/R model is an intuitive (graphical) model used to sketch database designs, (where entity = object). Properties of entities and how entities connect. Not how to operate on entities. E/R designs are given by pictures (entity/relationship diagrams). It is in general straightforward to convert E/R designs to relational database designs.
7
Entity Sets Entity = “thing” or object.
8
Entity Sets Entity = “thing” or object.
Entity set = collection of similar entities. Similar to a class in object-oriented languages.
9
Entity Sets Entity = “thing” or object.
Entity set = collection of similar entities. Similar to a class in object-oriented languages. Attribute = property of (the entities of) an entity set.
10
Entity Sets Entity = “thing” or object.
Entity set = collection of similar entities. Similar to a class in object-oriented languages. Attribute = property of (the entities of) an entity set. Attributes are of simple data types, e.g. integers or character strings.
11
E/R Diagrams In an entity-relationship diagram:
12
E/R Diagrams In an entity-relationship diagram:
Entity set = rectangle.
13
E/R Diagrams In an entity-relationship diagram:
Entity set = rectangle. Attribute = oval, with a line to the rectangle representing its entity set.
14
E/R Diagrams In an entity-relationship diagram: Example
Entity set = rectangle. Attribute = oval, with a line to the rectangle representing its entity set. Example Entity set Beers has two attributes, name and manf (manufacturer)
15
E/R Diagrams In an entity-relationship diagram: Example
Entity set = rectangle. Attribute = oval, with a line to the rectangle representing its entity set. Example Entity set Beers has two attributes, name and manf (manufacturer) Each Beers entity has values for these two attributes. e.g. (Bud, Anheuser-Busch)
16
E/R Diagrams In an entity-relationship diagram: Example
Entity set = rectangle. Attribute = oval, with a line to the rectangle representing its entity set. Example Entity set Beers has two attributes, name and manf (manufacturer) Each Beers entity has values for these two attributes. e.g. (Bud, Anheuser-Busch) Beers name manf
17
Relationships A relationship connects some entity sets.
18
Relationships A relationship connects some entity sets.
It is given by a diamond, with lines to each of the entity sets involved.
19
Relationships A relationship connects some entity sets.
It is given by a diamond, with lines to each of the entity sets involved. Bars name license addr name manf Beers Drinkers name addr
20
Relationships A relationship connects some entity sets.
It is given by a diamond, with lines to each of the entity sets involved. Bars sell some beers. Bars name license addr name manf Beers Drinkers name addr
21
Relationships A relationship connects some entity sets.
It is given by a diamond, with lines to each of the entity sets involved. Bars sell some beers. Bars name license addr name manf Sells Beers Drinkers name addr
22
Relationships A relationship connects some entity sets.
It is given by a diamond, with lines to each of the entity sets involved. Bars sell some beers. Bars name license addr name manf Sells Beers Likes Drinkers like some beers. Drinkers name addr
23
Relationships A relationship connects some entity sets.
It is given by a diamond, with lines to each of the entity sets involved. Bars sell some beers. Bars name license addr name manf Sells Beers Frequents Likes Drinkers like some beers. Drinkers frequent some bars. Drinkers name addr
24
Relationship Set The current “value” of an entity set is the set of entities that belong to it. Example: the set of all bars in our database. The “value” of a relationship is a set of lists of currently related entities, one from each of the related entity sets. Bar Beer Joe’s Bar Bud Joe’s Bar Miller Sue’s Bar Bud Sue’s Bar Pete’s Ale Sue’s Bar Bud Lite For the relationship Sells, we might have a relationship set like:
25
Multiway Relationships
Sometimes, we need a relationship that connects more than two entity sets.
26
Multiway Relationships
Sometimes, we need a relationship that connects more than two entity sets. Example. Suppose that drinkers will only drink certain beers at certain bars.
27
Multiway Relationships
Sometimes, we need a relationship that connects more than two entity sets. Example. Suppose that drinkers will only drink certain beers at certain bars. Our three binary relationships Likes, Sells, and Frequents do not allow us to make this distinction.
28
Multiway Relationships
Sometimes, we need a relationship that connects more than two entity sets. Example. Suppose that drinkers will only drink certain beers at certain bars. Our three binary relationships Likes, Sells, and Frequents do not allow us to make this distinction. But a 3-way relationship would.
29
Multiway Relationships
Sometimes, we need a relationship that connects more than two entity sets. Example. Suppose that drinkers will only drink certain beers at certain bars. Our three binary relationships Likes, Sells, and Frequents do not allow us to make this distinction. But a 3-way relationship would. name addr name Bars Beers Preferences manf license Drinkers name addr
30
Multiway Relationships
Bar Drinker Beer Joe’s Bar Ann Miller Sue’s Bar Ann Bud Sue’s Bar Ann Pete’s Ale Joe’s Bar Bob Bud Joe’s Bar Bob Miller Joe’s Bar Cal Miller Sue’s Bar Cal Bud Lite
31
Multiplicity of Relationships
32
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets.
33
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a many-many relationship, an entity of either set can be connected to many entities of the other set.
34
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a many-many relationship, an entity of either set can be connected to many entities of the other set. Example. a bar Sells many beers; a beer is sold by many bars.
35
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a many-many relationship, an entity of either set can be connected to many entities of the other set. Example. a bar Sells many beers; a beer is sold by many bars. Bars Beers A many-many relationship Sells
36
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a many-one relationship, an entity in one A of the entity sets is connected to at most one entity of the other entity set B, but an entity of B can be connected any number of entities of A.
37
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a many-one relationship, an entity in one A of the entity sets is connected to at most one entity of the other entity set B, but an entity of B can be connected any number of entities of A. Example. Favorite, from Drinkers to Beers is many-one.
38
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a many-one relationship, an entity in one A of the entity sets is connected to at most one entity of the other entity set B, but an entity of B can be connected any number of entities of A. Example. Favorite, from Drinkers to Beers is many-one. Drinkers Beers A many-one relationship Favorite
39
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a one-one relationship, each entity of either entity set is related to at most one entity of the other set.
40
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a one-one relationship, each entity of either entity set is related to at most one entity of the other set Example. Relationship Best-seller between entity sets Manfs (manufacturer) and Beers.
41
Multiplicity of Relationships
We consider binary relationships, i.e., relationship between two entity sets. In a one-one relationship, each entity of either entity set is related to at most one entity of the other set Example. Relationship Best-seller between entity sets Manfs (manufacturer) and Beers. Manfs Beers A one-one relationship Best-seller
42
Representing “Multiplicity”
43
Representing “Multiplicity”
Show a many-one relationship by an arrow entering the “one” side.
44
Representing “Multiplicity”
Show a many-one relationship by an arrow entering the “one” side. Show a one-one relationship by arrows entering both entity sets.
45
Representing “Multiplicity”
Show a many-one relationship by an arrow entering the “one” side. Show a one-one relationship by arrows entering both entity sets. Rounded arrow = “exactly one,” i.e., each entity of the first set is related to exactly one entity of the target set.
46
Representing “Multiplicity”
Show a many-one relationship by an arrow entering the “one” side. Show a one-one relationship by arrows entering both entity sets. Rounded arrow = “exactly one,” i.e., each entity of the first set is related to exactly one entity of the target set. Drinkers Beers Likes Favorite
47
Example Consider Best-seller between Manfs and Beers.
48
Example Consider Best-seller between Manfs and Beers.
Some beers are not the best-seller of any manufacturer, so a rounded arrow to Manfs would be inappropriate.
49
Example Consider Best-seller between Manfs and Beers.
Some beers are not the best-seller of any manufacturer, so a rounded arrow to Manfs would be inappropriate. But a beer manufacturer has to have a best-seller.
50
Example Consider Best-seller between Manfs and Beers.
Some beers are not the best-seller of any manufacturer, so a rounded arrow to Manfs would be inappropriate. But a beer manufacturer has to have a best-seller. Manfs Beers Best- seller
51
Attributes on Relationships
Sometimes it is useful to attach an attribute to a relationship.
52
Attributes on Relationships
Sometimes it is useful to attach an attribute to a relationship. Think of this attribute as a property of tuples in the relationship set.
53
Attributes on Relationships
Sometimes it is useful to attach an attribute to a relationship. Think of this attribute as a property of tuples in the relationship set. Example. Price is a function of both the bar and the beer, not of one alone. Bars Beers Sells price
54
Attributes on Relationships
Sometimes it is useful to attach an attribute to a relationship. Think of this attribute as a property of tuples in the relationship set. Example. Price is a function of both the bar and the beer, not of one alone. Equivalently, you can create an entity set representing values of the attribute, and make that entity set participate in the relationship. Bars Beers Sells price
55
Attributes on Relationships
Sometimes it is useful to attach an attribute to a relationship. Think of this attribute as a property of tuples in the relationship set. Example. Price is a function of both the bar and the beer, not of one alone. Equivalently, you can create an entity set representing values of the attribute, and make that entity set participate in the relationship. Bars Sells Beers price Bars Beers Sells price Prices
56
Attributes on Relationships
Sometimes it is useful to attach an attribute to a relationship. Think of this attribute as a property of tuples in the relationship set. Example. Price is a function of both the bar and the beer, not of one alone. Equivalently, you can create an entity set representing values of the attribute, and make that entity set participate in the relationship. Bars Sells Beers price Bars Beers Sells price Prices Note convention: arrow from multiway relationship means “all other entity sets together determine a unique one of these.”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.