Views, Indexes, Authorization Virtual and Materialized Views Speeding Accesses to Data Grant/Revoke Priviledges 1 Views External Schema (Views) Conceptual Schema Physical Schema 2 Views A view is a relation defined in terms of stored tables (called base tables ) and other views. Two kinds: 1. Virtual = not stored in the database; just a query for constructing the relation. 2. Materialized = actually constructed and stored. 3 1
Declaring Views Declare by: CREATE [MATERIALIZED] VIEW <name> AS <query>; Default is virtual. 4 Example: View Definition Define CanDrink(drinker, beer) as a view containing the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer: 5 Recall Our Running Example All our SQL queries will be based on the following database schema. o Underline indicates key attributes. Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) 6 2
Example: View Definition Define CanDrink(drinker, beer) as a view containing the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer: CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; 7 Example: Accessing a View Query a view as if it were a base table. o Also: a limited ability to modify views if it makes sense as a modification of one underlying base table. Example query: SELECT beer FROM CanDrink WHERE drinker = Sally ; 8 Triggers on Views Generally, it is impossible to modify a virtual view, because it doesn t exist. But an INSTEAD OF trigger lets us interpret view modifications in a way that makes sense. Example: View Synergy has (drinker, beer, bar) triples such that the bar serves the beer, the drinker frequents the bar and likes the beer. 9 3
Example: The View CREATE VIEW Synergy AS Pick one copy of each attribute SELECT Likes.drinker, Likes.beer, Sells.bar FROM Likes, Sells, Frequents WHERE Likes.drinker = Frequents.drinker AND Likes.beer = Sells.beer AND Sells.bar = Frequents.bar; Natural join of Likes, Sells, and Frequents 10 Interpreting a View Insertion We cannot insert into Synergy --- it is a virtual view. But we can use an INSTEAD OF trigger to turn a (drinker, beer, bar) triple into three insertions of projected pairs, one for each of Likes, Sells, and Frequents. o Sells.price will have to be NULL. 11 The Trigger CREATE TRIGGER ViewTrig INSTEAD OF INSERT ON Synergy REFERENCING NEW ROW AS n FOR EACH ROW BEGIN INSERT INTO LIKES VALUES(n.drinker, n.beer); INSERT INTO SELLS(bar, beer) VALUES(n.bar, n.beer); INSERT INTO FREQUENTS VALUES(n.drinker, n.bar); END; 12 4
Materialized Views Problem: each time a base table changes, the materialized view may change. o Cannot afford to recompute the view with each change. Solution: Periodic reconstruction of the materialized view, which is otherwise out of date. 13 Example: A Data Warehouse Wal-Mart stores every sale at every store in a database. Overnight, the sales for the day are used to update a data warehouse = materialized views of the sales. The warehouse is used by analysts to predict trends and move goods to where they are selling best. 14 Indexes Index = data structure used to speed access to tuples of a relation, given values of one or more attributes. Could be a hash table, but in a DBMS it is always a balanced search tree with giant nodes (a full disk page) called a B-tree. 15 5
Declaring Indexes No standard! Typical syntax: CREATE INDEX BeerInd ON Beers(manf); CREATE INDEX SellInd ON Sells(bar, beer); 16 Using Indexes Given a value v, the index takes us to only those tuples that have v in the attribute(s) of the index. Example: use BeerInd and SellInd to find the prices of beers manufactured by Pete s and sold by Joe. (next slide) 17 Using Indexes --- (2) SELECT price FROM Beers, Sells WHERE manf = Pete s AND Beers.name = Sells.beer AND bar = Joe s Bar ; 1. Use BeerInd to get all the beers made by Pete s. 2. Then use SellInd to get prices of those beers, with bar = Joe s Bar 18 6
Database Tuning A major problem in making a database run fast is deciding which indexes to create. Pro: An index speeds up queries that can use it. Con: An index slows down all modifications on its relation because the index must be modified too. 19 Example: Tuning Suppose the only things we did with our beers database was: 1. Insert new facts into a relation (10%). 2. Find the price of a given beer at a given bar (90%). Then SellInd on Sells(bar, beer) would be wonderful, but BeerInd on Beers(manf) would be harmful. DBMS is not required to use an index even if you create it 20 Tuning Advisors A major research thrust. o Because hand tuning is so hard. An advisor gets a query load, e.g.: 1. Choose random queries from the history of queries run on the database, or 2. Designer provides a sample workload. 21 7
Tuning Advisors --- (2) The advisor generates candidate indexes and evaluates each on the workload. o Feed each sample query to the query optimizer, which assumes only this one index is available. o Measure the improvement/degradation in the average running time of the queries. 22 SQL Authorization Privileges Grant and Revoke Grant Diagrams 23 Authorization A file system identifies certain privileges on the objects (files) it manages. o Typically read, write, execute. A file system identifies certain participants to whom privileges may be granted. o Typically the owner, a group, all users. 24 8
Privileges (1) SQL identifies a more detailed set of privileges on objects (relations) than the typical file system. Nine privileges in all, some of which can be restricted to one column of one relation. 25 Privileges (2) Some important privileges on a relation: 1. SELECT = right to query the relation. 2. INSERT = right to insert tuples. May apply to only one attribute. 3. DELETE = right to delete tuples. 4. UPDATE = right to update tuples. May apply to only one attribute. 26 Example: Privileges For the statement below: INSERT INTO Beers(name) SELECT beer FROM Sells WHERE NOT EXISTS (SELECT * FROM Beers WHERE name = beer); beers that do not appear in Beers. We add them to Beers with a NULL manufacturer. We require privileges SELECT on Sells and Beers, and INSERT on Beers or Beers.name. 27 9
Database Objects The objects on which privileges exist include stored tables and views. Other privileges are the right to create objects of a type, e.g., triggers. Views form an important tool for access control. 28 Example: Views as Access Control We might not want to give the SELECT privilege on Emps(name, addr, salary). But it is safer to give SELECT on: CREATE VIEW SafeEmps AS SELECT name, addr FROM Emps; Queries on SafeEmps do not require SELECT on Emps, just on SafeEmps. 29 Authorization ID s A user is referred to by authorization ID, typically their login name. There is an authorization ID PUBLIC. o Granting a privilege to PUBLIC makes it available to any authorization ID. 30 10
Granting Privileges You have all possible privileges on the objects, such as relations, that you create. You may grant privileges to other users (authorization ID s), including PUBLIC. You may also grant privileges WITH GRANT OPTION, which lets the grantee also grant this privilege. 31 The GRANT Statement To grant privileges, say: GRANT <list of privileges> ON <relation or other object> TO <list of authorization ID s>; If you want the recipient(s) to be able to pass the privilege(s) to others add: WITH GRANT OPTION 32 Example: GRANT Suppose you are the owner of Sells. You may say: GRANT SELECT, UPDATE(price) ON Sells TO sally; Now Sally has the right to issue any query on Sells and can update the price component only. 33 11
Example: Grant Option Suppose we also grant: GRANT UPDATE ON Sells TO sally WITH GRANT OPTION; Now, Sally not only can update any attribute of Sells, but can grant to others the privilege UPDATE ON Sells. o Also, she can grant more specific privileges like UPDATE(price)ON Sells. 34 Revoking Privileges REVOKE <list of privileges> ON <relation or other object> FROM <list of authorization ID s>; Your grant of these privileges can no longer be used by these users to justify their use of the privilege. o But they may still have the privilege because they obtained it independently from elsewhere. 35 REVOKE Options We must append to the REVOKE statement either: 1. CASCADE. Now, any grants made by a revokee are also not in force, no matter how far the privilege was passed. 2. RESTRICT. If the privilege has been passed to others, the REVOKE fails as a warning that something else must be done to chase the privilege down. 36 12
Example: Grant Diagram AP** A: GRANT P TO B WITH GRANT OPTION BP* CP* B: GRANT P TO C WITH GRANT OPTION A owns the object on which P is a privilege CP A: GRANT P TO C 37 Grant Diagrams Nodes = user/privilege/grant option?/is owner? o UPDATE ON R, UPDATE(a) on R, and UPDATE(b) ON R live in different nodes. o SELECT ON R and SELECT ON R WITH GRANT OPTION live in different nodes. Edge X ->Y means that node X was used to grant Y. 38 Notation for Nodes Use AP for the node representing authorization ID A having privilege P. o P * = privilege P with grant option. o P ** = the source of the privilege P. I.e., A is the owner of the object on which P is a privilege. Note ** implies grant option. 39 13
Manipulating Edges (1) When A grants P to B, We draw an edge from AP * or AP ** to BP. o Or to BP * if the grant is with grant option. If A grants a subprivilege Q of P [say UPDATE(a) on R when P is UPDATE ON R] then the edge goes to BQ or BQ *, instead. 40 Manipulating Edges (2) Fundamental rule: User C has privilege Q as long as there is a path from XP ** to CQ, CQ *, or CQ **, and P is a superprivilege of Q. o Remember that P could be Q, and o X could be C. 41 Manipulating Edges (3) If A revokes P from B with the CASCADE option, delete the edge from AP to BP. But if A uses RESTRICT instead, and there is an edge from BP to anywhere, then reject the revocation and make no change to the graph. 42 14
Manipulating Edges (4) Having revised the edges, we must check that each node has a path from some ** node, representing ownership. Any node with no such path represents a revoked privilege and is deleted from the diagram. 43 Example: Grant Diagram AP** A: GRANT P TO B WITH GRANT OPTION BP* CP* B: GRANT P TO C WITH GRANT OPTION A owns the object on which P is a privilege CP A: GRANT P TO C 44 AP** BP* CP* CP Example: Grant Diagram A executes REVOKE P FROM B CASCADE; AP** BP* CP* Even had C passed P to B, both nodes are still cut off. Not only does B lose P*, but C loses P*. Delete BP* and CP*. CP However, C still has P without grant option because of the direct grant. 45 15