/
6.830 Lecture 3 Relational Algebra and Normalization 6.830 Lecture 3 Relational Algebra and Normalization

6.830 Lecture 3 Relational Algebra and Normalization - PowerPoint Presentation

alida-meadow
alida-meadow . @alida-meadow
Follow
342 views
Uploaded On 2019-11-08

6.830 Lecture 3 Relational Algebra and Normalization - PPT Presentation

6830 Lecture 3 Relational Algebra and Normalization 9132017 Relational Algebra Projection πR c1 cn π c1c2n R select a subset c1 cn of columns of R Selection σ R ID: 764778

feedtime int animals select int feedtime select animals age keptby office schema cageno table time string client pred account

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "6.830 Lecture 3 Relational Algebra and N..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

6.830 Lecture 3 Relational Algebra and Normalization 9/13/2017

Relational Algebra Projection π(R, c1, …, cn ) = π c1…c2n R select a subset c1 … cn of columns of R Selection σ ( R , pred ) = σ pred R select a subset of rows that satisfy pred Cross Product (||R|| = # attrs in R, |R| = #rows in row) R1 X R2 (aka C artesian product) combine R1 and R2, producing a new relation with ||R1|| + ||R2|| attrs , |R1| * |R2| rows Join ⨝ ( R 1 , R2 , pred ) = R1 ⨝ pred R 2 = σ pred ( R 1 X R2)

Relational Algebra  SQL SELECT List  Projection FROM List  all tables referenced WHERE  SELECT and JOIN Many equivalent relational algebra expressions to any one SQL query (due to relational identities) Join reordering Select reordering Select pushdown

Multiple Feedtimes f eedtimes (time, animal)

Multiple Feedtimes in SQL a nimals:( name STRING , cageno INT,keptby INT,age INT,feedtime TIME)CREATE TABLE feedtimes(aname STRING, feedtime TIME); ALTER TABLE animals RENAME TO animals2; ALTER TABLE animals2 DROP COLUMN feedtime; CREATE VIEW animals ASSELECT name, cageno, keptby, age, (SELECT feedtime FROM feedtimes WHERE aname=name LIMIT 1) AS feedtimeFROM animals Views enable logical data independence by emulating old schema in new schema

Study Break Given animals table: animals:( name STRING , cageno INT,keptby INT,age INT,feedtime TIME)Find a view rewrite that will allow the following schema changes (while maintaining backwards compatibility)?Key of table is animalId instead of nameAnimals can be in multiple cagesAge  Birthday

Study Break Key of table is animalId instead of name newAnimals :( animalId int, name STRING, cageno INT,keptby INT,age INT,feedtime TIME)CREATE VIEW animals AS (SELECT name, cageno, keptby , age, feedtime FROM newAnimals ) Animals can be in multiple cages newAnimals :( name STRING , keptby INT, age INT,feedtime TIME ) animalCages :( aName STRING , cageId INT ) CREATE VIEW animals AS (SELECT name, (SELECT cageId FROM animalCages WHERE aName = name LIMIT 1) AS cageno , keptby , age, feedtime FROM newAnimals )

Study Break Age  Birthday newAnimals :(name STRING, cageno INT,keptby INT, bday DATE,feedtime TIME)CREATE VIEW animals AS (SELECT name, cageno, keptby, ((now() – bday)/(365 * 24 * 60 * 60))::INT AS age, feedtime )

Hobby Schema SSN Name Address Hobby Cost 123 john main st dolls $ 123 john main st bugs $ 345 mary lake st tennis $$ 456joefirst stdolls$ “Wide” schema – has redundancy and anomalies in the presence of updates, inserts, and deletes Table key is Hobby, SSN Person Hobby SSN Address Name Name Cost n:n Entity Relationship Diagram

Boyce Codd Normal Form (BCNF) A set of relations is in BCNF if: For every functional dependency X  Y,in a set of functional dependencies F over a relation R,X is a superkey key of R, (where superkey means that X contains a key of R )

BCNFify Algorithm While some relation R is not in BCNF: Find an FD F=X  Y that violates BCNF on R Split R into: R1 = (X U Y) R2 = R – Y

BCNFify Example for Hobbies Schema FDs ( S,H ,N,A,C) S,H  N,A,C S  N, A H  C S = SSN, H = Hobby, N = Name, A = Addr, C = Cost violates bcnf Schema FDs ( S , N,A)S  N, ASchemaFDs(S,H, C)S,H  CH  Cviolates bcnfSchemaFDs(H, C) H  C SchemaFDs ( S,H ) Iter 1 Iter 2 key Iter 3

Accounts, Client, Office FD’s Client, Office  Account Account  Office Account Client Office a joe 1 b mary1ajohn1cjoe2

Accounts, Client, Office FD’s Client, Office  Account Account  Office Account Client Office a joe 1 b mary1ajohn1cjoe2Redundancy!

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?