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.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
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 3ed, Online chapter, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Online Chapter Example is the school of mankind,
SQL Constraints and Triggers
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
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Chapter 6 Example is the school of mankind, and they will learn at no.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
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.
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.
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 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.
The Relational Model These slides are based on the slides of your text book.
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.
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: 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.
Spring 2011 ITCS3160: Database Design and Implementation Hands-on Learning.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CS122A: Introduction to Data Management Lecture #11: SQL (4) -- Triggers, Views, Access Control 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.
Query-by-Example (QBE)
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)
Relational Calculus Chapter 4 – Part II.
CS 405G: Introduction to Database Systems
Presentation transcript:

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 is a single table in the database –CREATE 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? (1) Now: Find snames of Sailors who reserved 103 using SRB

4 What are views good for? (2) 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 though 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 InsertNoYes 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 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);  

11 Preventing Insertions that are not seen through the View How can Oracle implement WITH CHECK OPTION?

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"

15 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?

16 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?

17 Some More Examples: What will these commands do? UPDATE OldSailors SET rating = 10; DELETE FROM OldSailors; UPDATE OldSailors SET age = age +1 WHERE age <= 50; CREATE VIEW OldSailors as SELECT * FROM Sailors WHERE age > 50;

18 Materialized Views

19 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

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

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

22 Fast Refresh Consider a materialized view defined by the query: SELECT sname, age FROM Sailors WHERE rating 50;

23 Fast Refresh INSERT INTO Sailors(sid,sname,rating,age) VALUES (12, ’ Joe ’,8,52); How would the materialized view be updated when the following commands are performed? DELETE FROM Sailors WHERE age < 54; UPDATE Sailors SET age = age - 1 WHERE rating < 10;

24 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. Why is this useful?

25 Query Rewrite Suppose the materialized view from before was called InterestingSailors. Given the query: Oracle could decide to evaluate instead SELECT age FROM Sailors WHERE rating 51 SELECT age FROM InterestingSailors WHERE age > 51;

26 Null Values

27 Null Values in Expressions The result of an arithmetic expression, over something that is null -> is null (e.g., null*0, null = null) Nulls in logical expressions: –null AND true -> null –null AND false -> false –null OR true -> true –null OR false -> null –NOT (null) -> null Tuples only pass the WHERE condition if the WHERE evaluates to true (not to false or null)

28 Nulls in Aggregation Functions count(*): counts all rows (even rows that are all null values) 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

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

30 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