dbs201: relational databases. agenda entity relationship model discovering attributes identifying...

29
DBS201: Relational Databases

Upload: lawrence-parks

Post on 29-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

DBS201: Relational Databases

Page 2: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Agenda

Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Page 3: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Entity Relationship Diagram

Shows entities – similar to a table structure An entity is an object that exists and is

distinguishable from other objects An entity is represented by a set of attributes An attribute has a domain (i.e. a set of

permitted values) Entities are associated by relationships

Page 4: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Agenda

Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Page 5: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Attributes

Attributes are characteristics of Entities. They describe an entity Different types of attributes:

Simple Composite Single-Valued Multi-Valued

Attributes have a Domain

Page 6: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Attributes

Simple An attribute that can not be divided i.e. FIRST_NAME

Page 7: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Attributes

Composite Attributes that can be further subdivided to yield

additional attributes i.e. NAME can be subdivided into FIRST_NAME,

INITIAL, and LAST_NAME

Page 8: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Attributes

Single-Valued An attribute that can have only a single value Not necessarily a simple attribute i.e. Customer Number, Part Number

Page 9: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Attributes

Multi-Valued An attribute that can have many values i.e. Education Two possible ways to deal with these:

Create attributes for each value Create a new entity composed of the original multi-valued

attributes components

Second approach is preferred

Page 10: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Remember requirements gathering approaches?

Different approaches: bottom-up/top-down Top-down uses an overview/narrative of a

system to discover entities Bottom-up uses screen/report samples to

discover attributes, then these are grouped into entities

Discovering Attributes

Page 11: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Top-down Interview client Look for description of entities (i.e. their

characteristics in a narrative)

Bottom-up Use fields on screen/report samples

Discovering Attributes

Page 12: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

From this example:1. A composer writes/creates many

compositions.

2. The composer includes the name, country, year of birth, year of death and era (e.g. classical, baroque, romantic, or modern).

3. The composition includes a title, type of composition (symphony, concerto, instrumental, chamber, opera, or choral)

Discovering Attributes

attributes

attributes

Attributes – because these describe the entity

Page 13: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Discovering Attributes

Composer

Name Country YearOfBirth YearOfDeath

Composition

Title

Era

Description

CompositionType

Description

The composer includes the name, country, year of birth, year of death and era (e.g. classical, baroque, romantic, or modern).

The composition includes a title, type of composition (symphony, concerto, instrumental, chamber, opera, or choral)

No reference to era; assuming it is another entity

No to composition type; assuming it is another entity

Page 14: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Potential Attributes: Team

Title Player

Name, Address, DateOfBirth, Phone Number Sponsor

Name, Address, Phone Number, Fax Coaches

Name, Address, Phone Number District

Name, Description

Discovering Attributes

Page 15: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Discovering Attributes

Team

Title

Coach

Name Address PhoneNumber

Sponsor

Name Address PhoneNumber Fax

Player

Name Address DateOfBirth PhoneNumber

District

Name Description

Page 16: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

From this example:Multiple Real Estate Listing Service (MRELS) is a company that has offices throughout Southern Ontario. These offices have a number of Real Estate agents working for them. MRELS deals in both residential and commercial real estate. Agents work with customers to get a listing or to find a listing for a customer. A listing will include the address and the type of property (residential or commercial), square footage, annual taxes and closing date. If it is a residential property, it will include number of bedrooms, number of bathrooms, special features (central air, heated by gas or oil, radiators or forced air, for instance). For a commercial property, it will include number of offices as well as number of docking bays (where trucks back up to be loaded/unloaded).

Discovering Attributes

Some attributes described – We will have to assume what some of the attributes might be

Page 17: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Discovering Attributes

Agent

Name PhoneNumber FaxNumber

Office

Name Address PhoneNumber

Customer

Name Address PhoneNumber

CommercialListing

Address SquareFootage AnnualTaxes ClosingDate NumberOfOffices NumberOfDockingBays

ResidentialListing

Address SquareFootage AnnualTaxes ClosingDate NumberOfBedrooms NumberOfBathrooms SpecialFeatures

Potential multi-valued attribute; thereforecreate a new entity

Page 18: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Discovering Attributes

Agent

Name PhoneNumber FaxNumber

Office

Name Address PhoneNumber

Customer

Name Address PhoneNumber

CommercialListing

Address SquareFootage AnnualTaxes ClosingDate NumberOfOffices NumberOfDockingBays

ResidentialListing

Address SquareFootage AnnualTaxes ClosingDate NumberOfBedrooms NumberOfBathrooms

SpecialFeatures

CentralAirFlag HeatingSystem

Page 19: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Agenda

Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Page 20: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

A key is used to identify and locate the information contained in a particular record

A key consists of one or more attributes that determine other attributes

A multi-attribute key is called a composite key

Two types of keys we will address: Primary Key and Foreign Key

Identifying Keys

Identified from original list of attributesIdentified through relationships

Page 21: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Identifying Keys

Primary Key Uniquely identifies any given entity

If an entity does not contain an attribute that would be a good Primary Key candidate, then need to create a new attribute to be the Primary Key

Composer

Name Country YearOfBirth YearOfDeath

Course code could uniquely identify this entity and therefore is a candidate to be a primary key

Course

CourseCode Name Description

None of these attributes could uniquely identify a composer; therefore, need to create a new entity

Course

PK CourseCode

Name Description

Composer

PK ComposerID

Name Country YearOfBirth YearOfDeath

Page 22: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Identifying Keys

What should be the Primary Key for the following examples?

Composition

Title

Era

Description

CompositionType

Description

Page 23: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Identifying Keys

Foreign Key Created automatically through relationships between

entities An attribute whose value matches the primary key

value in the related table

Course

PK CourseCode

Name Description

Section

PK SectionCode

MaxNumStudentsFK1 CourseCode

Page 24: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Agenda

Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Page 25: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Defining Relationships

1:1 Determine most obvious path (i.e. which entity is

the true ‘1’). Take the PK from that true ‘1’ and make it an FK in the other ‘1’

1:M Take the PK from the 1 and make it an FK in the M

M:N Create a bridge table and take the PKs from each

original table and make the PKs and FKs of the bridge table

Page 26: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Agenda

Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Page 27: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Relational Model

Entities translate into tables Attributes translate into fields Table is made up of columns (fields) and rows

(records)

Composer

PK ComposerID

Name Country YearOfBirth YearOfDeath

Composition

PK CompositionID

TitleFK1 ComposerID

Page 28: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Relational ModelComposer

PK ComposerID

Name Country YearOfBirth YearOfDeath

Composition

PK CompositionID

TitleFK1 ComposerID

CompositionID Title ComposerID

111 Rhapsody in Blue G18

222 Second Rhapsody G18

333 Academic Festival Overture B113

444 Tragic Overture B113

555 Ein deutsches Requiem B113

ComposerID Name Country YearOfBirth YearOfDeath

B33 Johann Sebastian Bach Germany 1685 1750

B76 Ludwig van Beethoven Germany 1770 1827

B90 Irving Berlin America 1888 1989

B113 Johannes Brahms Germany 1833 1897

G18 George Gershwin America 1898 1937

G80 Orlando Gibbons England 1583 1625

Primary keys are obvious – they uniquely identify a row.

Foreign keys are obvious – they refer to a field in another table.

Page 29: DBS201: Relational Databases. Agenda Entity Relationship Model Discovering Attributes Identifying Keys Defining Relationships Relational Model

Relational Model

FIGURE P3.23 Table name: TRUCK Database name: Ch03_TransCo

Table name: BASE

Table name: TYPE 1. What are the Primary Keys for each table?2. What are the Foreign Keys for each table?3. Draw the ERD