Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.

Similar presentations


Presentation on theme: "1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that."— Presentation transcript:

1 1 Views

2 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that there is a single table in the database CREATE OR REPLACE VIEW SRB as SELECT S.sid, sname, rating, age, R.bid, day, bname, color FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid

3 3 What are views good for?(2) Find names of Sailors who reserved boat 103 using SRB SELECT sname FROM SRB WHERE bid=‘103’

4 4 What are views good for?(3) Security issues – preventing unauthorized access. Example: hiding the rating value CREATE VIEW SailorInfo SELECT sname, sid, age FROM Sailors grant SELECT on SailorInfo to joe;

5 5 Changing Tables through a View

6 6 Changing a Table through a View If a view is based on a single table you can insert, update and delete rows from the table through the view, if you have the necessary permissions, under the following conditions: –You can’t insert if the underlying table has non null columns not appearing in the view –You can’t insert or update if any of the view columns referenced in the command contains functions or calculations –You can’t insert, update or delete if the view contains group by or distinct

7 7 What can be changed? Only Values of Rows seen through the View Only Values of Columns seen through the View Insert No (Yes if CHECK) Yes UpdateYes DeleteYesNo

8 8 Inserting Allowed CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; INSERT INTO OldSailors(sid,sname,age,rating) VALUES(12, ‘ Joe ’,51,10); When we select from OldSailors next time, we will see Joe

9 9 Inserting Allowed CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; INSERT INTO OldSailors(sid,sname,age,rating) VALUES(12, ‘ Mary ’,49,10); When we select from OldSailors next time, we will not see Mary!

10 10 WITH CHECK OPTION If a view has a WITH CHECK OPTION clause, all rows inserted or updated through the view must satisfy the view's selection criteria

11 11 Preventing Insertions that are not seen through the View CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50 WITH CHECK OPTION; INSERT INTO OldSailors(sid,sname,age,rating) VALUES(12, ‘ Joe ’,51,10); INSERT INTO OldSailors(sid,sname,age,rating) VALUES(12, ‘ Mary ’,49,10); Not inserted!

12 12 Inserting Not Allowed CREATE VIEW SailorsInfo as SELECT sname, rating FROM Sailors WHERE age>50; INSERT INTO SailorsInfo VALUES( ‘ Joe ’,10); Illegal! Why?

13 13 Updating Allowed CREATE VIEW SailorsInfo as SELECT sname, rating, age FROM Sailors WHERE age>50; UPDATE SailorsInfo SET rating = 6 WHERE sname = ‘ Joe ’ ; UPDATE Sailors SET rating = 6 WHERE sname = ‘ Joe ’ and age>50; Oracle only changes the rating of Joes who are older than 50. Implemented by adding WHERE condition of view to WHERE condition of Update

14 14 Updating Allowed CREATE VIEW SailorsInfo2 as SELECT sname, rating, age FROM Sailors WHERE age>50; UPDATE SailorsInfo2 SET age = age - 1; How is it implemented? Will cause tuples to "disappear from the view" Can prevent this "WITH CHECK OPTION" UPDATE Sailors SET age = age - 1 WHERE age>50;

15 15 Updating Not Allowed CREATE VIEW SailorsInfo2 as SELECT sname, rating, age FROM Sailors WHERE age>50 WITH CHECK OPTION; UPDATE SailorsInfo2 SET age = age - 1;

16 16 Updating Not Allowed CREATE VIEW SailorsInfo3 as SELECT sname, rating + age as ra FROM Sailors WHERE age>50; UPDATE SailorsInfo3 SET ra = 7 WHERE sname = ‘ Joe ’ ; Illegal! Why? UPDATE SailorsInfo3 SET sname = ‘ Joe ’ WHERE ra = 7;

17 17 Deleting Allowed CREATE VIEW SailorsInfo3 as SELECT sname, rating + age as ra FROM Sailors WHERE age>50; DELETE FROM SailorsInfo3 WHERE sname = ‘ Joe ’ and ra = 56; Oracle only deletes Joes visible through the view. How do you think that this is implement by Oracle? DELETE FROM Sailors WHERE sname = ‘ Joe ’ and rating + age = 56 and age > 50;

18 18 Examples (1) UPDATE OldSailors SET rating = 10; CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; Update rating of sailors older than 50

19 19 Examples (2) UPDATE OldSailors SET age = age +1 WHERE age <= 50; CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; NOTHING!

20 20 Examples(3) DELETE FROM OldSailors; CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; DELETE FROM Sailors WHERE age > 50 Implementation Remove from Sailors the sailors that are older than 50

21 21 Inserting/Updating/Deleting Not Allowed CREATE VIEW OldSailors as SELECT sname FROM Sailors GROUP BY sname HAVING MIN(age) > 50; DELETE FROM OldSailors; INSERT INTO OldSailors VALUES( ‘ Joe ’ );

22 22 Inserting/Updating/Deleting Not Allowed CREATE VIEW OldSailors as SELECT distinct sname, age FROM Sailors WHERE age > 50; DELETE FROM OldSailors; INSERT INTO OldSailors VALUES( ‘ Joe ’,55); UPDATE OldSailors SET age = age +1;

23 23 Materialized Views

24 24 What and Why? What: A materialized view is a view that actually exists as a table Why: This can be more efficient than re- computing the view’s query each time it is accessed Problem: How is the materialized view kept up to date when the underlying tables are changed? Note: We will just see the ideas here and not the syntax

25 25 Simple vs. Complex A Simple Materialized View: 1.Selects rows from only 1 table 2.Does not perform set operations, joins or group bys Otherwise, it is a Complex Materialized View.

26 26 Options for Materialized Views Refresh mode –Complete: Recompute the query –Fast (only possible for Simple Materialized Views): add minimal changes (use logs) When is refresh performed? –On demand: When refresh is specifically asked for –On commit: When underlying table has changed

27 27 Query Rewrite We can use a materialized view in a query, similarly to a table and to a regular view The Query Rewrite Option specifies that Oracle can automatically substitute a materialized view in a query, instead of a table mentioned in the query

28 28 Query Rewrite Materialized view defined by the query Given the query: Oracle could decide to evaluate instead SELECT age FROM Sailors WHERE rating 51 SELECT age FROM InterestingSailors WHERE age > 51; SELECT age FROM Sailors WHERE rating < 10

29 29 Null Values

30 30 Null Values in Expressions (1) The result of an arithmetic expression, over something that is null -> is null (e.g., null*10 = null) Three-valued logic: true, false, unknown Nulls in logical expressions: –null AND true -> unknown –null AND false -> false –null OR true -> true –null OR false -> unknown –NOT (null) -> unknown –x { =, <>,, =} null -> unknown

31 31 Null Values in Expressions (2) Tuples only pass the WHERE/HAVING condition if the WHERE/HAVING evaluate to true (not to false or unknown) Null verification: –IS NULL (not = NULL) –IS NOT NULL (not <> NULL)

32 32 Examples If x is null –x = 3 -> unknown –null = 3 -> unknown –x = x -> unknown –null = null -> unknown –x * 0 -> = null –null * 0 -> = null

33 33 Nulls in Aggregation Functions count(A): counts non-null As. Returns 0 if all As are null sum(A), avg(A), min(A), max(A) –ignore null values of A –if A only contains null value, the result is null count(*): counts all rows (even rows that are all null values)

34 34 Distinct and Group By Rows are considered identical if they have all the same non-null values and both have null values in the same columns Distinct removes duplicates of such rows Such rows form a single group when using GROUP BY

35 35 Example SELECT distinct * FROM R BC 1 1 34 35 A BC 1 34 35 Result:

36 36 Example SELECT count(*), count(c), min(c), sum(c) FROM (SELECT c FROM A WHERE c IS NULL or c <> NULL GROUP BY c) BC 1 2 34 35 A Result: 1 0 null null

37 37 Not Loosing information (1) SELECT S.sname, R.bid FROM Sailors S, Reserves R WHERE S.sid = R.sid(+) We will obtain also name of sailors that have not reserved any boat with a NULL bid

38 38 Not Loosing information (2) SELECT S.sname, R.bid FROM Sailors S, Reserves R WHERE S.sid = R.sid(+) UNION SELECT S.sname, R.bid FROM Sailors S, Reserves R WHERE R.sid = S.sid(+)

39 39 Design Theory

40 40 Algorithm for Computing a Key R = ABCDE F = { BD -> C, CE -> B, A -> D, BC -> E }

41 41 Naïve Algorithm Compute Key(R, F) for i=1 to |R| for each subset X of R with i attributes if closure(X,F)=R Return X

42 42 Complexity |R| = n Closure: O(|F|) n!/(n-i)!i! possibilities for choosing a subset X of R with i attributes Exponential!

43 43 Efficient Algorithm Compute Key(R={A1,…,An}, F) Set K := R for i=1..n do if F  = K-{Ai}  Ai K := K \{Ai} Return K


Download ppt "1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that."

Similar presentations


Ads by Google