Download presentation
Presentation is loading. Please wait.
1
CS 440 Database Management Systems
Schema Design Practice and Stored procedures
2
Question 1: FD & key Consider a relation R(A,B,C,D,E) with FD's, S={AB C, CD E, C A, C D, D B}: a) Determine all the keys of relation R. Do not list super keys that are not a minimal key. Solution: Keys: AB, AD, C To get the key AB, we can do the following: From AB C and C D, we obtain AB D. From AB C and AB D, we obtain AB CD. From AB CD and CD E, we obtain AB E.
3
Question 1 (solution, cont’d)
To get the key AD, we can do the following: From D B, we can get AD AB. From AB, we can obtain the rest of the attributes. To get the key C, we can do the following: From C A and C B, we obtained C AB.
4
Question 2: FD Consider a relation R(A, B, C, D, E, F) with the following set of FD’s : S:{ ABC, CDE, B D, EA, CFB} a) Give an example of FD that follows from S and explain your answer. Solution: AB D, D is in the closure of AB. Because A B and B D Thus AB D is a valid FD that follows S.
5
Question 2 (solution, cont’d)
Consider a relation R(A, B, C, D, E, F) with the following set of FD’s : S:{ ABC, CDE, B D, EA, CFB} b) Give an example of FD that does not follow from S and explain your answer. Solution: B C, C is not in the closure of B. B doesn’t uniquely identify C accordance to S. So, BC is not valid accordance to S.
6
Question 3: BCNF Consider relation R (A, B, C) with a set of FDs
F={AB → C, C→A}. determine whether R is in BCNF. Solution: The keys are AB and BC. R is not in BCNF since left hand side of C→A is not a super key.
7
Question 4: BCNF Consider the relation schema R(A, B, C, D, E) with FD’s, A BCDE, C D, and CE B . Decompose the relation till it follows BCNF . Solution: R is not in BCNF because CE B and CE is not a super key. Decompose R: R1= {CEB}, R2={ACDE} R1 is in BCNF R2 is not in BCNF, because C D and C is not a super key Decompose R2: R21= {C,D} , R22={A,C,E} R1,R21,R22 are in BCNF.
8
Question 5: BCNF Consider a relation R=(A,B,C,D,E) with the following functional dependencies, S= {BC ADE, D B}. a) Find all candidate keys. Solution: The keys are {B,C} and {C,D}. {B,C} is a key from BC ADE. To get the key {C,D}: from D B we get B, with B and C we have BC ADE.
9
Question 5 cont’d Consider a relation R=(A,B,C,D,E) with the following functional dependencies, S= {BC ADE,D B}. b) Identify whether or not R is in BCNF. Solution: The relation is not BCNF because D is not a super key which violates BCNF.
10
Question 6: BCNF Consider a relation R = (A,B,C,D,E) with the following functional dependencies, S= {CE D,D B,C A}. a) Find all candidate keys. Solution: The only key is {C,E} To get the key CE, we can do the following: From CE D and D B, we obtain CE B. From CE D and C A, we obtain CE AD.
11
Question 6 ,cont’d Consider a relation R = (A,B,C,D,E) with S = {CE D,D B,C A}. b) If the relation is not in BCNF, decompose it until it becomes BCNF. Solution: Relation R is not in BCNF. Step 1: Decomposes R into R1=(A,C) and R2=(B,C,D,E). Resulting R1 is in BCNF. R2 is not. Step 2: Decompose R2 into, R21=(C,D,E) and R22=(B,D). Both relations are in BCNF.
12
Writing Large DB Programs
How to to write large scale programs that use a database? SQL cannot express all types of queries: e.g., recursive queries. no support for advanced features, e.g., GUI. Embed SQL in a programming language to write programs that contain many queries Libraries to submit SQL queries as strings to RDBMS PHP, Java: JDBC,
13
Problems with Using SQL in PL
The programs are hard to debug and maintain Programmers have to run their programs to find SQL compilation bugs. If a programmer adds an attribute to a table, she has to manually find and change many lines in the source code. Communication overhead queries are sent to RDBMS per each call.
14
Stored Procedures Programming modules written in a procedural language. Stored in the database. Compiled and run by RDBMS. Most RDBMS provide their own language to write stored procedures. Oracle: PL / SQL SQL Server: Transact-SQL Some RDBMSs use widely used programming languages VoltDB: Java
15
Example: PL/SQL Create a stored procedure that inserts the integer numbers between 1 and 100 into table NumOddEven(numVal, oddEven). CREATE OR REPLACE PROCEDURE sample IS BEGIN FOR i IN LOOP IF MOD(i,2) = 0 THEN i is even INSERT INTO numOddEven VALUES (i, ‘i is even’); ELSE INSERT INTO numOddEven VALUES (i, ‘i is odd’); END IF; END LOOP; COMMIT; END
16
Example: PL/SQL Called by user for from a program.
> Execute sample Users can pass input parameters to stored procedures For example, the number of values to insert in table NumOddEven.
17
Advantages of Stored Procedures
They are more expressive than SQL. It is easy to use SQL in them SQL queries are parts of the programming language. many errors are detected in compilation phase. They run faster than using SQL in programming languages such as PHP minimal communication overhead for submitting queries to RDBMS. and sometimes no communication overhead for results queries are pre-compiled and may be pre-optimized.
18
Disadvantages of Stored Procedures
They do not support some advanced features e.g., graphical user interface. Each RDBMS has its own. If we write a program over a RDBMS, we cannot run it over another one. They are harder to write, debug, and maintain than the programs written in Java or C++.
19
When to use Stored Procedures
Generally Speaking: They should be used for portions of the program whose performances matter a lot. ID generation They should contain minimal amount of business logic keep them simple so they are easy to debug and maintain.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.