Upload
technofreak9
View
110
Download
6
Embed Size (px)
DESCRIPTION
PLSQL Codes for Database Security
Citation preview
University Of Wollongong, Dubai CampusCSCI235 – Databases
Assignment 2Date of submission: Thursday 5 pm-Week 13.Note:
1) This assignment is a group work of no more than two students.2) Detection of plagiarism or copying of work will lead to severe penalty.
Normalization
Objectives:The objective of this assignment is to normalize the given conceptual database schema.
[2.5 marks]1- Consider the following collection of relations and dependencies. Assume that each
relation is obtained through decomposition from a relation with attributes ABCDEFGHI and that all the known dependencies over relation ABCDEFGHI are listed for each question.
a. state the strongest normal form that the relation is in (1NF, 2NF, 3NF and BCNF)
b. If it is not in BCNF, decompose it into a collection of BCNF relations.
R1(A,C,B,D,E), A B, C D
Answer:It is 1NFThe BCNF decomposition is: (A,B) (C,D) and (A,C,E)
2- Suppose you are given a relation R with four attributes ABCD. For each of the following sets of FDs, assuming those are the only dependencies that hold for R. Do the following: [7.5 marks 2.5 marks each]
a. Identify the candidate key(s) for Rb. Identify the best normal form that R satisfies (1NF, 2NF, 3NF, BCNF)c. If R is not in BCNF, decompose it into a set of BCNF relations that preserves
the dependencies.
I. C D, C A, B CCandidate key(B)2NF(A,C,D) and C D, C A BCNF(B,C) and B C BCNF
II. ABC D, D AKey(ABC), 3NF and it is not possible to make it BCNF
III. AB C, AB D, C A, D BCandidate Key(AB, BC, AD, CD) 3NF but can not be BCNF
3- Consider the attribute set R= ABCDEGH and the following FD set: [10 marks, 2.5 each]
F= { AB C, AC B, AD E, B D, BC A, E G}a) Name the strongest normal form that each relation satisfies. Decompose it into a
collection of BCNF relations if it is not in BCNF.
I. ABCDAB C, AC B, B D, BC A1NF(A,B,C) and AB C, AC B, BC A (candidate keys(AB, AC, BC) BCNF(B,D) and B D, (candidate keys(B)) BCNF
II. DCEGHE G1NF(D,C,H,E) BCNF key(D,C,H,E)(E,G) E G, key(E) BCNF
III. ACEHNo dependency so, key(A,C,E,H) BCNF
b) which of the following decompositions of R=ABCDEG with the same set of dependencies F, is a) dependency –preserving b) lossless-join?
{AB, BC, ABDE, EG}
{AB , BC, ABDE (AD E, B D), , EG (E G )}
This decomposition is not lossless becauseThis dependency AB C tells us that value of C depends ob both A and B but in table BC we only have B, so we do not have a complete foreign key to BC from AB or ABDE
or dependency preserving because:the dependencies AB C, AC B, BC A, are not part of any tables.
Transaction Management: Objectives:The objective of this assignment is to practice assigning isolation levels to control concurrency in database transactions. [10 marks, 2.5 each]
Consider the university enrollment database schema:
Student(snum: integer, sname: string, major: string, level: string, age: integer)Class(name: string, meets at: time, room: string, fid: integer)Enrolled(snum: integer, cname: string)Faculty(fid: integer, fname: string, deptid: integer)
The meaning of these relations is straightforward; for example, Enrolled has one recordper student-class pair such that the student is enrolled in the class.For each of the following transactions, state the SQL isolation level you would use andexplain why you chose it.
1. Enroll a student identified by her snum into the class named ’Introduction toDatabase Systems’.
Read committed (because it is lowest isolation level which is not read only)
2. Change enrollment for a student identified by her snum from one class to anotherclass.
Read committed if no other insert into enrollment is happening orSERIALIZABLE. To prevent inserting the same snum and the same course into the table.
3. Assign a new faculty member identified by his fid to the class with the least numberof students.
SERIALIZABLE
4. For each class, show the number of students enrolled in the class.
Read uncommitted because we are just looking for a sum in all classes.But in order to make the sums accurate, it has to be SERIALIZABLE
PL/SQL
Objectives:The objective of this assignment is to practice with PL/SQL.
A. Answer the following questions based on the PL/SQL program belowYou are working for a company and your boss gives you the following table Employee and a stored procedure below and a program (on next page).
A.1. Describe what the program does. (4 marks)A.2. Run the program (on the next page) and show the resulting table. (6 marks)
Employee TableEmployee Year_Experience Salary10001 5 500010002 10 1000010003 2 500010004 4 500010005 3 300010006 8 10000
PROCEDURE update_ES (iSSN INTEGER, fNewSalary NUMBER) IS fCurSalary NUMBER(10, 2); missing_salary EXCEPTIONBEGIN SELECT Salary INTO fCurSalary FROM Employee WHERE SSN = iSSN; IF fCurSalary IS null THEN RAISE missing_salary; ELSE
UPDATE Employee SET Salary = fNewSalary WHERE SSN = iSSN; END IF;COMMIT;EXCEPTIONWHEN NO_DATA_FOUND THEN INSERT INTO item_audit
VALUES (iSSN, 'Invalid Employee identifier.'); COMMIT;WHEN missing_salary THEN INSERT INTO item_audit
VALUES (iSSN, 'Salary is null.'); COMMIT;WHEN OTHERS THEN ROLLBACK; INSERT INTO item_audit VALUES (iSSN, 'Miscellaneous error.'); COMMIT;END update_ES;
Employee TableEmployee Year_Experience Salary10001 5 500010002 10 10500
10004 4 500010005 3 300010006 8 10500
Deleted 10003
2 5000
DECLARE CURSOR emp_cursor IS
SELECT emp_year_exp, emp_salary, emp_SSN FROM Employee; emp_rec item%ROWTYPE;
BEGIN FOR emp_rec IN emp_cursor LOOP IF (emp_rec.emp_year_exp > 10) THEN
update_ES(emp_rec.emp_SSN, emp_rec.salary + emp_rec.salary * 0.10)
ELSIF ((emp_rec.emp_year_exp <= 10) AND (emp_rec.emp_year_exp > 5))
THEN
update_ES(emp_rec.emp_SSN, item emp_rec.salary + emp_rec.salary * 0.05);
ELSIF ((emp_rec.emp_year_exp < 5) AND (emp_rec.emp_year_exp >= 3))
THEN ;
ELSIF ((emp_rec.emp_year_exp < 3) DELETE Employee WHERE CURRENT OF emp_cursor; END IF; END LOOP; COMMIT; -- Commit the transaction WHEN OTHERS THEN ROLLBACK;END;
Exercise 19.7 Suppose you are given a relation R with four attributes ABCD. Foreach of the following sets of FDs, assuming those are the only dependencies that holdfor R, do the following: (a) Identify the candidate key(s) for R. (b) Identify the bestnormal form that R satisfies (1NF, 2NF, 3NF, or BCNF). (c) If R is not in BCNF,decompose it into a set of BCNF relations that preserve the dependencies.1. C → D, C → A, B → C2. B → C, D → A3. ABC → D, D → A4. A → B, BC → D, A → C5. AB → C, AB → D, C → A, D → BAnswer 19.71. (a) Candidate keys: B(b) R is in 2NF but not 3NF.Schema Refinement and Normal Forms 193(c) C → D and C → A both cause violations of BCNF. One way to obtain a(lossless) join preserving decomposition is to decompose R into AC, BC, andCD.2. (a) Candidate keys: BD(b) R is in 1NF but not 2NF.(c) Both B → C and D → A cause BCNF violations. The decomposition: AD,BC, BD (obtained by first decomposing to AD, BCD) is BCNF and losslessand join-preserving.3. (a) Candidate keys: ABC, BCD(b) R is in 3NF but not BCNF.(c) ABCD is not in BCNF since D → A and D is not a key. However if we splitup R as AD, BCD we cannot preserve the dependency ABC → D. So thereis no BCNF decomposition.4. (a) Candidate keys: A(b) R is in 2NF but not 3NF (because of the FD: BC → D).(c) BC → D violates BCNF since BC does not contain a key. So we split up Ras in: BCD, ABC.5. (a) Candidate keys: AB, BC, CD, AD(b) R is in 3NF but not BCNF (because of the FD: C → A).(c) C → A and D → B both cause violations. So decompose into: AC, BCDbut this does not preserve AB → C and AB → D, and BCD is still notBCNF because D → B. So we need to decompose further into: AC, BD,CD. However, when we attempt to revive the lost functioanl dependenciesby adding ABC and ABD, we that these relations are not in BCNF form.Therefore, there is no BCNF decomposition.