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.

Slides:



Advertisements
Similar presentations
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Relational Database Design Algorithms and Further Dependencies.
Lecture 07: Relational Algebra
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
Lecture 6: Design Constraints and Functional Dependencies January 21st, 2004.
Relational Algebra Maybe -- SQL. Confused by Normal Forms ? 3NF BCNF 4NF If a database doesn’t violate 4NF (BCNF) then it doesn’t violate BCNF (3NF) !
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
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:
Lecture #3 Functional Dependencies Normalization Relational Algebra Thursday, October 12, 2000.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Boyce-Codd NF & Lossless Decomposition Professor Sin-Min Lee.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #6 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Complex Queries (1) Product ( pname, price, category, maker)
Relational Schema Design (end) Relational Algebra Finally, querying the database!
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Functional Dependencies and Relational Schema Design.
Integrity Constraints An important functionality of a DBMS is to enable the specification of integrity constraints and to enforce them. Knowledge of integrity.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1 Lecture 7: End of Normal Forms Outerjoins, Schema Creation and Views Wednesday, January 28th, 2004.
Chapter 10 Functional Dependencies and Normalization for Relational Databases.
Database Systems Normal Forms. Decomposition Suppose we have a relation R[U] with a schema U={A 1,…,A n } – A decomposition of U is a set of schemas.
CS143 Review: Normalization Theory Q: Is it a good table design? We can start with an ER diagram or with a large relation that contain a sample of the.
Lecture 09: Functional Dependencies. Outline Functional dependencies (3.4) Rules about FDs (3.5) Design of a Relational schema (3.6)
Today Collection of unrelated stuff Questions? HW –4 -5 & drop lowest –emphasize project Projects Functional Dependencies? –Someone asked why: Keys, Normal.
1 Introduction to Database Systems CSE 444 Lecture 20: Query Execution: Relational Algebra May 21, 2008.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Functional Dependencies. Outline Functional dependencies (3.4) Rules about FDs (3.5) Design of a Relational schema (3.6)
1 Lecture 7: Normal Forms, Relational Algebra Monday, 10/15/2001.
Relational Algebra 2. Relational Algebra Formalism for creating new relations from existing ones Its place in the big picture: Declartive query language.
Advanced Relational Algebra & SQL (Part1 )
Functional Dependencies and Relational Schema Design.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
Lecture 13: Relational Decomposition and Relational Algebra February 5 th, 2003.
1 Lecture 10: Database Design and Relational Algebra Monday, October 20, 2003.
CS411 Database Systems Kazuhiro Minami 04: Relational Schema Design.
Final Review Zaki Malik November 20, Basic Operators Covered.
1 Lecture 9: Database Design Wednesday, January 25, 2006.
CSE 326: Data Structures Lecture #22 Databases and Sorting Alon Halevy Spring Quarter 2001.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
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.
Lecture 11: Functional Dependencies
Relational Algebra.
COMP3017 Advanced Databases
Module 2: Intro to Relational Model
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Chapter 2: Intro to Relational Model
Problems in Designing Schema
Database Systems 10/13/2010 Lecture #4.
Lecture 10: Relational Algebra (continued), Datalog
LECTURE 3: Relational Algebra
Functional Dependencies and Relational Schema Design
Lecture 9: Relational Algebra (continued), Datalog
Lecture 8: Database Design
Where are we? Until now: Modeling databases (ODL, E/R): all about the schema Now: Manipulating the data: queries, updates, SQL Then: looking inside -
Chapter 2: Intro to Relational Model
Integrity Constraints
Chapter 2: Intro to Relational Model
Functional Dependencies
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Terminology Product Attribute names Name Price Category Manufacturer
Lecture 3: Relational Algebra and SQL
Relational Schema Design (end) Relational Algebra SQL (maybe)
Syllabus Introduction Website Management Systems
Lecture 6: Functional Dependencies
Lecture 11: Functional Dependencies
Lecture 09: Functional Dependencies
Presentation transcript:

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 that: B, B, … B 12m C, C, … C 12l  A, A, … A 12n Can we always decompose a relation with no dire consequences? When we see a BCNF violation, how do we decompose?

Wild Decompositions Name Address Move-Date Name Address Name Move-Date What’s wrong?

Not This Example, Again... SSN Name Phone Number Fred (201) Fred (206) Joe (908) Joe (212)

More Careful Strategy Given a dependency that violates the BCNF condition: A, A, … A 12n B, B, … B 12m A’s Others B’s R1R2 Decompose the relation as follows: Continue until there are no BCNF violations left. Find a 2-attribute relation that is not in BCNF.

Example Decomposition Name Social-security-number Age Eye Color Phone Number Functional dependencies: Name + Social-security-number Age, Eye Color What if we also had an attribute Draft-worthy, and the FD: Age Draft-worthy

A Problem Unit Company Product Unit Company Unit Product FD’s: unit -> company; company, product -> unit So, there is a BCNF violation, and we decompose.

So What’s the Problem? Unit Company Product Unit CompanyUnit Product Galaga99 UW Galaga99 databases NullValue UW NullValue databases No problem so far. All local FD’s are satisfied. Let’s put all the data back into a single table again: Galaga99 UW databases NullValue UW databases Violates the dependency: company, product -> unit!

Solution: 3rd Normal Form A simple condition for removing anomalies from relations: A relation R is in 3rd normal form if and only if: Whenever there is a nontrivial dependency for R, it is the case that { } a super-key for R, or B is part of a key. A, A, … A 12n B 12n What happened to first and second normal forms? Will we have more normal forms?

Multi-valued Dependencies Name SSN Phone Number Course Fred (206) CSE-444 Fred (206) CSE-341 Fred (206) CSE-444 Fred (206) CSE-341 The multi-valued dependencies are: Name, SSN Phone Number Name, SSN Course 4th Normal form: replace FD by MVD.

Summary of This Section Functional dependencies: basic definition. Properties of functional dependencies. Entailment and equivalence of FD’s: –Compare what FD’s say about sets of databases. Algorithm for finding the closure of a set of attributes A: –Start with A, and add attributes implied by the FDs.

Summary Continued Algorithm for entailment: Does the set of FDs S entail the dependency A -> B? –Compute the closure of A. If it includes the attribute B, return Yes. Otherwise, return No. Algorithm for entailment of a set of FDs: does S entail S1? –Apply the previous algorithm for each FD in S1. Return Yes only if it’s Yes for all. Algorithm for equivalence: apply entailment in both directions.

More Summary Decompose a relation by identifying BCNF violations. You can weaken BCNF by considering 3rd normal form. If multi-valued dependencies are present, consider 4th normal form. Given a relational schema and a set of dependencies, we now know how to put it into normalized form. Do we always want to do that??

Summary Beyond This Section We built a conceptual model (in E/R or ODL): –We chose entities, relationships –We articulated the constraints on the domain. We translated it into a relational schema in the proper way. What do we do with the data now that it’s in tables? –How do we query it? –How do we combine data from multiple tables?

Querying the Database How do we specify what we want from our database? Find all the employees who earn more than $50,000 and pay taxes in New Jersey. We design high-level query languages: –SQL (used everywhere) –Datalog (used by database theoreticians, their students, friends and family) Relational algebra: a basic set of operations on relations that provide the basic principles.

Relational Algebra at a Glance Operators: sets as input, new set as output Basic Set Operators –union, intersection, difference, but no complement. Selection:  Projection:  Cartesian Product: X Joins (natural,equi-join, theta join, semi- join) Renaming: 

Set Operations Binary operations –Result is table(set) with same attributes Watch out for naming of attributes in resulting relation. Union: all tuples in R1 or R2 Intersection: all tuples in R1 and R2 Difference: all tuples in R1 and not in R2 No complement. Why? Bags later.

Selection Produce a subset of the tuples in a relation which satisfy a given condition Unary operation… returns set with same attributes, but ‘selects’ rows Use and, or, not, >, <… to build condition Find all employees with salary more than $40,000:

Find all employees with salary more than $40,000.

Projection Unary operation, selects columns Eliminates duplicate tuples Example: project social-security number and names.

Cartesian Product Binary Operation Result is tuples combining any element of R1 with any element of R2, for R1 X R2 Schema is union of Schema(R1) & Schema(R2)

Join (Natural) Most important, expensive and exciting. Combines two relations, selecting only related tuples Equivalent to a cross product followed by selection Resulting schema has all attributes of the two relations, but one copy of join condition attributes

Other Joins and Renaming Theta join: the join involves a predicate –R S Semi-join: the attributes of one relation are included in the other. Renaming:

Complex Queries Product ( name, price, category, maker) Purchase (buyer, seller, store, product) Company (name, stock price, country) Person( name, phone number, city) Find phone numbers of people who bought gizmos from Fred. Find telephony products that somebody bought

Exercises Product ( name, price, category, maker) Purchase (buyer, seller, store, product) Company (name, stock price, country) Person( name, phone number, city) Ex #1: Find people who bought telephony products. Ex #2: Find names of people who bought American products Ex #3: Find names of people who bought American products and did not buy French products Ex #4: Find names of people who bought American products and they live in Seattle. Ex #5: Find people who bought stuff from Joe or bought products from a company whose stock prices is more than $50.

Operations on Bags (and why we care) Basic operations: Projection Selection Union Intersection Set difference Cartesian product Join (natural join, theta join)