Docsity

Relational Calculi, Functional Dependencies - Database Design | CMSC 424, Study notes of Principles of Database Management

Partial preview of the text

Download Relational Calculi, Functional Dependencies - Database Design | CMSC 424 and more Study notes Principles of Database Management in PDF only on Docsity!

Recap of Feb 13: SQL, Relational

Calculi, Functional Dependencies

  • SQL: multiple group bys , having , lots of examples
  • Tuple Calculus
  • Domain Calculus
  • Functional Dependencies
    • F+ = the closure of the set of FDs on a given relation

Relational Database Design

  • A major goal in designing a database is to have a schema

that

  • makes queries simpler (easy to phrase)
  • avoids redundancies and update anomalies ( about which more later)

Schema and Query Simplicity (2)

Example Schema 2: (a single relation)

ED( eno , ename, sal, dno, dname, floor, mgr)

Query 1: find all employees that make more than their manager

select e.ename from ED e, ED m where e.mgr=m.eno and e.sal>m.sal

Query 2: for each department, find the maximum salary

select d.dname, max(sal) from ED e group by dno

Q1 requires one join; Q2 requires just a group-by.

Schema and Query Simplicity (3)

  • How did we get simpler queries?
  • Schema 2 was a more complicated relation with more information; in essence ED was EMP and DEPT from Schema 1 with the join pre- computed
  • Should we just precompute the joins and store bigger relations?
  • Taken to the extreme, we could compute the universal relation with all attributes inside it and null values for those values that make no sense
  • Why wouldn’t we want to do that?
  • Problems with too-complex relations: repetition of information (data redundancy) and inability to represent certain information (update anomalies)

DB Design: Dealing with Anomalies

  • So complex relations make for simpler queries, but have

the disadvantages of data redundancy and creation of

anomalies. How do we balance the two objectives? We

want:

  • simple queries
  • no anomalies; minimize data redundancy
  • If we start with Schema 2 and discover anomalies we can

decompose the relation(s) until the problems go away.

This process is called normalization.

Objectives of DB Design

(Normalization)

  • no redundancy
    • for space efficiency and to reduce the potential for inconsistencies
  • update integrity
    • avoid update anomalies
  • linguistic efficiency
    • simpler queries are much better for the application programmer and for the query optimizer
  • good performance
    • smaller relations imply more joins (bad)

Lossy Decompositions

Shipment(S#, P#, J#) decomposed into SP(S#, P#) and SJ(P#, J#) s1 p1 j1 s1 p1 p1 j s2 p2 j1 s2 p2 p2 j s2 p3 j2 s2 p3 p3 j s3 p3 j3 s3 p3 p3 j s4 p4 j3 s4 p4 p4 j If we join SP and SJ again into SP-PJ(S#, P#, P#, J#) we get: s1 p1 p1 j s2 p2 p2 j s2 p3 p3 j2 from the joined tuples we cannot s2 p3 p3 j3 deduce the original form of the data. s3 p3 p3 j2 this is called the connection trap s3 p3 p3 j3 and the decomposition is lossy s4 p4 p4 j

Example of Lossy Join Decomposition

  • Lossy-join decompositions result in information loss
  • Example: decomposition of R=(A,B) into R 1 =(A) and R 2 =(B)

R= (A, B) R 1 = (A) R 2 = (B)

? 1? 1 ? 2? 2 ? 1 R 1 X R 2 = (A, B) ? 1 ? 2 ? 1 ? 2

Lossless Join Decomposition and

Functional Dependencies

  • So FDs can help determine whether a decomposition is lossless
  • R is a relation schema and F its FDs. Then a decomposition

R = R 1? R 2 is lossless if at least one of the following dependencies holds R 1? R 2? R 1 R 1? R 2? R 2

  • either of the above FDs guarantees uniqueness in the mapping (and therefore that the decomposition is lossless)

Dependency Preservation

  • Dependencies are preserved in a decomposition if we do

not need to join in order to enforce FDs -- all FDs remain

intra-relational and do not become inter-relational

  • To check if a decomposition is dependency preserving, we

need to examine all FDs in F+

  • There is an algorithm for testing dependency preservation

(requires the computation of F+)

Normalization

  • Types of FDs in R(A, B, C, D) with (A, B) a candidate

key:

  • trivial: AB ==> A
  • partial: A ==> C ( C depends upon a part of the key) TEACH(student, teacher, subject) student, subject ==> teacher (students not allowed in the same subject of two different teachers) teacher ==> subject (each teacher teaches only one subject)
  • transitive: A ==> C ==> D ED( eno , ename, sal, dno, dname, floor, mgr) eno ==> dno ==> mgr

Normalization using FDs

  • When we decompose a relation schema R with a set of

functional dependencies F into R 1 , R 2 , R 3 , … , Rn we want:

  • lossless-join decomposition: otherwise the decomposition results in loss of information relative to the original schema R
  • no redundancy: the relations Ri should be in either BCNF (Boys- Codd Normal Form) or 3NF (Third Normal Form) (about which more in a slide or two)
  • Dependency preservation: let Fi be the set of dependencies in F+ that include only attributes in Ri: - preferably the decomposition should be dependency perserving. That is, F 1? F 2? F 3? …? Fn = F+ - Otherwise checking updates for violation of FDs may require computing joins, which is expensive

Distinguishing Examples

  • 1NF but not 2NF: SUPPLY( sno, pno, jno , scity, jcity, qty)
    • (sno, pno, jno) is the candidate key
    • sno ==> scity, jno ==> jcity are both partial dependencies
  • 2NF but not 3NF: ED( eno , ename, sal, dno, dname, floor, mgr)
    • transitive FD: eno ==> dno ==> dname
  • 3NF but not BCNF: TEACH(student, teacher, subject)
    • student, subject ==> teacher
    • teacher ==> subject

Boyce-Codd Normal Form

BCNF is perhaps the most useful Normal Form for database

design

A relation schema R is in BCNF with respect to a set F of

functional dependancies if for all functional dependancies

in F+ of the form X==> Y where X? R, Y? R; at least one

of the following holds:

  • X ==>Y is trivial (that is, Y? X)
  • X is a superkey for R