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.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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.
Security and Authorization. Introduction to DB Security Secrecy: Users shouldn’t be able to see things they are not supposed to. –E.g., A student can’t.
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),
Security and Authorization. Introduction to DB Security Secrecy: Users shouldn’t be able to see things they are not supposed to. –E.g., A student can’t.
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.
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.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
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.
Security and Authorization. Introduction to DB Security Secrecy: Users shouldn’t be able to see things they are not supposed to. –E.g., A student can’t.
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.
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.
10/20/20151 Introduction to SQL and the Relational Model ICOS Big Data Summer Camp June 2, 2015 Mike Anderson (slides from Mike Cafarella)
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.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
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).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
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.
Spring 2011 ITCS3160: Database Design and Implementation Hands-on Learning.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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.
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.
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.
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Structured Query Language
SQL: The Query Language (Part III)
Presentation transcript:

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 is used as a ‘window’ to underlying tables The view is computed every time that it is referenced Changes can be done in both directions: View Table

3 Advantages and Disadvantages Advantages: –no memory used for views –update of table does not require updating views –gives query processor more choices for optimizing Disadvantages: –must be recomputed every time used –if tables that view uses are dropped, view data is lost

4 Example A View is a query that looks like a table and can be used as a table. CREATE OR REPLACE VIEW Dept20 as SELECT Ename, Job, Sal*12 As AnnualSal FROM Emp Where dept=20;

5 Example 2: Products table CREATE VIEW [ProductsAboveAveragePrice] AS SELECT ProductName,UnitPrice FROM Products WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products) ProductNameQuantityUnitPrice Milk1004 Eggs8710 Bread645

6 What are views good for?(1) Simplifying complex queries 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

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

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

9 Changing Tables through a View

10 Changing a view changes the underlying tables We will specify the limitations on the changes we are allowed to perform on a view based on one table only The guiding principle: We cannot “approach” a value which doesn’t appear in the view (but many changes do not require “approaching”)

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

12 You can insert a value to the view even if it will not appear in the view after insertion. You can update a value appearing in the view so that the result will not appear in the view. This is true unless ‘with check option’ is specified. CREATE VIEW OldSailors as SELECT sid, age FROM Sailors Where age>50 Insert into OldSailors values(1132, 57); CREATE VIEW OldSailors as SELECT sid, age FROM Sailors Where age>50 With check option Insert into OldSailors values(1132, 57); Legal!Illegal!

13 You cannot update a value which doesn’t appear in the view (this requires “approaching”) You cannot insert or update columns which do not appear in the view But if you delete a row, it will delete the values of columns which are not in the view as well (does not require “approaching”)

14 Only Values of Rows seen through the View Only Values of Columns seen through the View Insert No (Yes if CHECK) Yes Update (values after change) No (Yes if CHECK) Yes Update (values before change) Yes DeleteYesNo

15 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

16 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. But she will appear in Sailors!

17 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); Error! OK

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

19 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

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

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

22 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; legal

23 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;

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

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

26 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

27 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 ’ );

28 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;

29 Materialized Views 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 How: We specify how often the materialized view is refreshed and how

30 Null Values "As we know, There are known knowns. There are things we know we know. We also know There are known unknowns. That is to say We know there are some things We do not know.” - Donald Rumsfeld, US Defence Secretary

31 Null Values in Expressions 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

32 Three-Valued Logic Table pqp AND qp OR q True False True NullUnknownTrue FalseTrueFalseTrue False NullFalseUnknown NullTrueUnknownTrue NullFalse Unknown Null Unknown Two-Valued Logic Table pqp AND qp OR q True False True FalseTrueFalseTrue False

33 What will these return? SELECT S.sname FROM Sailors S WHERE S.age = null SELECT S.sname FROM Sailors S WHERE S.age != null

34 ? ?

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

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

37 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)

38 SELECT S.sname, R.bid FROM Sailors S, Reserves R WHERE S.sid = R.sid(+) We want the sailors that have not reserved a boat to appear in the result as well Sailors who have not reserved a boat will have null in the R.bid column