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.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
111 A DMINISTRATIVE Exam Oct 19, :30pm in 3005 (usual room) No class on Oct 21 What topic to do for review? November 18 th away for conference.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 Views and Null values. 2 What does this return? SELECT B.bid, COUNT(*) FROM Boats B, Reserves R WHERE R.bid=B.bid and B.color=‘red’ GROUP BY B.bid For.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Views. 2 Views A view is a "virtual table" defined using a query You can use a view as if it were a table, even though it doesn't contain data The view.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 Rewriting Minus Queries Using Not In SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 Views. 2 What are views good for? (1) Simplifying complex queries: We saw one example. Here is another that allows the user to "pretend" that there.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Moving a Java Program into a Database. 2 Motivation We would like to be able to run a Java program from within Oracle. This will allow us to: –call.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
SQL – Part 2.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Basic SQL Lecture 6 Fall
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Relational Algebra.
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
SQL: Structured Query Language
CENG 351 File Structures and Data Managemnet
Relational Algebra Chapter 4 - part I.
SQL: The Query Language (Part III)
Presentation transcript:

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 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 What are views good for?(2) Find names of Sailors who reserved boat 103 using SRB SELECT sname FROM SRB WHERE bid=‘103’

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 Changing Tables through a View

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 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 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 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 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 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 Inserting Not Allowed CREATE VIEW SailorsInfo as SELECT sname, rating FROM Sailors WHERE age>50; INSERT INTO SailorsInfo VALUES( ‘ Joe ’,10); Illegal! Why?

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 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 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 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 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 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 Examples (2) UPDATE OldSailors SET age = age +1 WHERE age <= 50; CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50; NOTHING!

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 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 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 Materialized Views

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 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 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 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 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 Null Values

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 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 Examples If x is null –x = 3 -> unknown –null = 3 -> unknown –x = x -> unknown –null = null -> unknown –x * 0 -> = null –null * 0 -> = null

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 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 Example SELECT distinct * FROM R BC A BC Result:

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 A Result: 1 0 null null

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 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 Design Theory

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

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 Complexity |R| = n Closure: O(|F|) n!/(n-i)!i! possibilities for choosing a subset X of R with i attributes Exponential!

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