lecture 2: e/r diagrams and the relational model wednesday, april 3 rd, 2002

60
Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd , 2002

Upload: ethelbert-carter

Post on 05-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Lecture 2: E/R Diagrams and the Relational Model

Wednesday, April 3rd, 2002

Page 2: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Outline

• E/R Diagrams– Reading assignment: 2.4 (except 2.4.2, 2.4.5), 2.5

(except 2.5.4)

• The Relational Model– Reading assignment: 3.1, 3.2, 3.3

• E/R to Relational:– Reading assignment: 3.5

• SQL– Reading assignment: 5.1, 5.2

NOTE: lots of material in this lecture. Please read the book.Will review some key issues in the next lecture

Page 3: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

E/R Diagrams: Review

Product

address

buys

Entity sets:

Properties:

Relationships:

One-one

Many-one

Many-many

Page 4: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Multi-way Relationships in E/RHow do we model a purchase relationship between buyers,products and stores?

Purchase

Product

Person

Store

Can still model as a mathematical set (how ?)

Page 5: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Arrows in Multi-way Relationships (not in the book)

Purchase

Product

Person

Store

“A person buys aproduct at most once”

Limited expressive power.Cannot say: “a person buys at most one product”

Page 6: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

• Q: what does the arrow mean ?

• A: if I know the store, person, invoice, I know the movie too

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 7: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

• Q: what do these arrow mean ?

• A: store, person, invoice determines movie and store, invoice, movie determines person

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 8: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

• Q: how do I say: “invoice determines store” ?

• A: no good way; best approximation:

• Why is this incomplete ?

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships

Page 9: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Converting Multi-way Relationships to Binary

Purchase

Product

Person

Store

date

Page 10: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Converting Multi-way Relationships to Binary

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

Moral: Find a nice way to say things.

date

Page 11: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Subclasses and Inheritance in E/R

Some objects (entities) in a class may be special•define a new class•better: define a subclass that inherits the properties of the superclass

Products

Software products

Educational products

We define subclasses in E/R

Page 12: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Product

name category

price

isa isa

Educational ProductSoftware Product

Age Groupplatforms

Subclasses in E/R

Page 13: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Understanding Subclasses

• Think in terms of records:– Product

– SoftwareProduct

– EducationalProduct

field1

field2

field1

field2

field1

field2

field3

field4field5

Page 14: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

• C++: classes are disjoint

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Difference between C++ and E/R inheritance

Product

SoftwareProductEducationalProduct

Page 15: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

• E/R: entity sets overlap

Difference between C++ and E/R inheritance

SoftwareProduct

EducationalProduct

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Product

Page 16: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Difference between C++ and E/R inheritance

• No need for multiple inheritance in E/R

• we have three entity sets, but four different kinds of objects

SoftwareProduct

EducationalProduct

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Product

esp1 esp2

Page 17: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Modeling Union Types in E/R

FurniturePiece

Person Company

Say: each piece of furniture is owned either by a person, or by a company

Page 18: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Modeling Union Types in E/R

Say: each piece of furniture is owned either by a person, or by a company

Solution 1. Acceptable, imperfect (What’s wrong ?)

FurniturePiecePerson Company

ownedByPerson ownedByPerson

Page 19: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Modeling Union Types in E/R

Solution 2: better, more laborious

isa

FurniturePiecePerson Company

ownedBy

Owner

isa

Page 20: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Database Constraints

• A constraint = an assertion about the database that must be true at all times

• part of the db schema

• types in programming languages do not have anything similar

• correspond to invariants in programming languages

PLEASE READ THE TEXTBOOK

Page 21: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Database ConstraintsVery important in databases

Keys: social security number uniquely identifies a person.

Single-value constraints: e.g. one-one, one-many, many-one

Participation constrain: total many-one

Domain constraints: peoples’ ages are between 0 and 150.

General constraints: all others (at most 50 students enroll in a class)

Page 22: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

KeysA set of attributes that uniquely identify an object or entity:

Person: ssn name name + address name + address + age

Perfect keys are often hard to find, so organizations usuallyinvent something.

An object may have multiple keys:

employee number, ssn

Page 23: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Keys in E/R Diagrams

address name ssn

Person

Product

name category

price

No formal way to specify multiple keys in E/R diagrams

Page 24: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Single Value Constraints in E/R

makes CompanyProduct

A product is made by at most one company:

Notice: some products are not made by any company

Page 25: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Participation Constraint

makes CompanyProduct

makes CompanyProduct

Each product is made by a lest one company

(notation from the book)

Each product is made by exactly one company

This: also called referential integrity constraint

Page 26: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Referential Integrity Constraint

• Another notation (in Ullman’s book):

CompanyProduct makes

CompanyProduct makes

Page 27: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Weak Entity SetsEntity sets are weak when their key attributes come from otherclasses to which they are related.

This happens if:

- part-of hierarchies - splitting n-ary relations to binary.

UniversityTeam affiliation

numbersport name

Page 28: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

The Relational Data ModelDatabase Model(E/R)

Relational Schema

Physicalstorage

Diagrams (E/R) Tables SQL column names: attributes rows: tuples

Complexfile organizationand index structures.

Page 29: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Terminology

tuples

Attribute namesTable name

Products:

Name Price Category Manufacturer

Gizmo $19.99 Gadgets Gizmo Works

Power gizmo $29.99 Gadgets Gizmo Works

SingleTouch $149.99 Photography Canon

MultiTouch $299.99 Household Canon

Page 30: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Creating Tables in SQL

CREATE TABLE Person(

name VARCHAR(30), price REAL, category VARCHAR(20), manufacturer VARCHAR(30) );

CREATE TABLE Person(

name VARCHAR(30), price REAL, category VARCHAR(20), manufacturer VARCHAR(30) );

Domains: CHAR, VARCHAR, INTEGER, REAL, etc, etc

Page 31: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Inserting Tuples SQL

INSERTINTO Person(name, price, category, manufacturer)VALUES (“Gizmo”, 19.99, “Gadgets”, “Gizmo Works”)

INSERTINTO Person(name, price, category, manufacturer)VALUES (“Gizmo”, 19.99, “Gadgets”, “Gizmo Works”)

INSERTINTO Person(name)VALUES (“Power gizmo”)

INSERTINTO Person(name)VALUES (“Power gizmo”)

What happens withthe missing fields ?

Page 32: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Tables with Default Values

Specifying default values:

The default of defaults: NULL

CREATE TABLE Person(

name VARCHAR(30), price REAL DEFAULT 99.99, category VARCHAR(20) DEFAULT “General”, manufacturer VARCHAR(30) );

CREATE TABLE Person(

name VARCHAR(30), price REAL DEFAULT 99.99, category VARCHAR(20) DEFAULT “General”, manufacturer VARCHAR(30) );

Page 33: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Foundations of theRelational Model

• Relational Schema:

– Relation name plus attribute names

– E.g. Product(Name, Price, Category, Manufacturer)

– In practice we add the domain for each attribute

• Database Schema:

– Set of relational schemas

– E.g. Product(Name, Price, Category, Manufacturer) Vendor(Name, Address, Phone)

Page 34: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Foundations of theRelational Model

• An instance of a relational schema R(A1,…,Ak), is a relation with k attributes with values of corresponding domains

• An instance of a database schema R1(…), R2(…), …, Rn(…), consists of n relations, each an instance of the corresponding relational schema.

Page 35: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

ExampleRelational schema: Product(Name, Price, Category, Manufacturer)Relational instance:

Name Price Category Manufacturer

Gizmo $19.99 Gadgets Gizmo Works

Power gizmo $29.99 Gadgets Gizmo Works

SingleTouch $149.99 Photography Canon

MultiTouch $299.99 Household Canon

Page 36: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Schemas and Instances

• Analogy with programming languages:– Schema = type– Instance = value

• Important distinction:– Database Schema = stable over long periods of time– Database Instance = changes constantly, as data is

inserted/updated/deleted

Page 37: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Two Mathematical Definitions of Relations

Definition A relation R(dom1, ..., domn) is a subset of the cartesian product: R dom1 ... domn

Example• Product string x real x string x string• Order in the tuple is important !

– (gizmo, 19, gadgets, GizmoWorks)

– (gizmo, 19 , GizmoWorks, gadgets)

• No attributes

Page 38: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Two Mathematical Definitions of Relations

Definition A relation R(A1:dom1, ..., An:domn) is a set of functions t: {A1, ..., An} dom1 ... domn

s.t. t(A1) A1, ..., t(An) An

Example• A={name , price, category, manufacturer}• Example of a tuple:

• Order in a tuple is not important• Attribute names are important

{name gizmo, price 19, category gadgets, manufacturer gizmoWorks}

{name gizmo, price 19, category gadgets, manufacturer gizmoWorks}

Page 39: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Two Definitions of Relations

• We will switch back and forth between these two:– Relational schemas with attribute names– Positional tuples, without attribute names

Page 40: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

From E/R Diagrams to Relational Schema

• Each entity set relation

• Each relationship relation

• Special cases:– one-one, many-one relationships– subclasses– weak entity sets

Page 41: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

address name ssn

Person

buys

makes

employs

CompanyProduct

name category

Stock price

name

price

Convert toFive tables (why ?):

Product(name, price, category, cname)Company(name, stockPrice)Person(ssn, name, address)Buys(pname, ssn)Employs(cname, ssn)

Product(name, price, category, cname)Company(name, stockPrice)Person(ssn, name, address)Buys(pname, ssn)Employs(cname, ssn)

Page 42: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Entity Sets to Relations

Product

name category

price

Product:

Name Category Price

gizmo gadgets $19.99

Page 43: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Relationships to Relations

makes CompanyProduct

name category

Stock price

name

Relations: Product, Makes, Company:

Product-name Product-Category Company-name Starting-year

gizmo gadgets gizmoWorks 1963

Start Year

price

Page 44: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Many-one Relationships

makes CompanyProduct

name category

Stock price

name

No need for Makes. Just modify Product:

name category price StartYear companyName

gizmo gadgets 19.99 1963 gizmoWorks

Start Year

price

Page 45: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Handling Weak Entity Sets

UniversityTeam affiliation

numbersport name

Relation Team:

Sport Number Affiliated University

mud wrestling 15 Montezuma State U.

- need all the attributes that contribute to the key of Team - don’t need a separate relation for Affiliation. (why ?)

Page 46: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Modeling Subclass Structure

Product

Educational Product

SoftwareProduct

ageGrouptopic

Platformsrequired memory isaisa

Page 47: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

The right way

Product(name, price, category, manufacturer)

EducationalProduct( name, ageGroup, topic)

SoftwareProduct( name, platforms, requiredMemory)

Notice: each subclass stores only the additional attributes

Page 48: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Option #2: The Null Value Approach

Have one table:

Product ( name, price, manufacturer, age-group, topic, platforms, required-memory, educational-method)

Some values in the table will be NULL, meaning that the attribute not make sense for the specific product.

Page 49: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

SQL IntroductionStandard language for querying and manipulating data

Structured Query Language

Many standards out there: SQL92, SQL2, SQL3, SQL99Vendors support various subsets of these, but all of what we’llbe talking about.

Page 50: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

SQL Introduction

Basic form: (many many more bells and whistles in addition)

Select [attributes] From [relations] Where [conditions]

Select [attributes] From [relations] Where [conditions]

Page 51: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Selections

Company(sticker, name, country, stockPrice)

Find all US companies whose stock is > 50:

Output schema: R(sticker, name, country, stockPrice)

SELECT *FROM CompanyWHERE country=“USA” AND stockPrice > 50

SELECT *FROM CompanyWHERE country=“USA” AND stockPrice > 50

Page 52: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Selections

What you can use in WHERE: attribute names of the relation(s) used in the FROM. comparison operators: =, <>, <, >, <=, >= apply arithmetic operations: stockprice*2 operations on strings (e.g., “||” for concatenation). Lexicographic order on strings. Pattern matching: s LIKE p Special stuff for comparing dates and times.

Page 53: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

The LIKE operator

• s LIKE p: pattern matching on strings• p may contain two special symbols:

– % = any sequence of characters

– _ = any single character

Company(sticker, name, address, country, stockPrice)Find all US companies whose address contains “Mountain”:

SELECT *FROM CompanyWHERE country=“USA” AND address LIKE “%Mountain%”

SELECT *FROM CompanyWHERE country=“USA” AND address LIKE “%Mountain%”

Page 54: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Projections

SELECT name, stockPriceFROM CompanyWHERE country=“USA” AND stockPrice > 50

SELECT name, stockPriceFROM CompanyWHERE country=“USA” AND stockPrice > 50

Select only a subset of the attributes

Input schema: Company(sticker, name, country, stockPrice)Output schema: R(name, stock price)

Page 55: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Rename the attributes in the resulting table

Input schema: Company(sticker, name, country, stockPrice)Output schema: R(company, price)

Projections

SELECT name AS company, stockprice AS priceFROM CompanyWHERE country=“USA” AND stockPrice > 50

SELECT name AS company, stockprice AS priceFROM CompanyWHERE country=“USA” AND stockPrice > 50

Page 56: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Ordering the Results

SELECT name, stockPriceFROM CompanyWHERE country=“USA” AND stockPrice > 50ORDERBY country, name

SELECT name, stockPriceFROM CompanyWHERE country=“USA” AND stockPrice > 50ORDERBY country, name

Ordering is ascending, unless you specify the DESC keyword.

Ties are broken by the second attribute on the ORDERBY list, etc.

Page 57: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Joins

Product (pname, price, category, maker)Purchase (buyer, seller, store, product)Company (cname, stockPrice, country)Person(pname, phoneNumber, city)

Find names of people living in Seattle that bought gizmo products, and the names of the stores they bought from

SELECT pname, storeFROM Person, PurchaseWHERE pname=buyer AND city=“Seattle” AND product=“gizmo”

SELECT pname, storeFROM Person, PurchaseWHERE pname=buyer AND city=“Seattle” AND product=“gizmo”

Page 58: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Disambiguating Attributes

Product (name, price, category, maker)Purchase (buyer, seller, store, product)Person(name, phoneNumber, city)

Find names of people buying telephony products:

SELECT Person.nameFROM Person, Purchase, ProductWHERE Person.name=Purchase.buyer AND Product=Product.name AND Product.category=“telephony”

SELECT Person.nameFROM Person, Purchase, ProductWHERE Person.name=Purchase.buyer AND Product=Product.name AND Product.category=“telephony”

Page 59: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Tuple Variables

SELECT product1.maker, product2.makerFROM Product AS product1, Product AS product2WHERE product1.category=product2.category AND product1.maker <> product2.maker

SELECT product1.maker, product2.makerFROM Product AS product1, Product AS product2WHERE product1.category=product2.category AND product1.maker <> product2.maker

Find pairs of companies making products in the same category

Product ( name, price, category, maker)

Page 60: Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002

Tuple VariablesTuple variables introduced automatically by the system: Product ( name, price, category, maker)

Becomes:

Doesn’t work when Product occurs more than once:In that case the user needs to define variables explicitely.

SELECT name FROM Product WHERE price > 100

SELECT name FROM Product WHERE price > 100

SELECT Product.name FROM Product AS Product WHERE Product.price > 100

SELECT Product.name FROM Product AS Product WHERE Product.price > 100