Top Banner
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009
41

CS 405G: Introduction to Database Systems

Dec 31, 2015

Download

Documents

grant-fernandez

CS 405G: Introduction to Database Systems. Instructor: Jinze Liu Fall 2009. Review. A database is a large collection of integrated data A miniworld is some aspect of the real word, described by facts (data). Topics. Database design ER Model Entities and Attributes - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 405G: Introduction to Database Systems

CS 405G: Introduction to Database Systems

Instructor: Jinze LiuFall 2009

Page 2: CS 405G: Introduction to Database Systems

Review

04/19/23Jinze Liu @ University of Kentucky2

A database is a large collection of integrated data

A miniworld is some aspect of the real word, described by facts

(data)

Page 3: CS 405G: Introduction to Database Systems

Topics

04/19/23Jinze Liu @ University of Kentucky3

Database design ER Model

Entities and Attributes Entity Types, Value Sets, and Key

Attributes Relationships and Relationship Types Weak Entity Types Roles and Attributes in Relationship Types

ER Diagrams – Notation

Page 4: CS 405G: Introduction to Database Systems

Database Design

04/19/23Jinze Liu @ University of Kentucky4

Understand the mini-world being modeled Specify it using a database design model

A few popular ones are: Entity/Relationship (E/R) model UML (Unified Modeling Language)

Intuitive and convenient But not necessarily implemented by DBMS

Translate specification to the data model of DBMS Relational, XML, object-oriented, etc.

Create DBMS schema

Page 5: CS 405G: Introduction to Database Systems

Database Design

04/19/23Jinze Liu @ University of Kentucky5

Page 6: CS 405G: Introduction to Database Systems

An Database Design Example

04/19/23Jinze Liu @ University of Kentucky6

The company is organized into DEPARTMENTs. Each department has a name, number and an employee who manages the department. We keep track of the start date of the department manager.

Each department controls a number of PROJECTs. Each project has a name, number and is located at a single location.

We store each EMPLOYEE’s social security number, address, salary, sex, and birthdate. Each employee works for one department but may work on several projects. We keep track of the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee.

Each employee may have a number of DEPENDENTs. For each dependent, we keep track of their name, sex, birthdate, and relationship to employee.

Page 7: CS 405G: Introduction to Database Systems

04/19/23Jinze Liu @ University of Kentucky7

Page 8: CS 405G: Introduction to Database Systems

Entity-relationship (E/R) model

04/19/23Jinze Liu @ University of Kentucky8

Historically and still very popular Can think of as a “watered-down” object-

oriented design model Primarily a design model—not directly

implemented by DBMS Designs represented by E/R diagrams

there are other styles Very similar to UML diagrams

Page 9: CS 405G: Introduction to Database Systems

Entities and Attributes

04/19/23Jinze Liu @ University of Kentucky9

Entity: A specific object or “thing” in the mini-world that is represented in the database. For example, the EMPLOYEE John Smith,

the Research DEPARTMENT, the ProductX PROJECT.

Attributes: properties used to describe an entity. For example, an EMPLOYEE entity may have a Name, SSN, Address, Sex, BirthDate

A specific entity will have a value for each of its attributes. For example, a specific employee entity may have Name='John Smith', SSN='123456789', Address ='731 Fondren, Houston, TX', Sex='M', BirthDate='09-JAN-55'

Page 10: CS 405G: Introduction to Database Systems

Types of Attributes

04/19/23Jinze Liu @ University of Kentucky10

Simple vs. Composite Attributes Simple: Each entity has a single atomic

value for the attribute. For example, SSN or Sex.

Composite: The attribute may be composed of several components. For example, Name (FirstName, MiddleName, LastName).

Page 11: CS 405G: Introduction to Database Systems

Types of Attributes (cont.)

04/19/23Jinze Liu @ University of Kentucky11

Single-valued vs. Multi-valued. Single-valued: an entity may have at most

one value for the attribute Multi-valued: An entity may have multiple

values for that attribute. For example, PreviousDegrees of a STUDENT. {PreviousDegrees}.

NULL values What if the student does not hold a previous degree? What if the student has a previous degree but the

information is not provided? Apartment number in an address

Page 12: CS 405G: Introduction to Database Systems

Types of Attributes (cont.)

04/19/23Jinze Liu @ University of Kentucky12

Stored vs. derived Number of credit hours a student took in

a semester GPA of a student in a semester

Page 13: CS 405G: Introduction to Database Systems

Key Attributes

04/19/23Jinze Liu @ University of Kentucky13

Entities with the same basic attributes are grouped or typed into an entity type. For example, the EMPLOYEE entity type or the

PROJECT entity type. An attribute of an entity type for which each

entity must have a unique value is called a key attribute of the entity type. For example, SSN of EMPLOYEE. A key attribute may be composite. An entity type may have more than one key.

Page 14: CS 405G: Introduction to Database Systems

SUMMARY OF ER-DIAGRAM NOTATION

04/19/23Jinze Liu @ University of Kentucky14

MeaningENTITY TYPE

ATTRIBUTE

KEY ATTRIBUTE

MULTIVALUED ATTRIBUTE

COMPOSITE ATTRIBUTE

DERIVED ATTRIBUTE

Symbol

Page 15: CS 405G: Introduction to Database Systems

Summary (cont.)

04/19/23Jinze Liu @ University of Kentucky15

Page 16: CS 405G: Introduction to Database Systems

Relationships

04/19/23Jinze Liu @ University of Kentucky16

A relationship relates two or more distinct entities with a specific meaning. For example, EMPLOYEE John Smith works

on the ProductX PROJECT or EMPLOYEE Franklin Wong manages the Research DEPARTMENT.

Relationships of the same type are grouped or typed into a relationship type. For example, the WORKS_ON relationship

type in which EMPLOYEEs and PROJECTs participate, or the MANAGES relationship type in which EMPLOYEEs and DEPARTMENTs participate.

The degree of a relationship type is the number of participating entity types. Both MANAGES and WORKS_ON are binary

relationships.

Page 17: CS 405G: Introduction to Database Systems

Instances of a relationship

04/19/23Jinze Liu @ University of Kentucky17

e1

e2

e3

e4

e5

e6

e7

EMPLOYEE

r1

r2

r3

r4

r5

r6

r7

WORKS_FOR

d1

d2

d3

DEPARTMENT

Page 18: CS 405G: Introduction to Database Systems

Structural Constraints (I)

04/19/23Jinze Liu @ University of Kentucky18

Maximum Cardinality One-to-one (1:1)

One-to-many (1:N) or Many-to-one (N:1)

Many-to-many

Page 19: CS 405G: Introduction to Database Systems

Many-to-one (N:1) RELATIONSHIP

04/19/23Jinze Liu @ University of Kentucky19

e1

e2

e3

e4

e5

e6

e7

EMPLOYEE

r1

r2

r3

r4

r5

r6

r7

WORKS_FOR

d1

d2

d3

DEPARTMENT

Page 20: CS 405G: Introduction to Database Systems

Many-to-many (M:N) RELATIONSHIP

04/19/23Jinze Liu @ University of Kentucky20

e1

e2

e3

e4

e5

e6

e7

r1

r2

r3

r4

r5

r6

r7

p1

p2

p3

r8

r9

EMPLOYEE WORKS_FOR DEPARTMENT

Page 21: CS 405G: Introduction to Database Systems

More Examples

04/19/23Jinze Liu @ University of Kentucky21

Each student may have exactly one account. Each faculty may teach many courses Each student may enroll many courses

Students Enroll Courses

Courses TaughtBy Instructors

Students Own UKY Accounts

Page 22: CS 405G: Introduction to Database Systems

Structural Constraints (II)

04/19/23Jinze Liu @ University of Kentucky22

Minimum Cardinality (also called participation constraint or existence dependency constraints) Zero (partial participation) One or more (total participation)

Page 23: CS 405G: Introduction to Database Systems

Roles in relationships

04/19/23Jinze Liu @ University of Kentucky23

An entity set may participate more than once in a relationship set

May need to label edges to distinguish roles Examples

People are married as husband and wife; label needed

People are roommates of each other; label not needed

Persons Marry

husband

wife

Roommate

Page 24: CS 405G: Introduction to Database Systems

Recursive relationship

04/19/23Jinze Liu @ University of Kentucky24

We can also have a recursive relationship type.

Both participations are same entity type in different roles.

For example, SUPERVISION relationships between EMPLOYEE (in role of supervisor or boss) and (another) EMPLOYEE (in role of subordinate or worker).

In ER diagram, need to display role names to distinguish participations.

Page 25: CS 405G: Introduction to Database Systems

04/19/23Jinze Liu @ University of Kentucky25

Page 26: CS 405G: Introduction to Database Systems

04/19/23 26

Weak Entity Types

A weak entity is an entity that does not have a key attribute

A weak entity must participate in an identifying relationship type with an owner or identifying entity type

Entities are identified by the combination of: A partial key of the weak entity type The particular entity they are related to in the

identifying entity type

Example: Suppose that a DEPENDENT entity is identified

by the dependent’s first name and birthdate, and the specific EMPLOYEE that the dependent is related to. DEPENDENT is a weak entity type with EMPLOYEE as its identifying entity type via the identifying relationship type DEPENDENT_OF

26 04/19/23

Page 27: CS 405G: Introduction to Database Systems

27

Weak Entity-Set Rules A weak entity set has one or more many-one

relationships to other (supporting) entity sets. Not every many-one relationship from a weak

entity set need be supporting. The key for a weak entity set is its own

underlined 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.

Page 28: CS 405G: Introduction to Database Systems

28

ER-Design Techniques1. Avoid redundancy.2. Limit the use of weak entity sets.3. Don’t use an entity set when an attribute

will do.

Page 29: CS 405G: Introduction to Database Systems

29

Avoiding Redundancy Redundancy occurs when we say the same

thing in two different ways. Redundancy wastes space and (more

importantly) encourages inconsistency. The two instances of the same fact may become

inconsistent if we change one and forget to change the other, related version.

Page 30: CS 405G: Introduction to Database Systems

30

Example: Bad

Beers ManfsManfBy

name

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

name

manf

addr

Page 31: CS 405G: Introduction to Database Systems

31

Example: Bad

Beers

name

This design repeats the manufacturer’s address once for each beer; loses the address if there are temporarily no beers for a manufacturer.

manf manfAddr

Page 32: CS 405G: Introduction to Database Systems

32

Example: Good

Beers ManfsManfBy

name

This design gives the address of each manufacturer exactly once.

name addr

Page 33: CS 405G: Introduction to Database Systems

33

Entity Sets Versus Attributes An entity set should satisfy at least

one of the following conditions: It is more than the name of something; it has at least one nonkey attribute.

or It is the “many” in a many-one or

many-many relationship.

Page 34: CS 405G: Introduction to Database Systems

34

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

Page 35: CS 405G: Introduction to Database Systems

35

Example: Good

Beers

name

There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their name.

manf

Page 36: CS 405G: Introduction to Database Systems

36

Example: Good

Beers ManfsManfBy

name

•Manfs deserves to be an entity set because of the nonkey attribute addr.•Beers deserves to be an entity set because it is the “many” of the many-one relationship ManfBy.

name addr

Page 37: CS 405G: Introduction to Database Systems

37

Don’t Overuse Weak Entity Sets

Beginning database designers often doubt that anything could be a key by itself. They make all entity sets weak, supported by all

other entity sets to which they are linked. In reality, we usually create unique ID’s for

entity sets. Examples include social-security numbers,

automobile VIN’s etc.

Page 38: CS 405G: Introduction to Database Systems

38

When Do We Need Weak Entity Sets? The usual reason is that there is no global

authority capable of creating unique ID’s. Example it is unlikely that there could be an agreement

to assign unique player numbers across all football teams in the world.

Page 39: CS 405G: Introduction to Database Systems

39

ER Case Study I Works_In does

not allow an employee to work in a department for two or more periods.

We want to record several values of the descriptive attributes for each instance of this relationship.

name

Employees

ssn lot

Works_In

from todname

budgetdid

Departments

dnamebudgetdid

name

Departments

ssn lot

Employees Works_In

Durationfrom to

39 04/19/23

Page 40: CS 405G: Introduction to Database Systems

ER Case study II Design a database representing cities, counties,

and states For states, record name and capital (city) For counties, record name, area, and location (state) For cities, record name, population, and location

(county and state) Assume the following:

Names of states are unique Names of counties are only unique within a state Names of cities are only unique within a county A city is always located in a single county A county is always located in a single state

40 04/19/23

Page 41: CS 405G: Introduction to Database Systems

Homework Reading

Chapter 3.1-3.6

Project Find your partner

Due - Friday, September 15th Proposal for a given database application

Draw the E-R diagram Due - Friday, October 2nd

04/19/23Jinze Liu @ University of Kentucky41