Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 440 Database Management Systems

Similar presentations


Presentation on theme: "CS 440 Database Management Systems"— Presentation transcript:

1 CS 440 Database Management Systems
Views, Stored procedure

2 Views Virtual relations defined over base relations
Create View <name> As <query>; create view Fans(name, addr, phone) that contains information about people who like at least one beer. Create View Fans As Select name, addr, phone From Likes, Drinkers Where Drinkers.name = Likes.drinker; Used similar to base relations. using view Fans, find the phone numbers of people who like at least one beer. Select phone From Fans;

3 Advantages of using views
Views make databases more usable. easier for users to find relevant relations. HR users in a large company. DBAs create the relevant views for each group of users. Fans view for the marketing division. Views make databases more secure. Each group of users should access only a subset of database. HR users should not access accounting relations. DBA defines views and their access privileges.

4 Maintaining views Store their definitions: virtual view
rewriting the query using view definitions Pre-compute the views: view materialization marketing department issues a lot of queries over Fans advantage: improves the running time for some queries. disadvantage: refreshing the view as its base relations modified. Which views to materialize? Tradeoff: #read from view vs. #write to its base relations popular in data warehouses only read, no write

5 Maintaining materialized views
Two steps Propagate: compute changes to the view when data changes. Refresh: apply changes to the materialized view table. Maintenance policy Immediate: as soon as the base tables are updated. advantage: views are always consistent. disadvantage: updates are slower. Deferred: some time later advantage: can scale to maintain many views without slowing down updates. disadvantage: views may become inconsistent.

6 Deferred maintenance Lazy Periodic (snapshot)
Delay refresh until the next query over view Refresh before answering the query Periodic (snapshot) refresh the view periodically disadvantage: Some queries may be answered over an outdated version of view. advantage: scale to update many views more widely used

7 Support for view materialization
Oracle create materialized view Fans As Select name, addr, phone From Likes, Drinkers Where Drinkers.name = Likes.drinker; Also called snapshot. re-compute view entirely or incrementally update the view

8 Is SQL Sufficient? Using IsParent(parent,child), find grand children of a given person. Now find all descendants of a given person. It is not possible to write this query in standard SQL! We can prove it.

9 Writing Complex Queries
SQL cannot express some complex queries. We can add more programming constructs like recursion to SQL: Recursive SQL Oracle’s connected by

10 Writing Large DB Programs
We have to write large scale programs that query the database. SQL does not support features such as graphical user interface development. SQL is not enough!

11 Using SQL in a PL Using SQL in a programming language to write programs that contain many queries PHP, Java, C, C++,… CS 275

12 Stored Procedures Programming modules written in a procedural language. Stored in the database. Compiled and run by RDBMS. Called by user.

13 Stored Procedure Programming modules written in a procedural language.
We cannot use standard procedural languages like C. RDBMS must support the language.

14 Stored Procedure Each RDBMS provides its own language to write stored procedures. Oracle: PL / SQL SQL Server: Transact-SQL

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 The languages of stored procedures support most features of programming languages. 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. They run faster than using SQL in standard programming languages such as PHP, Java, …. No need to submit multiple SQL statements to the RDBMS.

18 Disadvantages of Stored Procedures
They do not support features such as 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++. They do not generally have the nice object-oriented features of Java or C++.

19 When to use Stored Procedures
Generally Speaking: They should contain small number of queries. Keep them simple so they are easy to maintain. They should be used for portions of the program whose performances matter a lot. ID generation


Download ppt "CS 440 Database Management Systems"

Similar presentations


Ads by Google