CS 440 Database Management Systems

Slides:



Advertisements
Similar presentations
CS 440 Database Management Systems Practice problems for normalization.
Advertisements

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.
Programming Types of Testing.
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
ABC Insurance Co. Paul Barry Steve Randolph Jing Zhou CSC8490 Database Systems & File Management Dr. Goelman Villanova University August 2, 2004.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Advanced SQL: Cursors & Stored Procedures
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Final Exam Revision Instructor: Mohamed Eltabakh 1.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Section 3 CSE 444 Introduction to Databases. Announcements Project 1 was due yesterday (10/14/2009) Homework 1 was released, due 10/28/2009.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
CS 440 Database Management Systems Stored procedures & OR mapping 1.
By Durga Dasari. DbFit - DB Unit and Integration test tool  Test-driven database development. Write readable, easy- to-maintain unit and integration.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Formal definition of a key A key is a set of attributes A 1,..., A n such that for any other attribute B: A 1,..., A n  B A minimal key is a set of attributes.
Web Database Programming Using PHP
Databases and DBMSs Todd S. Bacastow January
Top 8 Best Programming Languages To Learn
Relational Database Schema Designer Using Bernstein’s Algorithm
Chapter 9: The Client/Server Database Environment
A Guide to SQL, Seventh Edition
CS 440 Database Management Systems
PL/pgSQL
CS 540 Database Management Systems
Stored Procedures.
PGT(CS) ,KV JHAGRAKHAND
Web Technologies IT230 Dr Mohamed Habib.
CS422 Principles of Database Systems Normalization
Introduction and Principles
Web Database Programming Using PHP
CS411 Database Systems 08: Midterm Review Kazuhiro Minami 1.
Dynamic SQL: Writing Efficient Queries on the Fly
Database Systems: Design, Implementation, and Management Tenth Edition
CS422 Principles of Database Systems Normalization
Relational Database Design by Dr. S. Sridhar, Ph. D
UNIT - V STORED PROCEDURE.
Database Application Development
The Client/Server Database Environment
Haritha Dasari Josue Balandrano Coronel -
Lecture 2 Introduction to Programming
PHP / MySQL Introduction
Database Performance Tuning and Query Optimization
T-SQL Transact - Structured Query Language (Microsoft)
CPSC-310 Database Systems
ISC440: Web Programming 2 Server-side Scripting PHP 3
Client Access, Queries, Stored Procedures, JDBC
Module 5: Overview of Normalization
CS 440 Database Management Systems
How to test Whether Subschemes in BCNF??
Dynamic SQL: Writing Efficient Queries on the Fly
Chapter 15 Basics of Functional Dependencies and Normalization for Relational Databases.
Knowledge Byte In this section, you will learn about:
Some slides are from Dr. Sara Cohen
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
Database Systems: Design, Implementation, and Management Tenth Edition
Review #1 Intro stuff What is a database, 4 parts, 3 users, etc.
CMPE 152: Compiler Design March 19 Class Meeting
Chapter 7a: Overview of Database Design -- Normalization
Modern Collections Classes & Generics
Database Application Development
CS4222 Principles of Database System
Presentation transcript:

CS 440 Database Management Systems Schema Design Practice and Stored procedures

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.

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.

Question 2: FD Consider a relation R(A, B, C, D, E, F) with the following set of FD’s : S:{ ABC, CDE, B D, EA, CFB} 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.

Question 2 (solution, cont’d) Consider a relation R(A, B, C, D, E, F) with the following set of FD’s : S:{ ABC, CDE, B D, EA, CFB} 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, BC is not valid accordance to S.

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.

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.

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.

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.

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.

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.

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,

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.

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

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 1.. 100 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

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.

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.

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++.

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.