Final Exam Revision Instructor: Mohamed Eltabakh 1.

Slides:



Advertisements
Similar presentations
Schema Refinement: Normal Forms
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Relational Database Design Algorithms and Further Dependencies.
Normalization 1 Instructor: Mohamed Eltabakh Part II.
Database Systems Lecture 12 Natasha Alechina
Logical Database Design (3 of 3) John Ortiz. Lecture 7Logical Database Design (2)2 Normalization  If a relation is not in BCNF or 3NF, we refine it by.
CS 440 Database Management Systems Practice problems for normalization.
Review for Final Exam Lecture Week 14. Problems on Functional Dependencies and Normal Forms.
1 Design Theory. 2 Minimal Sets of Dependancies A set of dependencies is minimal if: 1.Every right side is a single attribute 2.For no X  A in F and.
Murali Mani Normalization. Murali Mani What and Why Normalization? To remove potential redundancy in design Redundancy causes several anomalies: insert,
CS Algorithm : Decomposition into 3NF  Obviously, the algorithm for lossless join decomp into BCNF can be used to obtain a lossless join decomp.
Classroom Exercise: Normalization
Normalization DB Tuning CS186 Final Review Session.
Normalization DB Tuning CS186 Final Review Session.
CMSC424: Database Design Instructor: Amol Deshpande
Nov 11, 2003Murali Mani Normalization B term 2004: lecture 7, 8, 9.
Normal Form Design addendum by C. Zaniolo. ©Silberschatz, Korth and Sudarshan7.2Database System Concepts Normal Form Design Compute the canonical cover.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 9.
Decomposition By Yuhung Chen CS157A Section 2 October
Schema Refinement and Normalization Nobody realizes that some people expend tremendous energy merely to be normal. Albert Camus.
Cs3431 Normalization Part II. cs3431 Attribute Closure : Example Consider R (A, B, C, D, E) with FDs A  B, B  C, CD  E Does A  E hold ? (Is A  E.
Chapter 14 Advanced Normalization Transparencies © Pearson Education Limited 1995, 2005.
CS 405G: Introduction to Database Systems 16. Functional Dependency.
Functional Dependencies and Normalization 1 Instructor: Mohamed Eltabakh
Final-Exam Revision Instructor: Mohamed Eltabakh 1.
Chapter 8: Relational Database Design First Normal Form First Normal Form Functional Dependencies Functional Dependencies Decomposition Decomposition Boyce-Codd.
Lecture 6 Normalization: Advanced forms. Objectives How inference rules can identify a set of all functional dependencies for a relation. How Inference.
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.
Functional Dependencies An example: loan-info= Observe: tuples with the same value for lno will always have the same value for amt We write: lno  amt.
SCUJ. Holliday - coen 1784–1 Schedule Today: u Normal Forms. u Section 3.6. Next u Relational Algebra. Read chapter 5 to page 199 After that u SQL Queries.
THIRD NORMAL FORM (3NF) A relation R is in BCNF if whenever a FD XA holds in R, one of the following statements is true: XA is a trivial FD, or X is.
BCNF & Lossless Decomposition Prof. Sin-Min Lee Department of Computer Science.
Functional Dependencies and Normalization 1 Instructor: Mohamed Eltabakh
CS 564 Database Management Systems: Design and Implementation Discussion Session Friday, Sept 18, Apul Jain.
Revision Instructor: Mohamed Eltabakh 1.
Revisit FDs & BCNF Normalization 1 Instructor: Mohamed Eltabakh
Announcements Midterm is Nov. 22 (8:15am – 9:45am) Covers until the end of Normalization You answer in the same exam paper Closed-Book, One sheet is allowed.
Minimum Cover of F. Minimal Cover for a Set of FDs Minimal cover G for a set of FDs F: –Closure of F = closure of G. –Right hand side of each FD in G.
Copyright, Harris Corporation & Ophir Frieder, The Process of Normalization.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 338Database Design and Normal Forms9-1 Database Design and Normal Forms Lecture Topics Measuring the quality of a schema Schema design with normalization.
Ch 7: Normalization-Part 1
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
© D. Wong Functional Dependencies (FD)  Given: relation schema R(A1, …, An), and X and Y be subsets of (A1, … An). FD : X  Y means X functionally.
Chapter 8 Relational Database Design. 2 Relational Database Design: Goals n Reduce data redundancy (undesirable replication of data values) n Minimize.
1 CS 430 Database Theory Winter 2005 Lecture 8: Functional Dependencies Second, Third, and Boyce-Codd Normal Forms.
Copyright © Curt Hill Schema Refinement II 2 nd NF to 3 rd NF to BCNF.
Normalization and FUNctional Dependencies. Redundancy: root of several problems with relational schemas: –redundant storage, insert/delete/update anomalies.
Chapter 14 Functional Dependencies and Normalization Informal Design Guidelines for Relational Databases –Semantics of the Relation Attributes –Redundant.
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
Functional Dependencies and Normalization 1 Instructor: Mohamed Eltabakh Part 2.
Advanced Normalization
Schedule Today: Next After that Normal Forms. Section 3.6.
Relational Database Design (Discussion Session)
CS411 Database Systems 08: Midterm Review Kazuhiro Minami 1.
Normalization First Normal Form (1NF) Boyce-Codd Normal Form (BCNF)
CS 480: Database Systems Lecture 22 March 6, 2013.
Advanced Normalization
CS405G: Introduction to Database Systems
Functional Dependencies and Normalization
Functional Dependencies and Normalization
Review  Only two chapters (6 & 7) Normalization Theory Triggers.
Functional Dependencies and Normalization
Functional Dependencies and Normalization
Instructor: Mohamed Eltabakh
Designing Relational Databases
Instructor: Mohamed Eltabakh
Functional Dependencies and Normalization
Functional Dependencies and Normalization
Instructor: Mohamed Eltabakh
CS4222 Principles of Database System
Presentation transcript:

Final Exam Revision Instructor: Mohamed Eltabakh 1

What You Learned Data Models Entity-Relationship Model & ERD Relational Model Conversion between the data models Relational Algebra & Operators Structured Query Language SQL DML: Data Manipulation Language DDL: Data Definition Language 2

What You Learned (Cont’d) Advanced SQL Triggers, Views, Cursors, Stored Procedures and Functions PL/SQL Functional Dependencies Normalization Rules 3

In Advanced Courses Things get more interesting Indexing Techniques Transaction Management Query Optimization Handling of Big Data And many more … 4

Final Exam Details Time and date Final exam will start on Feb 28th 11:00am The exam will be 80 mins Included material SQL commands (Select, update, delete, insert) Advanced SQL (Views, Triggers, Stored functions, Cursors) Functional Dependencies Normalization Closed book and closed notes Allowed only ONE page (front and back) to write anything you want. You answer in the same sheet 5

Functional Dependencies & Normalization 6

Key Points Identifying the candidate keys of relations Use of Transitive, and Union properties to generate more FDs Computing the attribute closure A is a key if all attributes are in its closure A candidate key is also a super key (But it is minimal) Remember the rules for BCNF and 3NF Test which FD is violating the rules Decompose based on this rule 7

Question 1 Given relation R= (A, B, C, D, E) with the following FDs: F = {AB  C, C  D, B  D, CD  E, AB  E} Compute the canonical (minimal) cover of F = G 8 Use Union property: AB  CE, C  D, B  D, CD  E Take the shortest L.H.S first {C  D, B  D, …} D in CD  E is not needed We have {C  DE, B  D, AB  CE} AB determines C, and C determines E…So no need for AB  E Canonical cover G = {C  DE, B  D, AB  C}

Question 2 Given relation R= (A, B, C, D, E) with the following FDs: F = {AB  C, C  D, B  D, CD  E, AB  E} What are the candidate keys of R? 9 Is there any FD that determines A  No (then A must be part of the candidate key) Is there any FD that determines B  No (then B must be part of the candidate key) Lets check {AB} + = {AB …} {ABCD…} {ABCDE}  Then AB is the only candidate key

Question 3 Given relation R = (A, B, C, D) with the following FDs: F = {B  C, B  D} What are the candidate keys of R? Report FDs violating BCNF (if any)? Decompose R to be in BCNF (if not already)? 10 {AB} + = {ABCD} AB is the only candidate key B  C B  D

Question 3 (Cont’d) Given relation R = (A, B, C, D) with the following FDs: F = {B  C, B  D} What are the candidate keys of R? Report FDs violating 3NF (if any)? Decompose R to be in 3NF (if not already)? 11 AB B  C B  D

Question 4 Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} Is ABF candidate key for R? Is it a superkey (Yes or No)? 12 Compute {ABF}+ = {ABF…} {ABDEF…} {ABCDEF}  This means ABF is a superkey Is ABF minimal key? {AF} + = {AF}  That is not a key {BF} + = {BEF}  That is not a key {AB} + = {ABCDEF}  That is a key (minimal one) So ABF is not a candidate key

Question 5 Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} If R is decomposed into two relations R1(A, B, D, F) and R2(C, D, E, F) Is this decomposition dependency preserving or not? 13 Step 1: Find the local dependencies to R1 and R2 Step 2: Check each of the original FDs on R, whether you can get it from the local FDs + the global derived ones

Question 5 (Cont’d) Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} If R is decomposed into two relations R1(A, B, D, F) and R2(C, D, E, F) Is this decomposition dependency preserving or not? 14 Local to R1: {AB  D, B  F, DF  A, BD  A F } Local to R2: {CD  E, DF  C}

Question 5 (Cont’d) Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} If R is decomposed into two relations R1(A, B, D, F) and R2(C, D, E, F) Is this decomposition dependency preserving or not? 15 Local to R1: {AB  D, B  F, DF  A, BD  A F } Local to R2: {CD  E, DF  C} Check original ones: AB  DE – Preserved using (AB  D) from R1 and (AB  E) global one CD  E -- Preserved from (CD  E) in R2 B  F -- Preserved from (B  F) in R1 B  E -- Lost DF  AC -- Preserved from (DF  A) from R1, and (DF  C) from R2 BD  AF -- Preserved from (BD  AF) from R1 Decomposition is NOT dependency preserving

Question 6 Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} If R is decomposed into R1 = (A, B, D, F) and R2 = (C, D, E, F) Is this decomposition lossless or lossy? Indicate why? 16 Step 1: Find the common attributes between R1 and R2 (these are the ones used in natural join) Step 2: Check if the common attributes are candidate keys in either of R1 or R2 If yes  Lossless If no  lossy

Question 6 (Cont’d) Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} If R is decomposed into R1 = (A, B, D, F) and R2 = (C, D, E, F) Is this decomposition lossless or lossy? Indicate why? 17 Common attributes are {DF} In R1: {DF} + = {DFA}  So DF is not a key in R1 In R2: {DF} + = {DFCE}  So DF is a key in R2 Decomposition is lossless

Question 7 Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} Which of the five given FDs violates the BCNF (if any)? 18 Step 1: Before you check a normal form, you need to find the keys Step 2: Check each dependency against the normal form rules

Question 7 (Cont’d) Given R = (A, B, C, D, E, F), FDs: F = {AB  DE, CD  E, B  EF, DF  AC, BD  AF} Which of the five given FDs violates the BCNF (if any)? 19 Candidate keys of R: {AB} {BD} Violation to BCNF: CD  E B  EF DF  AC Using (CD  E) to divide R R1 = (C, D, E), R2 = (A, B, C, D, F) R2 is still not in BCNF and violated by: B  F and DF  AC Using (B  F) to divide R2 R1 = (C, D, E), R3 = (B, F), R4 = (A, B, C, D) Now R1, R3, R4 are in BCNF

SQL Commands 20

21 Question 1

22 Question 2 Delete prescription lines for prescriptions written on date ‘Jan ’ Delete From Prescription_Medicine Where prescription_id in ( Select id From Prescription Where date = ‘Jan ’);

23 Question 3 Delete prescriptions that have no lines (no records in prescription_medicine) Delete From Prescription Where id not in ( Select prescription_id From prescription_medicine);

24 Question 4 Select patients who have no primary doctors Select * From Patient Where primaryDoctor_SSN is null;

25 Question 5 Report the prescription id and its total cost of prescriptions having total cost between $100 and $200. Sort ascending based on the total cost Select prescription_id, sum(unitPrice * NumOfUnits) As totalCost From Medicine M, Prescription_Medicine PM Where M.TradeName = PM.TradeName Group By prescription_id Having totalCost > 100 And totalCost < 200 Order By totalCost;

Advanced SQL Commands 26

27 Question 1 Create a view that reports the trade name, unit price, and the generic flag of the most expensive and cheapest medicines.

28 Question 2 Create a stored function that takes a date as a parameter and returns the number of prescriptions on that date Create Function NumPrescriptions (inDate IN date) Return int As temp int; Begin Select count(*) into temp From prescription where date = inDate; return temp; End;

29 Question 3 Using the function created in Question 15, report the prescriptions written on a date in which more than 10 prescriptions have been written Select * From prescription Where NumPrescriotions(date) > 10;

30 Question 4 Create trigger that ensures that if the medicine is generic, then its unit price is below $100, and if it is not generic then its unit price >= $100 Create Trigger UnitPrice Before Insert Or Update On Medicine For Each Row Begin IF (:new.GenericFlag = ‘T’ and :new.UnitPrice >= 100) Then RAISE_APPLICATION_ERROR(-20004, ‘Price should be < $100’); ELSIF (:new.GenericFlag = ‘F’ and :new.UnitPrice < 100) Then RAISE_APPLICATION_ERROR(-20004, ‘Price should be >= $100’); END IF; End;

End of Revision 31