dbms unit1

58
1–2

Upload: chandra-bhushan

Post on 24-Apr-2015

1.793 views

Category:

Education


3 download

DESCRIPTION

DBMS UNIT1

TRANSCRIPT

Page 1: DBMS UNIT1

1–2

Page 2: DBMS UNIT1

1–3

Page 3: DBMS UNIT1

1–4

Page 4: DBMS UNIT1

1–5

Page 5: DBMS UNIT1

1–6

Entity-Relationship (E-R) Model

COURSE# CTITILE PROF SCHED

ROOMCOURSE

ENROLL

STUDENT

STUID SNAME MAJOR CREDITS

GRADE

Page 6: DBMS UNIT1

1–7

Class Hierarchies

Employees

ssn

name

lot

ISA

HourlyEmps

ContractEmps

Hourlywage

Hoursworked contractid

* Sometimes it’s natural to classify the entities in an entity set into subclass, then we will use Class Hierarchies.

Page 7: DBMS UNIT1

1–8

Aggregationname

ssn lot

Employees

monitors until

since

Sponsors DepartmentProjects

pid

Started-on

pbudget

dname

did budget

* A relationship set is an

association between

entity sets.

Sometimes we have to model a relationship between a collection of entities and relationships, then we need to use aggregation.

Page 8: DBMS UNIT1

1–9

Conceptual design using the ER model

• Should a concept be modeled as an entity or an attribute ?

• Should a concept be modeled as an entity or a relationship ?

• What are the relationships ? Should we use binary or ternary relationships ?

• Should we use aggregation ?

Page 9: DBMS UNIT1

1–10

UNARY RELATIONSHIP• Unary relationship : is an associate of 1 entity set.

* NoteNote : an entity set is a group of related entities.

Student

* Entity set =

* Relation =

Dr. Lee

* Attribute =

Page 10: DBMS UNIT1

1–11

BINARY RELATIONSHIP

• Binary relationship : is an associate of 2 entities sets .

Employees Works_In Department

* Entity set =

* Relation =

* Attribute =

ssn name lot did budgetdname

Page 11: DBMS UNIT1

1–12

TERNARY RELATIONSHIP

Works_InEmployees Department

Locations

Ternary relationship : is an associate of 3 entities sets .

* Entity set =

* Relation =

ssn name lot did dname budget

capadd

* Attribute =

Page 12: DBMS UNIT1

1–13

QUARTERY RELATIONSHIP

Employees Works_In

Locations

Department

Product

Quartery relationship : is an associate of 4 entities sets.

* Entity set =

* Relation =

* Attribute =

Page 13: DBMS UNIT1

1–14

Page 14: DBMS UNIT1

1–15

Additional features of the ER model

• In the ER model allow us to draw important distinctions about the data.

• One of the features of the ER model is :

Key Constraints

Note Note : A key constraints is also known as Primary key.

A primary key is a candidate key selected to uniquely identify all other attribute values in any given row, can not contain null entries.

Page 15: DBMS UNIT1

1–16

An example of Key Constraints on Manage

Manages DepartmentEmployees

ssn

name

lot

Since dname

did budget

ssn = Social Security Number.

did = Department id.

Page 16: DBMS UNIT1

1–17

Page 17: DBMS UNIT1

1–18

Weak Entities

A weak entity : is an entity of which is existence depends on other entities.

Policy DepartmentEmployees

ssn

name

lot

cost

Pname---------

age

Page 18: DBMS UNIT1

1–19

Page 19: DBMS UNIT1

1–20

Page 20: DBMS UNIT1

1–21

Entity versus Attribute

Works-In2 DepartmentEmployees

ssn

name

lot

fromdname

did budget

to

* Entity set =

* Relation =

* Attribute =

Page 21: DBMS UNIT1

1–22

Entity versus Relationship

Manages DepartmentEmployees

ssn

name

apptnum

did budget

dname

Mgr-Appts

lot

since

dbudget

Page 22: DBMS UNIT1

1–23

Binary versus Ternary Relationship

Covers DependentsEmployees

ssn

name

Pname---------

age

Policies

lot

policyid cost

Page 23: DBMS UNIT1

1–24

Using a Ternary Relationship instead of Aggregation

Sponsors DepartmentsProjects

pid

Started-on

did budgetpbudget

dname

Employees

name

ssn lot

Page 24: DBMS UNIT1

1–25

ONE TO MANY RELATIONSHIP

customer

customer-name

customer-citycustomer-id

borrower

customer-street

loan

loan-number

amount

Page 25: DBMS UNIT1

1–26

MANY TO ONE RELATIONSHIP

customer

customer-name

customer-citycustomer-id

borrower

customer-street

loan

loan-number

amount

Page 26: DBMS UNIT1

1–27

ONE TO ONE RELATIONSHIP

customer

customer-name

customer-citycustomer-id

borrower

customer-street

loan

loan-number

amount

Page 27: DBMS UNIT1

1–28

E-R Diagram with Composite, Multivalued, and Derived Attributes

Customer

date-of-birthphone-number

name

customer-id

first-name

age

last-name

middle-name

zip-code

state

city

address

street

Street-number apartment-number

street-name

Page 28: DBMS UNIT1

1–29

WEAK ENTITY SETS

An entity set may not have sufficient attributes to form a primary key. Such an entity set is termed a weak entity set. An entity set that has a primary key is termed a strong entity set.

For a weak entity set to be meaningful, it must be associated with another entity set, called the identifying or owner entity set. The relationship associating the weak entity set with the identifying entity set is called the identifying relationship.

The identifying relationship is many to one from the weak entity set to the identifying entity set, and the participation of the weak entity set in the relationship is total.

Page 29: DBMS UNIT1

1–30

DISCRIMINATOR

The discriminator of a weak entity set is a set of attributes that allows this distinction to be made. For example, the discriminator of a weak entity set payment is the attribute payment-number, since, for each loan a payment number uniquely identifies one single payment for that loan. The discriminator of a weak entity set is also called the partial key of the entity set.

Note: although each payment entity is distinct, payments for different loans may share the same payment-number. Thus, payment entity set does not have a primary key; it is a weak entity set.

The primary key of a weak entity set is formed by the primary key of the identifying entity set, plus the weak entity set’s discriminator.

Page 30: DBMS UNIT1

1–31

E-R DIAGRAM WITH A WEAK ENTITY SET

In a E-R diagrams, a doubly outlined box indicates a weak entity set, and a doubly outlined diamond indicates the corresponding identifying relationship. We underline the discriminator of a weak entity set with a dashed line.

loan

E-R diagram with a weak entity set

loan-payment payment

payment-date

payment-amountpayment-number

amountloan-number

Page 31: DBMS UNIT1

1–32

Page 32: DBMS UNIT1

1–33

Page 33: DBMS UNIT1

1–34

Page 34: DBMS UNIT1

1–35

Page 35: DBMS UNIT1

1–36

Students

Entity/Relationship ModelDiagrams to represent designs.• Entity like object, = “thing.”• Entity set like class = set of “similar”

entities/objects.• Attribute = property of entities in an entity set,

similar to fields of a struct.• In diagrams, entity set rectangle;

attribute oval.ID name phone

height

Page 36: DBMS UNIT1

1–37

Relationships

• Connect two or more entity sets.

• Represented by diamonds.

Students CoursesTaking

Page 37: DBMS UNIT1

1–38

Relationship Set

Think of the “value” of a relationship set as a table.

• One column for each of the connected entity sets.

• One row for each list of entities, one from each set, that are connected by the relationship.

Students Courses

Sally CS180

Sally CS111

Joe CS180

… …

Page 38: DBMS UNIT1

1–39

Multiway RelationshipsUsually binary relationships (connecting two E.S.) suffice.• However, there are some cases where three or more E.S.

must be connected by one relationship.• Example: relationship among students, courses, TA's

(and graders).

Possibly, this E/R diagram is OK:

Students CoursesTaking

Assisting

TA/Graders

Page 39: DBMS UNIT1

1–40

• Works in CS180, because each TA (or grader) is a TA of all students. Connection student-TA is only via the course.

• But what if students were divided into sections, each headed by a TA? Then, a student in CS180 would be related to

only one of the TA's for CS180. Which one?

• Need a 3-way relationship to tell.

Page 40: DBMS UNIT1

1–41

Students Courses TAsAnn CS180 JanSue CS180 PatBob CS180 Jan… … …

Students

Courses

TAs

Enrolls

Page 41: DBMS UNIT1

1–42

Beers-Bars-Drinkers Example

• Our running example for the course.name addr license

name manf name addr

Beers Drinkers

BarsServes Frequents

Likes

Page 42: DBMS UNIT1

1–43

Multiplicity of Relationships

Representation of Many-One

• E/R: arrow pointing to “one.” Rounded arrow = “exactly one.”

Many-many Many-one One-one

Page 43: DBMS UNIT1

1–44

Example:Drinkers Have Favorite Beers

name addr license

name manf name addr

Beers Drinkers

BarsServes Frequents

Likes

Favorite

Page 44: DBMS UNIT1

1–45

One-One Relationships

Put arrows in both directions.

Design Issue:Is the rounded arrow justified?Design Issue:Here, manufacturer is an E.S.In earlier diagrams it is an attribute.Which is right?

Manfs BeersBest-seller

Page 45: DBMS UNIT1

1–46

Attributes on Relationships

• Shorthand for 3-way relationship:

Bars BeersSells

price

Bars BeersSells

price

Prices

Page 46: DBMS UNIT1

1–47

• A true 3-way relationship. Price depends jointly on beer and bar.

• Notice arrow convention for multiway relationships: “all other E.S. determine one of these.” Not sufficiently general to express any

possibility. However, if price, say, depended only on the

beer, then we could use two 2-way relationships: price-beer and beer-bar.

Or better: just make price an attribute of beer.

Page 47: DBMS UNIT1

1–48

Converting Multiway to 2-Way• Baroque in E/R, but necessary in certain “object-oriented” models.

• Create a new connecting E.S. to represent rows of a relationship set. E.g., (Joe's Bar, Bud, $2.50) for the Sells relationship.

• Many-one relationships from the connecting E.S. to the others.

Bars Beers

The-Bar

Price

The-Beer

The-Price

BBP

Page 48: DBMS UNIT1

1–49

Roles

Sometimes an E.S. participates more than once in a relationship.

• Label edges with roles to distinguish.

Husband Wifed1 d2

d3 d4

… …Drinkers

Married

husband wife

Page 49: DBMS UNIT1

1–50

• Notice Buddies is symmetric, Married not. No way to say “symmetric” in E/R.

Design Question

Should we replace husband and wife by one relationship spouse?

Buddy1 Buddy2d1 d2

d1 d3

d2 d1

d2 d4

… …Drinkers

Buddies

1 2

Page 50: DBMS UNIT1

1–51

More Design Issues

1. Subclasses.

2. Keys.

3. Weak entity sets. (Next class.)

Page 51: DBMS UNIT1

1–52

Subclasses

Subclass = special case = fewer entities = more properties.

• Example: Ales are a kind of beer. In addition to the properties (= attributes and relationships) of beers, there is a “color” attribute for ales.

Page 52: DBMS UNIT1

1–53

E/R Subclasses• Assume subclasses form a tree (no multiple

inheritance).• isa triangles indicate the subclass relation.

name manfBeers

Alescolor

isa

Page 53: DBMS UNIT1

1–54

Different Subclass Viewpoints1. E/R viewpoint: An entity has a component in each entity set

to which it logically belongs. Its properties are the union of the properties of these E.S.

2. Contrasts with object-oriented viewpoint: An object (entity) belongs to exactly one class. It inherits properties

of its superclasses.name manfBeers

Alescolor

isa Pete’s Ale

Page 54: DBMS UNIT1

1–55

Multiple InheritanceTheoretically, an E.S. could be a subclass of

several other entity sets.name manf

Beers

name manf

Wines

GrapeBeers

isaisa

Page 55: DBMS UNIT1

1–56

Problems

How should conflicts be resolved?• Example: manf means vintner for wines,

bottler for beers. What does manf mean for “grape beers”?

• Need ad-hoc notation to resolve meanings.• In practice, we shall assume a tree of entity

sets connected by isa, with all “isas” pointing from child to parent.

Page 56: DBMS UNIT1

1–57

Keys

A key is a set of attributes whose values can belong to at most one entity.

• In E/R model, every E.S. must have a key. It could have more than one key, but one set of

attributes is the “designated” key.

• In E/R diagrams, you should underline all attributes of the designated key.

Page 57: DBMS UNIT1

1–58

Example• Suppose name is key for Beers.

• Beer name is also key for ales. In general, key at root is key for all.

name manfBeers

Alescolor

isa

Page 58: DBMS UNIT1

1–59

Example: A Multiattribute Key

• Possibly, the combination of hours + room also forms a key, but we have not designated it as such.

dept roomCourses

number hours