Relational Algebra and Normalization 9/13/2017

Slides:



Advertisements
Similar presentations
Introduction to Databases
Advertisements

6.830/6.814 Lecture 3 Sam Madden Relational Algebra and Normalization Sept 10, 2014.
Relational Normalization Theory. Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does not provide.
Hobby Schema SSNNameAddressHobbyCost 123johnmain stdolls$ 123johnmain stbugs$ 345marylake sttennis$$ 456joefirst stdolls$ “Wide” schema – has redundancy.
EECS 339 Lecture 2 Schema Design, Relational Algebra Jennie Duggan January 13, 2014.
SQL = Sequel = Structured Query Language The Standard Language for Relational Databases.
Database Design Conceptual –identify important entities and relationships –determine attribute domains and candidate keys –draw the E-R diagram Logical.
Functional Dependencies Definition: If two tuples agree on the attributes A, A, … A 12n then they must also agree on the attributes B, B, … B 12m Formally:
EECS 339 Lecture 3 Normalization Database Internals.
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
Functional Dependencies and Relational Schema Design.
Lecture 11 Main Memory Databases Midterm Review. Time breakdown for Shore DBMS Source: “OLTP Under the Looking Glass”, SIGMOD 2008 Systematically removed.
Hobby Schema SSNNameAddressHobbyCost 123johnmain stdolls$ 123johnmain stbugs$ 345marylake sttennis$$ 456joefirst stdolls$ “Wide” schema – has redundancy.
11/07/2003Akbar Mokhtarani (LBNL)1 Normalization of Relational Tables Akbar Mokhtarani LBNL (HENPC group) November 7, 2003.
1 Lecture 7: Normal Forms, Relational Algebra Monday, 10/15/2001.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
CS 157B Database Systems Dr. T Y Lin. Updates 1.Red color denotes updated data (ppt) 2.Class participation will be part of “extra” credits to to “quiz.
CS411 Database Systems Kazuhiro Minami 04: Relational Schema Design.
FEN Introduction to the database field: The development process Seminar: Introduction to relational databases Development process: Analyse.
Database Architecture Normalization. Purpose of Normalization A technique for producing a set of relations with desirable properties, given the data requirements.
Formal definition of a key A key is a set of attributes A 1,..., A n such that for any other attribute B: A 1,..., A n  B A minimal key is a set of attributes.
1 Normalization Theory. 2 Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does not provide a way.
Fundamental of Database Systems
Lecture 11: Functional Dependencies
COP 6726: New Directions in Database Systems
Database Systems Chapter 6
The Entity-Relationship Model
Introduction to the database systems (1)
Schedule Today: Next After that Normal Forms. Section 3.6.
A brief summary of database normalization
Day 4 - Simple Queries & More on Tables
CS411 Database Systems 08: Midterm Review Kazuhiro Minami 1.
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
CPSC-310 Database Systems
Lecture 4 Database design IV Normal Forms: Summary.
Chapter 4 Relational Databases
Payroll Management System
Database Normalization
Translation of ER-diagram into Relational Schema
CMPE 226 Database Systems February 21 Class Meeting
Problems in Designing Schema
The Relational Model and Normalization
Relational Design Theory
CS405G: Introduction to Database Systems
Relational Design Theory
Database Normalization
Chapter 6 Normalization of Database Tables
Normalization Murali Mani.
Lecture 2: Database Modeling (end) The Relational Data Model
Cse 344 May 16th – Normalization.
Functional Dependencies and Relational Schema Design
Introduction to Database Systems CSE 444 Lectures 8 & 9 Database Design October 12 & 15, 2007.
Lecture 09: Functional Dependencies, Database Design
Normalization.
Database Design Agenda
Lecture 8: Database Design
Lecture 07: E/R Diagrams and Functional Dependencies
“Those who cannot remember the past are doomed to repeat it”
CS 405G: Introduction to Database Systems
Normalization February 28, 2019 DB:Normalization.
Functional Dependencies
Chapter 19 (part 1) Functional Dependencies
Relational Database Design
Introduction to Database Design
Terminology Product Attribute names Name Price Category Manufacturer
Lecture 6: Functional Dependencies
Lecture 11: Functional Dependencies
Database.
Lecture 09: Functional Dependencies
Presentation transcript:

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

Relational Algebra Projection π(R,c1, …, cn) = πc1…c2nR select a subset c1 … cn of columns of R Selection σ(R, pred) = σpredR select a subset of rows that satisfy pred Cross Product (||R|| = #attrs in R, |R| = #rows in row) R1 X R2 (aka Cartesian product) combine R1 and R2, producing a new relation with ||R1|| + ||R2|| attrs, |R1| * |R2| rows Join ⨝(R1, R2, pred) = R1 ⨝pred R2 = σpred (R1 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 feedtimes(time, animal)

Multiple Feedtimes in SQL animals:(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 AS SELECT name, cageno, keptby, age, (SELECT feedtime FROM feedtimes WHERE aname=name LIMIT 1) AS feedtime FROM 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 name Animals can be in multiple cages Age  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 $ bugs 345 mary lake st tennis $$ 456 joe first st Table key is Hobby, SSN “Wide” schema – has redundancy and anomalies in the presence of updates, inserts, and deletes Entity Relationship Diagram SSN Address Name Name Cost Person n:n Hobby

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 Iter 1 S = SSN, H = Hobby, N = Name, A = Addr, C = Cost Iter 2 Schema FDs (S,H,N,A,C) S,H  N,A,C S  N, A H  C Schema FDs (S, N,A) S  N, A violates bcnf Schema FDs (S,H, C) S,H  C H  C key violates bcnf Iter 3 Schema FDs (H, C) H  C Schema FDs (S,H)

Accounts, Client, Office FD’s Client, Office  Account Account  Office Account Client Office a joe 1 b mary john c 2

Accounts, Client, Office FD’s Client, Office  Account Account  Office Account Client Office a joe 1 b mary john c 2 Redundancy!

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?