6.830 lecture 3

6
6.830 Lecture 3 Relational Algebra and Normalization Feb 13

Upload: mitchell-jeremiah

Post on 30-Dec-2015

25 views

Category:

Documents


0 download

DESCRIPTION

6.830 Lecture 3. Relational Algebra and Normalization Feb 13. Study Break # 1. Schema: classes: ( cid , c_name, c_rid , …) rooms: ( rid , bldg, …) students: ( sid , s_name, …) takes: ( t_sid , t_cid ). SELECT s_name FROM student,takes,classes WHERE t_sid=sid AND t_cid=cid - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 6.830 Lecture 3

6.830 Lecture 3

Relational Algebra and NormalizationFeb 13

Page 2: 6.830 Lecture 3

Study Break # 1Schema:

classes: (cid, c_name, c_rid, …)rooms: (rid, bldg, …)students: (sid, s_name, …)takes: (t_sid, t_cid)

SELECT s_name FROM student,takes,classesWHERE t_sid=sid AND t_cid=cid AND c_name=‘6.830’

Page 3: 6.830 Lecture 3

Questions

• Write an equivalent relational algebra expression for this query

• Are there other possible expressions?• Do you think one would be more “efficient” to

execute? Why?

SELECT s_name FROM student,takes,classesWHERE t_sid=sid AND t_cid=cid AND c_name=‘6.830’

Page 4: 6.830 Lecture 3

Hobby SchemaSSN Name Address Hobby Cost

123 john main st dolls $

123 john main st bugs $

345 mary lake st tennis $$

456 joe first st dolls $

“Wide” schema – has redundancy and anomalies in the presence of updates, inserts, and deletes

Table key is Hobby, SSN

PersonPerson HobbyHobby

SSN

Address

Name

Name

Cost

n:n

Entity Relationship Diagram

Page 5: 6.830 Lecture 3

BCNFify Example for Hobbies

Schema FDs

(S,H,N,A,C) S,H N,A,CS N, AH C

S = SSN, H = Hobby, N = Name, A = Addr, C = Cost

violates bcnf

Schema FDs

(S, N,A) S N, A

Schema FDs

(S,H, C) S,H CH C

violates bcnf

Schema FDs

(H, C) H C

Schema FDs

(S,H)

Iter 1

Iter 2

key

Iter 3

Page 6: 6.830 Lecture 3

Study Break # 2• Patient database• Want to represent patients at hospitals with doctors• Patients have names, birthdates• Doctors have names, specialties• Hospitals have names, addresses• One doctor can treat multiple patients, each patient has one doctor• Each patient in one hospital, hospitals have many patients

1) Draw an ER diagram

2) What are the functional dependencies

3) What is the normalized schema? Is it redundancy free?