Download presentation
Presentation is loading. Please wait.
1
CSC 453 Database Systems Lecture
Tanu Malik College of CDM DePaul University
2
Today Review-Normalization Normalization-BCNF Recursion PL/SQL
3
Prime/Non-prime attributes
A BC, B C, D B AB C, C D, CE A
4
Consider the following relations R(Sname, Semail, Course, Instructor)
Students(S , Sname) Courses(Course, Instructor) Taking(student , Course) Gwen CSC453 Malik CSC443 Riely Priya
5
R(Sname, Semail, Course, Instructor)
Gwen CSC453 Malik CSC443 Riely Priya Maya Pitcher Students(S , Sname) Courses(Course, Instructor) Taking(S , Course)
6
Motivation for Normal Forms
Identify a “good” schema For some definition of good Avoid anomalies, redundancy Many normal forms 1st 2nd 3rd BCNF
7
First Normal Form (1NF) No multi‐valued attributes allowed
Counter-example Course(name, instructor, [student, ]*) Redundancy in non‐list attributes
8
Removing Multi-Valued Attributes
Remove the multi-valued attribute from the relation Create a new relation with the primary key of the original relation and the multi-valued attribute For each tuple in original relation with k values, add k tuples to the new relation Primary key of new relation contains all attributes; primary key of original relation becomes foreign key in new relation referencing original relation
9
Second Normal Form (2NF)
Second Normal Form: 1NF, plus every non-prime attribute in the relation is determined by the entire primary key (but not by any subset) To get 2NF, eliminate partial dependencies on the primary key XY is a partial dependency if ZY for some subset Z of X
10
Converting 1NF to 2NF (EmpID, EmpLName, EmpFName, Dept, ProjCode, Hours) Functional Dependencies EmpID EmpLName, EmpFName, Dept EmpID, ProjCode Hours
11
1NF vs 2NF Data EmpID EmpLName EmpFName Dept ProjCode Hours 550 Smith
Winston Accounting 101 20 252 10 601 Barney Finance 5 390 Hammond Evey Personnel 995 25 001 Preston Bill Special Events 100 Logan Ted 007 Bond James 505 Lane Lois Media Relations
12
Converting 1NF to 2NF Decompose (EmpID, EmpLName, EmpFName, Dept, ProjCode, Hours) Remove the offending FD EmpID EmpLName, EmpFName, Dept Two tables (EmpID, EmpLName, EmpFName, Dept) (EmpID, ProjCode, Hours)
13
Towards 2NF EmpID EmpLName EmpFName Dept 550 Smith Winston Accounting
601 Barney Finance 390 Hammond Evey Personnel 001 Preston Bill Special Events 100 Logan Ted 007 Bond James 505 Lane Lois Media Relations EmpID ProjCode Hours 550 101 20 252 10 601 5 390 995 25 001 100 007 505
14
Towards 2NF EmpID EmpLName, EmpFName, Dept EmpID, ProjCode Hours
550 Smith Winston Accounting 601 Barney Finance 390 Hammond Evey Personnel 001 Preston Bill Special Events 100 Logan Ted 007 Bond James 505 Lane Lois Media Relations EmpID ProjCode Hours 550 101 20 252 10 601 5 390 995 25 001 100 007 505 EmpID EmpLName, EmpFName, Dept EmpID, ProjCode Hours
15
Counterexample Movies(title, year, star, studio, studioAddress, salary) FD: title, year ‐> studio; studio ‐> studioAddress; star‐>salary
16
Removing Partial Dependencies
Find all dependencies where a subset of the primary key determines some non-prime attribute(s) Starting with the smallest subset, do the following: 1. Remove all attributes on the right-hand side from the relation and put them in a new relation 2. Add the attributes in the determinant(l.h.s) to the new relation; make them the primary key, and make them a foreign key in the original relation referencing the new table 3. Remove from any remaining partial dependencies any attributes removed from the original relation
17
Third Normal Form (3NF) Third Normal Form: 2NF, plus every non-prime attribute in the relation is determined only by the primary key of the relation. To get 3NF, eliminate transitive dependencies on the primary key XY is a transitive dependency if XZ and ZY for some Z that is disjoint from X
18
Counterexample Movies(title, year, star, studio, studioAddress, salary) FD: title, year ‐> studio; studio ‐> studioAddress; star‐>salary
19
Converting 2NF to 3NF Schema: (First , Last, Address, City, State, Zip) First, Last Address, City, State, Zip Transitive functional dependency: Zip City, State
20
2NF Data Table First Last Address City State ZipCode Henry Bienen
2145 Sheridan Rd Evanston IL 60202 Helmut Epp 55 E. Jackson St Chicago 60604 Denis Stein 55 E. Jackson St. David Miller 243 S. Wabash Av. Gary 5000 Forbes Av. Pittsburgh PA 15213 Leighton 77 Beacon St Cambridge MA 02139
21
Decompose the Tables City State ZipCode Evanston IL 60202 Chicago
First Last Address ZipCode Henry Bienen 2145 Sheridan Rd 60202 Helmut Epp 55 E. Jackson St 60604 Denis Stein 55 E. Jackson St. David Miller 243 S. Wabash Av. Gary 5000 Forbes Av. 15213 Leighton 77 Beacon St 02139 City State ZipCode Evanston IL 60202 Chicago 60604 Pittsburgh PA 15213 Cambridge MA 02139
22
Decompose the Tables ZipCode City, State
First Last Address ZipCode Henry Bienen 2145 Sheridan Rd 60202 Helmut Epp 55 E. Jackson St 60604 Denis Stein 55 E. Jackson St. David Miller 243 S. Wabash Av. Gary 5000 Forbes Av. 15213 Leighton 77 Beacon St 02139 City State ZipCode Evanston IL 60202 Chicago 60604 Pittsburgh PA 15213 Cambridge MA 02139 ZipCode City, State First, Last First, Last, Address, ZipCode
23
3NF Decomposition Input: A universal relation R and a set of functional dependencies F on R Output: A decomposition D of R into 3NF schemas with dependency preservation and nonadditive join
24
3NF-Normalization Algorithm (3NF Normalization):
Input: Relation R with FDs F c Output: 3NF decomposition D of R D = {} For every XY in F add sub-relation Q =(XY) to D, unless some sub-relation in D already contains all of XY: don’t add Q some sub-relation(S) in D is contained in XY: replace S with Q(XY) If no relation in D contains a key of R, then add new relation Q(X) on some key X of R
25
Example R = (A,B,C,D) A→ C D BA→ C The candiate key is ?
3NF decomposition is ? R1 = (C, A, D) R2 = (B, A, C)
26
3NF Decomposition R(A, B, C, D) R(A,B,C,D,E) R(A, B, C, D, E, F)
FDs: A B, C D R(A,B,C,D,E) FDs: CE, BC R(A, B, C, D, E, F) F = { AB CD, D A, C EF} (AB) (AC) (AB) (DB)
27
3NF Decomposition R={A, B, C, D, E, F} F = { AB CD, C EF, D A }
Non-prime determines a prime attribute
28
3NF-Checking Order Is F in minimal cover? What is the candidate key?
Which FDs violate 3NF? Decompose to 3NF. Is the decomposition lossless? Is it dependency preserving?
29
The End Result A collection of relations, each in 3NF
Each relation has a primary key (We are assuming that there is only one candidate key…) Every non-prime attribute in a relation is determined by its entire primary key No non-prime attribute in a relation is determined by any attributes other than its entire primary key Information can reconstructed using joins, and stored in views if desired
30
Boyce-Codd Normal Form
Boyce-Codd Normal Form (BCNF): For every non-trivial functional dependency XA, it must be the case that X is a superkey “Every determinant must contain a candidate key” X must be a superkey even if A is a prime attribute
31
BCNF example Pizza |Topping Type |Topping | | | cheese | mozzarella 1 | meat | pepperoni 1 | vegetable| olives 2 | meat | sausage 2 | cheese | mozzarella 2 | vegetable| peppers Each pizza must have exactly one of each topping type One type of cheese One type of meat One type of vegetable
32
BCNF example Pizza |Topping Type |Topping | | | cheese | mozzarella 1 | meat | pepperoni 1 | vegetable| olives 2 | meat | mozzarella 2 | cheese | sausage 2 | vegetable| peppers Pizza can have exactly 3 types of topping One type of cheese One type of meat One type of vegetable
33
Decompose Pizza |Topping | | | 1 |mozzarella| 1 |pepperoni | 1 |olives | 2 |mozzarella | 2 |sausage | 2 |peppers | Topping |Topping Type | mozzarella|cheese pepperoni |meat olives |vegetable mozzarella | cheese sausage |meat peppers |vegetable
34
BCNF Decomposition Input: A universal relation R and a set of functional dependencies F on R Output: A decomposition D of R into BCNF schemas with nonadditive join Algorithm on next page Algorithm does not guarantee dependency preservation
35
BCNF Decomposition Algorithm ALGORITHM BCNF (R: Relation, F: FD set)
BEGIN 1. D {R} 3. While some X → Y holds in some Ri(A1,…,An) in D and (X → Y) is not trivial, X is not a superkey of Ri Ri1 X+ ∩({A1,…,An}) Ri2 X ({A1,…,An} - X+ ) Result Result – {Ri} {Ri1,Ri2} 4. Return result END
36
BCNF Example: R = (A, B, C) F = {A → B, B → C} Is R in BCNF?
A: Consider the nontrivial dependencies in F: 1. A → B, A → R (A is a key) 2. B → C, B → A (B is not a key) Therefore, R not in BCNF
37
BCNF Example: Q: Is the decomposition lossless?
R = R1 R2 R1 = (A, B); R2 = (B, C) F = {A → B, B → C} Are R1, R2 in BCNF? A: 1. Test R1: A → B covered, A → R1 (all other FD’s covered trivial) 2. Test R2: B → C covered, B → R2 (all other FD’s covered trivial) R1, R2 in BCNF Q: Is the decomposition lossless?
38
BCNF Decompose R into BCNF: R = (A, B, C, D, E, H)
F = {A → BC, E → HA} Decompose R into BCNF:
39
BCNF Decomposition Decomposition #1: R = R1 R3 R4 Q: Is this DP?
R = (A, B, C, D, E, H) F = {A → BC, E → HA} (Note: Fc = F) Decomposition #1: R = R1 R3 R4 R = (A, B, C, D, E, H) Decompose on A → BC R1 = (A, B, C) R2 = (A, D, E, H) Decompose on E → HA R3 = (A, E, H) R4 = (D, E) Q: Is this DP? A: Yes. All Fc covered by R1, R3, R4. Therefore F+ covered
40
BCNF Decomposition (cont.)
R = (A, B, C, D, E, H) F = {A → B, E → HA, A → C} (Note: Fc = F) Decomposition #2: R = R1 R3 R5 R6 R = (A, B, C, D, E, H) Decompose on A → B R1 = (A, B) R2 = (A, C, D, E, H) Decompose on E → HA R3 = (A, E, H) R4 = (C, D, E) Decompose on E → C R5 = (C, E) R6 = (E, D) Q: Not DP. Why? A: A → C not covered by R1, R3, R5 , R6.
41
BCNF Decomposition R(A, B, C, D, E, F) F = { AB CD, C EF, D A }
(AC) (AB) (DB) (CB)
42
BCNF Decomposition R (S, P, Q, X, Y, N, C)
F = { S NC, P XY, SP Q , QP } Decompose to BCNF Is it dependency preserving?
43
Properties of Decompositions
When we work with BCNF, we must look at properties involving multiple relations: Nonadditive (Lossless) Join: No tuples that are not in the original relation (spurious tuples) are generated when decomposed relations are joined Dependency Preservation: Every functional dependency in the original relation is represented somewhere in the decomposition
44
BCNF vs. 3NF Every relation in BCNF is in 3NF
Not every relation in 3NF is in BCNF 3NF relations that are not in BCNF fail because some prime attribute is determined by something that is not a superkey – this is allowed by 3NF but not by BCNF Decomposing tables into BCNF can be tricky – functional dependencies can be lost!
45
Remarks on Algorithms Different runs may yield different results, depending on the order in which attributes and functional dependencies are considered We must know all functional dependencies We can’t always guarantee dependency preservation for BCNF, but we can generate a 3NF decomposition and then consider the individual relations in the result
46
PL/SQL A general-purpose procedural programming that includes SQL commands PL/SQL can create and issue SQL statements store and process the results of queries define procedures to respond to database events
47
Basic Structure of Code
Simplest form is an anonymous block: declare -- variable and subprogram declarations -- every statement must end with a ; begin -- PL/SQL statements to execute --every statement must end with a ; --statements can be nested with another B/E exception -- exception handling (optional) end;
48
Output To display output: RAISE NOTICE ‘string %’, arguments;
Output buffer displayed in DBMS Output tab Use View Dbms Output and ‘+’ to open tab Single line comments with – Multi-line with /* */
49
Data Types Numeric Character Boolean Datetime
Data types are not case sensitive DECLARE num1 INTEGER; num2 REAL; num3 DOUBLE PRECISION; BEGIN null; END; /
50
Declaring Variables All variables must be declared:
varName [CONSTANT] dataType [NOT NULL] [:= initialValue]; Assignments use :=, and PL/SQL has typical arithmetic operations
51
Scoping DECLARE BEGIN / -- Global variables num1 number := 95;
dbms_output.put_line('Outer Variable num1: ' || num1); dbms_output.put_line('Outer Variable num2: ' || num2); -- Local variables num1 number := 195; num2 number := 185; dbms_output.put_line('Inner Variable num1: ' || num1); dbms_output.put_line('Inner Variable num2: ' || num2); END; /
52
Declaring Variables Only one variable can be declared per line, but variable types can be given in terms of the domain of another variable or attribute: varName otherVar%type; varName TABLE.Attribute%type;
53
Operators Arithmetic operators Relational operators
Comparison operators LIKE, BETWEEN, IN, IS NULL Logical operators String operators
54
Branching if-then: if condition then
…’true’ statements… end if; if-else: if condition then …’true’ statements… else …’false’ statements… end if;
55
Branching if-elsif: if condition1 then … ‘true’ statements… elsif condition2 then … ‘false-true’ statements… elsif condition3 then … ‘false-false-true’ statements… (… as many times as needed…) else … ‘all false’ statements… end if;
56
Case Statement CASE [ expression ] WHEN condition_1 THEN result_1
... WHEN condition_n THEN result_n ELSE result END
57
Case Statement expression condition_1, condition_2, ... condition_n
Optional. It is the value that you are comparing to the list of conditions. (ie: condition_1, condition_2, ... condition_n) condition_1, condition_2, ... condition_n The conditions that must all be the same datatype. The conditions are evaluated in the order listed. Once a condition is found to be true, the CASE statement will return the result and not evaluate the conditions any further. result_1, result_2, ... result_n Results that must all be the same datatype. This is the value returned once a condition is found to be true.
58
Loops General loop: loop …loop body… end loop;
Repeats until exit; is executed in loop body While loop: while condition loop …loop body… end loop; Repeats until condition is false
59
Loops For loop: for variable in [reverse] lower..upper loop …loop body… end loop; Can only increment/decrement by one lower always appears before upper in header
60
Incorporating SQL Queries
Result of a query can be stored in a set of variables by adding INTO clause to query: SELECT list of attributes INTO list of variables FROM list of tables … Variable types must match attribute types
61
Procedures (In Oracle)
CREATE [OR REPLACE] PROCEDURE name (paramName IN [OUT] paramType …) AS …declarations… BEGIN …body of procedure… END; / ‘IN’ parameters are passed by value, for input only, read-only parameters ‘OUT’ parameters are passed by reference ‘IN OUT’ parameters are passed by reference, to return results to the calling sub-program
62
Functions CREATE [OR REPLACE] FUNCTION mode {IN|OUT|INOUT} name …) RETURNS returnType AS $$ …declarations… BEGIN …body of function… return returnValue; END; $$ language plpgsql; ‘IN’ parameters are default Specify return type and return value instead
63
Executing Procedures and Functions
A standalone procedure Using the EXECUTE keyword Calling the name of the procedure from a PL/SQL block A standalone function Calling the name of the function from a PL/SQL block Calling the name of the function in a SQL query
64
Cursors A cursor represents a pointer into a set of records returned by a query declare name cursor for query; cursor name can be used to iterate through the records returned by query
65
Cursor Commands/Expressions
open name; -- initializes to beginning of set fetch name into variableList; -- reads the next record into the variables close name; -- closes the cursor
66
Parameterized Cursors
Can supply a parameter in cursor declaration and query declare name (parameter in type) cursor for query; Each time cursor is opened, value of parameter is specified in parentheses to complete the query
67
Implicit Cursors for DML statements
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor is associated with this statement. INSERT operations: the cursor holds the data that needs to be inserted. UPDATE and DELETE operations: the cursor identifies the rows that would be affected.
68
Records Data structure to hold data items of different kinds
Table-based Records: Can create a record with same structure as the row of a table (fields are table attributes): recordName TABLE%rowtype; Can select a row of a table directly into a record, and access individual fields with recordName.Attribute
69
Records Cursor-based records: Assign rowtype from a query in cursor
User-defined Records: Declare a new data type and a table of records: create type newType ( attr1 datatype, attr2 datatype )
70
Exceptions DECLARE <declarations section> BEGIN <executable command(s)> EXCEPTION <exception handling goes here > WHEN exception1 THEN exception1-handling-statements WHEN exception2 THEN exception2-handling-statements WHEN exception3 THEN exception3-handling-statements WHEN others THEN exception3-handling-statements END;
71
Case Statement SELECT LastName, FirstName,
(CASE Career WHEN 'UGRD' THEN 'Undergraduate' WHEN 'GRD' THEN 'Graduate' WHEN ' SAL' THEN 'Student At Large' END) AS Career FROM student; UPDATE employee SET salary = (CASE WHEN salary < THEN 50000 WHEN salary < THEN salary * 1.05 ELSE salary * 1.1 END);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.