notes of awesome

Upload: frank-ong

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Notes of Awesome

    1/69

    1

    High Level Data Modeling with

    Entity-Relationship Diagrams

    Modeling tools (E/R Diagrams)

    Special techniques (Weak Entity Sets)Converting to Relations (Schema Definition)

  • 7/29/2019 Notes of Awesome

    2/69

    2

    Purpose of E/R Model

    The E/R model allows us to sketchdatabase schema designs.

    Includes some constraints, but not operations.

    Designs are pictures calledentity-relationship diagrams.

    Simple notation - Easy communication

    Later: convert E/R designs to relational DBdesigns.

    Close to 3NF or BCNF Normal Form

  • 7/29/2019 Notes of Awesome

    3/69

    3

    Framework for E/R

    Design is a serious business.

    Only few times in your life

    The boss knows they want adatabase, but they dont know whatthey want in it.

    I/O based modeling

    Sketching the key components is anefficient way to develop a workingdatabase.

    Data model independent design templates

  • 7/29/2019 Notes of Awesome

    4/69

    4

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

    Attributes are simple values, e.g. integers ordecimals, character strings, dates, enumsnot structs, sets, arrays, bags, lists etc.

  • 7/29/2019 Notes of Awesome

    5/69

    5/48

    Dataset

    2

    NoOfWheels

    PlateNo

    Age

    DrivingLicence

    Name

    Type

    Entities

    Data components

    Entity sets

    23

    E

    Joe

    Ferrari 007

  • 7/29/2019 Notes of Awesome

    6/69

    6

    Notation methods

    ERD: Entity-RelationshipDiagram

    Designed for pencil andpaper (Chen, 1970)

    UML: Unified ModelingLanguage (Rational, 1990)

    Designed for CASE tools

    ODL: Object DefinitionLanguage (ODMG, 1997)

    Formal language for static

    OO design

  • 7/29/2019 Notes of Awesome

    7/69

    7

    Example:

    Entity set Beers has two attributes, name and

    manf(manufacturer).Each Beers entity has values for these two

    attributes, e.g. b1: (Bud, Anheuser-Busch)tuple in the relational data model

    Beers

    name manf

    B (finite set)

    b 1

    b2

    b3

    b

    n

  • 7/29/2019 Notes of Awesome

    8/69

    8

    RelationshipsArelationship connects two or more

    entity sets.

    Binary relationships (ERD, UML, ODL)

  • 7/29/2019 Notes of Awesome

    9/69

    9

    Example: Relationships

    Drinkers addrname

    Beers

    manfname

    Bars

    name

    license

    addr

    Note:license =beer, full,none

    Sells Bars sell some

    beers.

    Likes

    Drinkers likesome beers.Frequents

    Drinkers frequentsome bars.

  • 7/29/2019 Notes of Awesome

    10/69

    10

    Relationship Set

    The current value of an entity set is theset of entities that belong to it.

    Example: the set of all bars in our database.

    The value of a relationship is arelationship set, a set of tuples with one

    component for each related entity set.Time is ignored: relationship may occur

    between entities any time in the future.

  • 7/29/2019 Notes of Awesome

    11/69

    11

    Example: Relationship Set

    For the relationship Sells, we mighthave a relationship set like:

    Bar BeerJoes Bar BudJoes Bar Miller

    Sues Bar BudSues Bar Petes AleSues Bar Bud Lite

  • 7/29/2019 Notes of Awesome

    12/69

    12

    Multiway Relationships

    Sometimes, we need a relationship thatconnects more than two entity sets.

    Only in ERD, but not in UML neither ODL

    Suppose that drinkers will only drinkcertain beers at certain bars.

    Our three binary relationships Likes, Sells,

    and Frequents do not allow us to make thisdistinction.

    But a 3-way relationship would.

  • 7/29/2019 Notes of Awesome

    13/69

    13

    Example: 3-Way Relationship

    Bars Beers

    Drinkers

    name nameaddr manf

    name addr

    license

    Preferences

  • 7/29/2019 Notes of Awesome

    14/69

    14

    A Typical Relationship Set

    Bar Drinker BeerJoes Bar Ann Miller

    Sues Bar Ann BudSues Bar Ann Petes AleJoes Bar Bob BudJoes Bar Bob Miller

    Joes Bar Cal MillerSues Bar Cal Bud Lite

  • 7/29/2019 Notes of Awesome

    15/69

    Decomposition of MultiwayRelationships

    15/47

    Bars Beers

    Drinkers

    name nameaddr manf

    name addr

    license

    Preferences

  • 7/29/2019 Notes of Awesome

    16/69

    16

    Many-Many (M:N)

    RelationshipsFocus: binary relationships,

    such as Sells between Bars and

    Beers.In a many-many relationship,

    an entity of either set can be

    connected to many entities ofthe other set.

    E.g., a bar sells many beers; a

    beer is sold by many bars.

    many-many

  • 7/29/2019 Notes of Awesome

    17/69

    17

    Many-One (M:1) Relationships

    Some binary relationships aremany-onefrom one entity set toanother.

    Each entity of the first set isconnected to at most one entity ofthe second set.

    But an entity of the second set canbe connected to zero, one, ormany entities of the first set.

    many-one

  • 7/29/2019 Notes of Awesome

    18/69

    18

    Example: Many-One Relationship

    Favorite, from Drinkers to Beers ismany-one.

    A drinker has at most one favorite beer.

    But a beer can be the favorite of anynumber of drinkers, including zero.

    Drinkers BeersFavorite

  • 7/29/2019 Notes of Awesome

    19/69

    19

    One-One (1:1) Relationships

    In a one-onerelationship, eachentity of either entity set is relatedto at most one entity of the other

    set.Example: Relationship Best-seller

    between entity sets Manfs

    (manufacturer) and Beers.A beer cannot be made by more

    than one manufacturer, and nomanufacturer can have more than

    one best-seller (assume no ties).

    one-one

  • 7/29/2019 Notes of Awesome

    20/69

    Multiplicity Notations

    ERD: any kind of theta condition (ie.

  • 7/29/2019 Notes of Awesome

    21/69

    21

    Representing Multiplicityin

    ERDShow a many-one relationship by an

    arrow entering the one side.

    Remember: Like a functional dependency.

    Show a one-one relationship by arrowsentering both entity sets.

    Rounded arrow= exactly one, i.e.,each entity of the first set is related toexactly one entity of the target set.

    0..1

    1..1

  • 7/29/2019 Notes of Awesome

    22/69

    22

    Example: Many-One Relationship

    Drinkers BeersLikes

    Favorite Notice: two relationshipsconnect the same entitysets, but are different.

  • 7/29/2019 Notes of Awesome

    23/69

    23

    Example: One-One Relationship

    Consider Best-seller between Manfs and Beers.

    Some beers are not the best-seller of anymanufacturer, so a rounded arrow to Manfswould be inappropriate.

    But a beer manufacturer has to have a best-seller.

    Manfs BeersBest-seller

    A manufacturer hasexactly one bestseller.

    A beer is the best-seller for 0 or 1manufacturer.

  • 7/29/2019 Notes of Awesome

    24/69

    VisualizingMultiplicity

    24/47

  • 7/29/2019 Notes of Awesome

    25/69

    25

    Attributes on Relationships

    It is allowed to use only in EERDs!

    Sometimes it is useful to attach an attributeto a relationship.

    Think of this attribute as a property oftuples in the relationship set.

    Bars BeersSells

    pricePrice is a function of

    both the bar and the beer, not of one alone.

  • 7/29/2019 Notes of Awesome

    26/69

    26

    Equivalent Diagrams WithoutAttributes on Relationships

    Create an entity set representingvalues of the attribute.

    Make that entity set participate in the

    relationship.Bars BeersSells

    price

    PricesNote convention: arrowfrom multiway relationship= all other entity setstogether determine aunique one of these.

  • 7/29/2019 Notes of Awesome

    27/69

    27

    Equivalent Diagrams WithoutAttributes on Relationships

    Create a connecting entity setrepresenting values of the attribute.

    Split the relationship into two binaryones.

    Bars Beers

    price

    Prices SellsWhatSellsWhere

    A i i d C i Cl

  • 7/29/2019 Notes of Awesome

    28/69

    Association and Connecting Classes

    28/47

  • 7/29/2019 Notes of Awesome

    29/69

    29

    1-way Relationships

    Sometimes an entity set appears morethan once in a relationship.

    Label the edgesbetween the

    relationship andthe entity set withnames called roles.

  • 7/29/2019 Notes of Awesome

    30/69

    30

    Example: Roles

    Drinkers

    Buddies

    1 2

    Relationship Set

    Buddy1 Buddy2

    Bob AnnJoe SueAnn BobJoe Moe

    Relationship Set

    Husband WifeBob AnnJoe Sue

    Drinkers

    Married

    husband wife

    K

  • 7/29/2019 Notes of Awesome

    31/69

    31

    Keys

    Akey is a set of attributes for one entity

    set such that no two entities in this setagree on all the attributes of the key.

    It is allowed for two entities to agree on some,

    but not all, of the key attributes.We must designate

    a key for every entityset.

    ERD: exactly one

    UML: at most one

    ODL: zero or more

  • 7/29/2019 Notes of Awesome

    32/69

    32

    Example: a Multi-attribute Key

    Courses

    dept number hours room

    Note that hours and room could also serve as akey, but we must select only one key.

  • 7/29/2019 Notes of Awesome

    33/69

    33

    Weak Entity Sets

    Occasionally, entities of an entity setneed help to identify them uniquely.

    Entity set E is said to be weak if inorder to identify entities ofE uniquely,we need to follow one or more many-

    one relationships from E and includethe key of the related entities from theconnected entity sets.

  • 7/29/2019 Notes of Awesome

    34/69

    34

    Example: Weak Entity Set

    name is almost a key for football players, butthere might be two with the same name.

    number is certainly not a key, since playerson two teams could have the same number.

    But number, together with the team name

    related to the player by Plays-on should beunique.

  • 7/29/2019 Notes of Awesome

    35/69

    35

    In E/R Diagrams

    Players TeamsPlays-on

    name namenumber

    Double diamond for supporting many-one relationship. Double rectangle for the weak entity set.

    Note: must be roundedbecause each player needsa team to help with the key.

  • 7/29/2019 Notes of Awesome

    36/69

    WeakEntity SetNotations

    36

    ERD: With supportingrelationships.

    UML: Withcompositions.

    ODL: There is nosuch thing as

    weak class

  • 7/29/2019 Notes of Awesome

    37/69

    37

    Weak Entity-Set Rules

    A weak entity set has one or moremany-one relationships to other

    (supporting) entity sets. Not every many-one relationship from a

    weak entity set need be supporting.

    But supporting relationships must have arounded arrow (entity at the one end isguaranteed).

  • 7/29/2019 Notes of Awesome

    38/69

    38

    Weak Entity-Set Rules (2)

    The key for a weak entity set is its ownunderlined attributes and the keys for

    the supporting entity sets. E.g., (player) number and (team) name is

    a key for Players in the previous example.

  • 7/29/2019 Notes of Awesome

    39/69

    39

    Design Techniques

    1. Avoid redundancy.

    2. Limit the use of weak entity sets.

    3. Dont use an entity set when anattribute will do.

    4. Use different attribute names.

    5. Names are usually case insensitive,but use the camelCase notation style.

  • 7/29/2019 Notes of Awesome

    40/69

    40

    Avoiding Redundancy

    Redundancy = saying the same thingin two (or more) different ways.

    Wastes space and (more importantly)encourages inconsistency.

    Two representations of the same fact

    become inconsistent if we change one andforget to change the other.

    Recall anomalies due to FDs.

  • 7/29/2019 Notes of Awesome

    41/69

    41

    Example: Bad

    Beers ManfsManfBy

    name

    This design states the manufacturer of a beertwice: as an attribute and as a related entity.

    name

    manf

    addr

  • 7/29/2019 Notes of Awesome

    42/69

    42

    Example: Good

    Beers ManfsManfBy

    name

    This design gives the address of eachmanufacturer exactly once.

    name addr

  • 7/29/2019 Notes of Awesome

    43/69

    43

    Example: Bad

    Beers

    name

    This design repeats the manufacturers addressonce for each beer and loses the address if thereare temporarily no beers for a manufacturer.

    manf manfAddr

  • 7/29/2019 Notes of Awesome

    44/69

    44

    Example: Good

    Beers ManfsManfBy

    name

    Manfs deserves to be an entity set because ofthe nonkey attribute addr.Beers deserves to be an entity set because it isthe many of the many-one relationship ManfBy.

    name addr

  • 7/29/2019 Notes of Awesome

    45/69

    45

    Entity Sets Versus Attributes

    An entity set should satisfy at leastone of the following conditions:

    It is more than the name of something; ithas at least one nonkey attribute.

    or

    It is the many in a many-one or many-many relationship.

  • 7/29/2019 Notes of Awesome

    46/69

    46

    Example: Bad

    Beers ManfsManfBy

    name

    Since the manufacturer is nothing but a name,and is not at the many end of any relationship,it should not be an entity set.

    name

  • 7/29/2019 Notes of Awesome

    47/69

    47

    Example: Good

    Beers

    name

    There is no need to make the manufacturer anentity set, because we record nothing aboutmanufacturers besides their name.

    manf

  • 7/29/2019 Notes of Awesome

    48/69

    E/R model transformation is an iterative process

    1. Identify

    entity sets

    2. Identify

    relationships

    3. Determine

    multiplicities

    4. Determine

    attributes

    5. E/R diagram

    justification

    6. Checking,

    Validation

    T1 Splitting

    entity sets

    T2 Generalization,

    specialization

    T3 Division into

    independent

    entity sets

    T4 Using parallel

    relationships

    T5 Splitting a

    relationship

    T6 Adding

    attributes

    T7 Splitting

    attributes

    vagy

  • 7/29/2019 Notes of Awesome

    49/69

    49

    From E/R Diagrams to Relations

    Entity set -> relation.

    Attributes -> attributes.

    Relationships -> relations whoseattributes are only:

    The keys of the connected entity sets.

    Attributes of the relationship itself.

  • 7/29/2019 Notes of Awesome

    50/69

    50

    Entity Set -> Relation

    Relation: Beers(name, manf)

    Beers

    name manf

  • 7/29/2019 Notes of Awesome

    51/69

    51

    Relationship -> Relation

    Drinkers BeersLikes

    Likes(drinker, beer)Favorite

    Favorite(drinker, beer)

    Married

    husband

    wife

    Married(husband, wife)

    name addr name manf

    Buddies

    1 2

    Buddies(name1, name2)

    C bi i R l ti

  • 7/29/2019 Notes of Awesome

    52/69

    52

    Combining Relations OK to combine into one relation:

    1. The relation for an entity-set E2. The relations for many-one relationships

    of which E is the many.

    Example: Drinkers(name, addr) andFavorite(drinker, beer) combine tomake Drinker1(name, addr, favBeer).

    Drinkers BeersFavourite

    name addr name manf

  • 7/29/2019 Notes of Awesome

    53/69

    53

    Risk with Many-Many Relationships

    Combining Drinkers with Likes would bea mistake. It leads to redundancy, as:

    name addr beerSally 123 Maple BudSally 123 Maple Miller

    Redundancy

    Drinkers BeersLikes

    name addr name manf

  • 7/29/2019 Notes of Awesome

    54/69

    54

    Handling Weak Entity Sets

    Relation for a weak entity set mustinclude attributes for its complete key

    (including those belonging to otherentity sets), as well as its own, nonkeyattributes.

    A supporting relationship is redundantand yields no relation (unless it hasattributes).

    E l W k E tit S t

  • 7/29/2019 Notes of Awesome

    55/69

    55

    Example: Weak Entity Set ->Relation

    Logins HostsAt

    name name

    Hosts(hostName, location)Logins(loginName, hostName, billTo)

    At(loginName, hostName, hostName2)

    Must be the same

    billTo

    At becomes part ofLogins

    location

  • 7/29/2019 Notes of Awesome

    56/69

    56

    Dont Overuse Weak Entity Sets

    Beginning database designers often doubtthat anything could be a key by itself.

    They make all entity sets weak, supported by allother entity sets to which they are linked.

    In reality, we usually create unique IDs for

    entity sets. Examples include social-security numbers,

    automobile VINs etc.

  • 7/29/2019 Notes of Awesome

    57/69

    57

    When Do We Need Weak

    Entity Sets?The usual reason is that there is no

    global authority capable of creating

    unique IDs.Example: it is unlikely that there could

    be an agreement to assign unique

    player numbers across all football teamsin the world.

  • 7/29/2019 Notes of Awesome

    58/69

    58

    E/Rdiagramof theJoomla 1.5portalsystem

    Now let

    us go topharmacies

  • 7/29/2019 Notes of Awesome

    59/69

    59

    Subclasses

    Subclass= special case = fewerentities = more properties.

    Example: Ales are a kind of beer. Not every beer is an ale, but some are.

    Let us suppose that in addition to all the

    properties(attributes and relationships) ofbeers, ales also have the attribute color.

  • 7/29/2019 Notes of Awesome

    60/69

    60

    Subclasses in E/R Diagrams

    Assume subclasses form a tree.

    I.e., no multiple inheritance.

    Isa triangles indicate the subclassrelationship.

    Point to the superclass.

    In an Isa hierarchy, only the root entityset has a key, and it must serve as thekey for all entities in the hierarchy.

  • 7/29/2019 Notes of Awesome

    61/69

    61

    Example: Subclasses

    name is Key for Beers

    Beers

    Ales

    isa

    name manf

    color

    Notations of Inheritance

  • 7/29/2019 Notes of Awesome

    62/69

    Notations of Inheritance

    62

  • 7/29/2019 Notes of Awesome

    63/69

    63

    E/R Vs. Object-Oriented Subclasses

    In OO, objects are in one class only.

    Subclasses inherit from superclasses.

    In contrast, E/R entities haverepresentatives in all subclasses towhich they belong.

    Rule: if entity eis represented in a subclass,then eis represented in the superclass (andrecursively up the tree).

  • 7/29/2019 Notes of Awesome

    64/69

    64

    Example: Representatives of

    Entities

    Beers

    Ales

    isa

    name manf

    color

    Petes Ale

  • 7/29/2019 Notes of Awesome

    65/69

    65

    Subclasses: Three Approaches

    1. Object-oriented: One relation per subset ofsubclasses, with all relevant attributes.

    2. Use nulls: One relation; entities have NULLin attributes that dont belong to them.

    3. E/R style: One relation for each subclass:

    Key attribute(s). Attributes of that subclass.

  • 7/29/2019 Notes of Awesome

    66/69

    66

    Example: Subclass -> Relations

    Beers

    Ales

    isa

    name manf

    color

  • 7/29/2019 Notes of Awesome

    67/69

    67

    Object-Oriented

    name manf

    Bud Anheuser-Busch

    Beers

    name manf colorSummerbrew Petes dark

    Ales

    Good for queries like find thecolor of ales made by Petes.

  • 7/29/2019 Notes of Awesome

    68/69

    68

    E/R Style

    name manfBud Anheuser-BuschSummerbrew Petes

    Beers

    name colorSummerbrew dark

    Ales

    Good for queries likefind all beers (includingales) made by Petes.

  • 7/29/2019 Notes of Awesome

    69/69

    Using Nulls

    name manf color

    Bud Anheuser-Busch NULLSummerbrew Petes darkBeers

    Saves space unless there are lotsof attributes that are usually NULL.