Download presentation

Presentation is loading. Please wait.

Published byBethany Gibbs Modified over 3 years ago

1
CSCI 4333 Database Design and Implementation Review for Midterm Exam II Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu 1

2
Review Chapters 5 ~ 7 in your textbook Lecture slides In-class exercises Assignments Projects 2

3
Review (cont'd) Question Types – Q/A Relational algebra SQL Normalization theory – 3 axioms, FD closure, attribute closure, BCNF, 3NF, minimal cover, lossless decomposition, dependency preserving 5 Questions (100 points) + 1 Bonus Question (20 extra points) 3

4
Chapter 5 Relational Algebra and SQL Relational algebra – Select, project, set operators, union, cartesian product, (natural) join, division SQL – SQL for operators above – Aggregates – Group by … Having – Order by 4

5
5 Select Operator Produce table containing subset of rows of argument table satisfying condition condition (relation) Example: Person Person Person Hobby=‘stamps’ ( Person ) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps Id Name Address Hobby

6
6 Project Operator Produces table containing subset of columns of argument table attribute list (relation) Example: PersonPerson Person Name,Hobby (Person) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps John stamps John coins Mary hiking Bart stamps Id Name Address Hobby Name Hobby

7
7 Set Operators Relation is a set of tuples, so set operations should apply: , , (set difference) Result of combining two relations with a set operator is a relation => all its elements must be tuples having same structure union compatible relations Hence, scope of set operations limited to union compatible relations

8
8 Union Compatible Relations union compatible Two relations are union compatible if – Both have same number of columns – Names of attributes are the same in both – Attributes with the same name in both relations have the same domain unionintersectionsetdifference Union compatible relations can be combined using union, intersection, and set difference

9
9 Cartesian Product RSRS R S If R and S are two relations, R S is the set of all concatenated tuples, where x is a tuple in R and y is a tuple in S – RS – R and S need not be union compatible RS R S is expensive to compute: – Factor of two in the size of each row – Quadratic in the number of rows A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 RS R S x3 x4 y3 y4 RS R S

10
10 Derived Operation: Join generalthetajoin A (general or theta) join of R and S is the expression R join-condition S where join-condition is a conjunction of terms: A i oper B i in which A i is an attribute of R; B i is an attribute of S; and oper is one of =,, , . The meaning is: join-condition ´ (R S) where join-condition and join-condition ´ are the same, except for possible renamings of attributes (next)

11
11 Natural Join Special case of equijoin: – join condition equates all and only those attributes with the same name (condition doesn’t have to be explicitly stated) – duplicate columns eliminated from the result Transcript Transcript (StudId, CrsCode, Sem, Grade) Teaching ( Teaching (ProfId, CrsCode, Sem) Transcript Teaching Teaching = StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId Transcript ( Transcript Sem Teaching CrsCode=CrsCode AND Sem=Sem Teaching ) [ StudId, CrsCode, Sem, Grade, ProfId ]

12
12 Division Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s – r – r (A 1, …A n, B 1, …B m ) – s – s (B 1 …B m ) – rs s r – r/s, with attributes A 1, …A n, is the set of all tuples such that for every tuple in s, is in r Can be expressed in terms of projection, set difference, and cross-product

13
13 Set Operators SQL provides UNION, EXCEPT (set difference), and INTERSECT for union compatible tables Example: Find all professors in the CS Department and all professors that have taught CS courses ( SELECT P.Name ProfessorTeaching FROM Professor P, Teaching T WHERE P.Id=T.ProfId AND T.CrsCode LIKE ‘CS%’) UNION (SELECT P.Name Professor FROM Professor P WHERE P.DeptId = ‘CS’)

14
14 Division in SQL Query type: Find the subset of items in one set that are related to all items in another set Example: Find professors who taught courses in all departments – Why does this involve division? ProfId DeptIdDeptId All department Ids Contains row if professor p taught a course in department d ProfId,DeptId (Teaching Course) / DeptId (Department)

15
15 Aggregates Functions that operate on sets: – COUNT, SUM, AVG, MAX, MIN Produce numbers (not tables) Not part of relational algebra (but not hard to add) SELECT COUNT(*) Professor FROM Professor P SELECT MAX (Salary) Employee FROM Employee E

16
16 Grouping But how do we compute the number of courses taught in S2000 per professor? – Strategy 1: Fire off a separate query for each professor: SELECT COUNT( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ AND T.ProfId = 123456789 Cumbersome What if the number of professors changes? Add another query? grouping operator – Strategy 2: define a special grouping operator: SELECT T.ProfId, COUNT( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ GROUP BY T.ProfId

17
17 HAVING Clause Eliminates unwanted groups (analogous to WHERE clause, but works on groups instead of individual tuples ) HAVING condition is constructed from attributes of GROUP BY list and aggregates on attributes not in that list SELECT T.StudId, AVG (T.Grade) AS CumGpa, COUNT (*) AS NumCrs Transcript FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5

18
18 ORDER BY Clause Causes rows to be output in a specified order SELECT T.StudId, COUNT (*) AS NumCrs, AVG (T.Grade) AS CumGpa Transcript FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5 ORDER BY DESC CumGpa, ASC StudId Descending Ascending

19
Chapter 6 Relational Normalization Theory Redundancy in the schema – Update, deletion, insertion anomalies – Solution: decomposition Normalization theory – Functional dependencies – FD closure – Attribute closure 19

20
Chapter 6 Relational Normalization Theory (cont'd) – BCNF What are two conditions of BCNF? BCNF decomposition algorithm – 3NF What are 3 conditions of 3NF? How to calculate the minimal cover? 3NF decomposition algorithm – Lossless decomposition Conditions? R = R 1 R 2 … R n – Dependency preserving Conditions? F + = (F1 F2 F n ) + 20

21
21 Redundancy Dependencies between attributes cause redundancy – Ex. All addresses in the same town have the same zip code SSN Name Town Zip 1234 Joe Stony Brook 11790 4321 Mary Stony Brook 11790 5454 Tom Stony Brook 11790 …………………. Redundancy

22
22 Anomalies Redundancy leads to anomalies: – Update anomaly: A change in Address must be made in several places – Deletion anomaly: Suppose a person gives up all hobbies. Do we: Set Hobby attribute to null? No, since Hobby is part of key Delete the entire row? No, since we lose other information in the row – Insertion anomaly: Hobby value must be supplied for any inserted row since Hobby is part of key

23
23 Decomposition Person Solution: use two relations to store Person information – Person1 – Person1 (SSN, Name, Address) – Hobbies – Hobbies (SSN, Hobby) The decomposition is more general: people without hobbies can now be described No update anomalies: – Name and address stored once – A hobby can be separately supplied or deleted

24
24 Functional Dependencies functional dependency Definition: A functional dependency (FD) on a relation schema R is a constraint of the form X Y, where X and Y are subsets of attributes of R. satisfied Definition: An FD X Y is satisfied in an instance r of R if for every pair of tuples, t and s: if t and s agree on all attributes in X then they must agree on all attributes in Y – Key constraint is a special kind of functional dependency: all attributes of relation occur on the right-hand side of the FD: SSN SSN, Name, Address

25
25 Armstrong’s Axioms for FDs This is the syntactic way of computing/testing the various properties of FDs Reflexivity: If Y X then X Y (trivial FD) – Name, Address Name Augmentation: If X Y then X Z YZ – If Town Zip then Town, Name Zip, Name Transitivity: If X Y and Y Z then X Z

26
26 Generating F + F AB C AB BCD A D AB BD AB BCDE AB CDE D E BCD BCDE Thus, AB BD, AB BCD, AB BCDE, and AB CDE are all elements of F + union aug trans aug decomp

27
27 Computation of Attribute Closure X + F closure := X; // since X X + F repeat old := closure; if there is an FD Z V in F such that Z closure and V closure then closure := closure V until old = closure – If T closure then X T is entailed by F

28
28 Example: Computation of Attribute Closure AB C (a) A D (b) D E (c) AC B (d) Problem: Compute the attribute closure of AB with respect to the set of FDs : Initially closure = {AB} Using (a) closure = {ABC} Using (b) closure = {ABCD} Using (c) closure = {ABCDE} Solution:

29
29 BCNF Definition: A relation schema R is in BCNF if for every FD X Y associated with R either – Y X (i.e., the FD is trivial) or – X is a superkey of R Person1 Example: Person1(SSN, Name, Address) – The only FD is SSN Name, Address Person1 – Since SSN is a key, Person1 is in BCNF

30
30 Third Normal Form A relational schema R is in 3NF if for every FD X Y associated with R either: – Y X (i.e., the FD is trivial); or – X is a superkey of R; or – Every A Y is part of some key of R 3NF is weaker than BCNF (every schema that is in BCNF is also in 3NF) BCNF conditions

31
31 Lossless Schema Decomposition A decomposition should not lose information lossless A decomposition (R 1,…,R n ) of a schema, R, is lossless if every valid instance, r, of R can be reconstructed from its components: where each r i = Ri (r) r = r 1 r2r2 rnrn ……

32
32 Testing for Losslessness A (binary) decomposition of R = (R, F) into R 1 = (R 1, F 1 ) and R 2 = (R 2, F 2 ) is lossless if and only if : – either the FD (R 1 R 2 ) R 1 is in F + – or the FD (R 1 R 2 ) R 2 is in F +

33
33 Dependency Preservation Consider a decomposition of R = (R, F) into R 1 = (R 1, F 1 ) and R 2 = (R 2, F 2 ) – An FD X Y of F + is in F i iff X Y R i – An FD, f F + may be in neither F 1, nor F 2, nor even (F 1 F 2 ) + Checking that f is true in r 1 or r 2 is (relatively) easy Checking f in r 1 r 2 is harder – requires a join Ideally: want to check FDs locally, in r 1 and r 2, and have a guarantee that every f F holds in r 1 r 2 dependency preserving The decomposition is dependency preserving iff the sets F and F 1 F 2 are equivalent: F + = (F 1 F 2 ) + – Then checking all FDs in F, as r 1 and r 2 are updated, can be done by checking F 1 in r 1 and F 2 in r 2

34
34 BCNF Decomposition Algorithm Input: R = (R; F) Decomp := R while there is S = (S; F ’ ) Decomp and S not in BCNF do Find X Y F ’ that violates BCNF // X isn’t a superkey in S Replace S in Decomp with S 1 = (XY; F 1 ), S 2 = (S - (Y - X); F 2 ) // F 1 = all FDs of F ’ involving only attributes of XY // F 2 = all FDs of F ’ involving only attributes of S - (Y - X) end return Decomp

35
35 Third Normal Form Compromise – Not all redundancy removed, but dependency preserving decompositions are always possible (and, of course, lossless) 3NF decomposition is based on a minimal cover

36
36 Minimal Cover minimal cover A minimal cover of a set of dependencies, F, is a set of dependencies, U, such that: – U is equivalent to F (F + = U + ) – All FDs in U have the form X A where A is a single attribute – It is not possible to make U smaller (while preserving equivalence) by Deleting an FD Deleting an attribute from an FD (either from LHS or RHS) redundant – FDs and attributes that can be deleted in this way are called redundant

37
37 Computing Minimal Cover Example: F = {ABH CK, A D, C E, BGH L, L AD, E L, BH E} step 1: Make RHS of each FD into a single attribute – Algorithm: Use the decomposition inference rule for FDs – Example: L AD replaced by L A, L D ; ABH CK by ABH C, ABH K step 2: Eliminate redundant attributes from LHS. – Algorithm: If FD XB A F (where B is a single attribute) and X A is entailed by F, then B was unnecessary – Example: Can an attribute be deleted from ABH C ? Compute AB + F, AH + F, BH + F. Since C (BH) + F, BH C is entailed by F and A is redundant in ABH C.

38
38 Computing Minimal Cover (con’t) step 3: Delete redundant FDs from F – Algorithm: If F – {f} entails f, then f is redundant If f is X A then check if A X + F-{f} – Example: BGH L is entailed by E L, BH E, so it is redundant Note: The order of steps 2 and 3 cannot be interchanged!! See the textbook for a counterexample

39
39 Synthesizing a 3NF Schema step 1: Compute a minimal cover, U, of F. The decomposition is based on U, but since U + = F + the same functional dependencies will hold – A minimal cover for F={ABH CK, A D, C E, BGH L, L AD, E L, BH E} is U={BH C, BH K, A D, C E, L A, E L} Starting with a schema R = (R, F)

40
40 Synthesizing a 3NF schema (con’t) step 2: Partition U into sets U 1, U 2, … U n such that the LHS of all elements of U i are the same – U 1 = {BH C, BH K}, U 2 = {A D}, U 3 = {C E}, U 4 = {L A}, U 5 = {E L}

41
41 Synthesizing a 3NF schema (con’t) step 3: For each U i form schema R i = (R i, U i ), where R i is the set of all attributes mentioned in U i – Each FD of U will be in some R i. Hence the decomposition is dependency preserving – R 1 = (BHCK; BH C, BH K), R 2 = (AD; A D), R 3 = (CE; C E), R 4 = (AL; L A), R 5 = (EL; E L)

42
42 Synthesizing a 3NF schema (con’t) step 4: If no R i is a superkey of R, add schema R 0 = (R 0,{}) where R 0 is a key of R. – R 0 = (BGH, {}) R 0 might be needed when not all attributes are necessarily contained in R 1 R 2 … R n – A missing attribute, A, must be part of all keys (since it’s not in any FD of U, deriving a key constraint from U involves the augmentation axiom) R 0 might be needed even if all attributes are accounted for in R 1 R 2 … R n – Example: (ABCD; {A B, C D}). Step 3 decomposition: R 1 = (AB; {A B}), R 2 = (CD; {C D}). Lossy! Need to add (AC; { }), for losslessness – Step 4 guarantees lossless decomposition.

43
Chapter 7 Triggers and Active Databases Syntax of trigger – Events – Conditions – Actions 43

44
44 Trigger Overview Element of the database schema General form: ON IF THEN – Event- request to execute database operation – Condition - predicate evaluated on database state – Action – execution of procedure that might involve database updates Example: ON updating maximum course enrollment IF number registered > new max enrollment limit THEN deregister students using LIFO policy

45
45 Triggers in SQL:1999 Events: INSERT, DELETE, or UPDATE statements or changes to individual rows caused by these statements Condition: Anything that is allowed in a WHERE clause Action: An individual SQL statement or a program written in the language of Procedural Stored Modules (PSM) (which can contain embedded SQL statements)

46
46 Triggers in SQL:1999 Consideration: Immediate – Condition can refer to both the state of the affected row or table before and after the event occurs Execution: Immediate – can be before or after the execution of the triggering event – Action of before trigger cannot modify the database Granularity: Both row-level and statement-level

Similar presentations

OK

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.

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.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on combination of resistances training Ppt on supply chain management of a mule Ppt on video teleconferencing jobs Ppt on hydrostatic forces on submerged surfaces Ppt on abstract art painting Marketing mix ppt on nokia phones Ppt on solar power satellites sps Ppt on non conventional sources of energy Ppt on history of australia prisoners Ppt on object-oriented programming php