Download presentation
Presentation is loading. Please wait.
Published byMyron Bryan Modified over 9 years ago
1
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009
2
8/30/2015Jinze Liu @ University of Kentucky2 Topics Review : View Transaction
3
8/30/2015Jinze Liu @ University of Kentucky3 Views A view is like a “virtual” table Defined by a query, which describes how to compute the view contents on the fly DBMS stores the view definition query instead of view contents Can be used in queries just like a regular table
4
8/30/2015Jinze Liu @ University of Kentucky4 Creating and dropping views Example: CS405roster CREATE VIEW CS405Roster AS SELECT SID, name, age, GPA FROM Student WHERE SID IN (SELECT SID FROM Enroll WHERE CID = ’CS405’); To drop a view DROP VIEW view_name ; Called “base tables”
5
8/30/2015Jinze Liu @ University of Kentucky5 Using views in queries Example: find the average GPA of CS405 students SELECT AVG(GPA) FROM CS405Roster; To process the query, replace the reference to the view by its definition SELECT AVG(GPA) FROM (SELECT SID, name, age, GPA FROM Student WHERE SID IN (SELECT SID FROM Enroll WHERE CID = ’CS405’));
6
8/30/2015Jinze Liu @ University of Kentucky6 Why use views? To hide data from users To hide complexity from users Logical data independence If applications deal with views, we can change the underlying schema without affecting applications Recall physical data independence: change the physical organization of data without affecting applications To provide a uniform interface for different implementations or sources Real database applications use tons of views
7
8/30/2015Jinze Liu @ University of Kentucky7 Modifying views Does not seem to make sense since views are virtual But does make sense if that is how users see the database Goal: modify the base tables such that the modification would appear to have been accomplished on the view Be careful! There may be one way to modify There may be many ways to modify There may be no way to modify
8
8/30/2015Jinze Liu @ University of Kentucky8 A simple case CREATE VIEW StudentGPA AS SELECT SID, GPA FROM Student; DELETE FROM StudentGPA WHERE SID = 123; translates to: DELETE FROM Student WHERE SID = 123;
9
8/30/2015Jinze Liu @ University of Kentucky9 An impossible case CREATE VIEW HighGPAStudent AS SELECT SID, GPA FROM Student WHERE GPA > 3.7; INSERT INTO HighGPAStudent VALUES(987, 2.5); No matter what you do on Student, the inserted row will not be in HighGPAStudent
10
8/30/2015Jinze Liu @ University of Kentucky10 A case with too many possibilities CREATE VIEW AverageGPA(GPA) AS SELECT AVG(GPA) FROM Student; Note that you can rename columns in view definition UPDATE AverageGPA SET GPA = 2.5; Set everybody’s GPA to 2.5? Adjust everybody’s GPA by the same amount? Just lower Lisa’s GPA?
11
8/30/2015Jinze Liu @ University of Kentucky11 SQL92 updateable views More or less just single-table selection queries No join No aggregation No subqueries Arguably somewhat restrictive Still might get it wrong in some cases See the slide titled “An impossible case” Adding WITH CHECK OPTION to the end of the view definition will make DBMS reject such modifications
12
8/30/2015Jinze Liu @ University of Kentucky12 Today’s Topic Review : View Transaction Recursion
13
8/30/2015Jinze Liu @ University of Kentucky13 Transactions A program may carry out many operations on the data retrieved from the database However, the DBMS is only concerned about what data is read/written from/to the database. database - a fixed set of relations (A, B, C, …) transaction - a sequence of read and write operations (read(A), write(B), …) DBMS’s abstract view of a user program
14
Correctness criteria: The ACID properties A A tomicity: All actions in the Xact happen, or none happen. C C onsistency: If each Xact is consistent, and the DB starts consistent, it ends up consistent. I I solation: Execution of one Xact is isolated from that of other Xacts. D D urability: If a Xact commits, its effects persist. 8/30/201514Jinze Liu @ University of Kentucky
15
8/30/2015Jinze Liu @ University of Kentucky15 An Example about SQL Transaction Consider two transactions (Xacts): T1:BEGIN A=A+100, B=B-100 END T2:BEGIN A=1.06*A, B=1.06*B END 1st xact transfers $100 from B’s account to A’s 2nd credits both accounts with 6% interest. Assume at first A and B each have $1000. What are the legal outcomes of running T1 and T2??? $1100 *1.06 = $1166 There is no guarantee that T1 will execute before T2 or vice-versa, if both are submitted together. But, the net effect must be equivalent to these two transactions running serially in some order.
16
8/30/2015Jinze Liu @ University of Kentucky16 Example (Contd.) Legal outcomes: A=1166,B=954 or A=1160,B=960 Consider a possible interleaved schedule: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B v This is OK (same as T1;T2). But what about: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B Result: A=1166, B=960; A+B = 2126, bank loses $6 The DBMS’s view of the second schedule: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)
17
8/30/2015Jinze Liu @ University of Kentucky17 SQL transactions Syntax in SQL: BEGIN COMMIT [ROLLBACK] A transaction is automatically started when a user executes an SQL statement (begin is optional) Subsequent statements in the same session are executed as part of this transaction Statements see changes made by earlier ones in the same transaction Statements in other concurrently running transactions do not see these changes COMMIT command commits the transaction (flushing the update to disk) ROLLBACK command aborts the transaction (all effects are undone)
18
8/30/2015Jinze Liu @ University of Kentucky18 Atomicity Partial effects of a transaction must be undone when User explicitly aborts the transaction using ROLLBACK E.g., application asks for user confirmation in the last step and issues COMMIT or ROLLBACK depending on the response The DBMS crashes before a transaction commits Partial effects of a modification statement must be undone when any constraint is violated However, only this statement is rolled back; the transaction continues How is atomicity achieved? Logging (to support undo)
19
8/30/2015Jinze Liu @ University of Kentucky19 Isolation Transactions must appear to be executed in a serial schedule (with no interleaving operations) For performance, DBMS executes transactions using a serializable schedule In this schedule, only those operations that can be interleaved are executed concurrently Those that can not be interleaved are in a serialized way The schedule guarantees to produce the same effects as a serial schedule
20
8/30/2015Jinze Liu @ University of Kentucky20 SQL isolation levels Strongest isolation level: SERIALIZABLE Complete isolation Usually use as default Weaker isolation levels: REPEATABLE READ, READ COMMITTED, READ UNCOMMITTED Increase performance by eliminating overhead and allowing higher degrees of concurrency Trade-off: sometimes you get the “wrong” answer
21
8/30/2015Jinze Liu @ University of Kentucky21 READ UNCOMMITTED Can read “dirty” data A data item is dirty if it is written by an uncommitted transaction Problem: What if the transaction that wrote the dirty data eventually aborts? Example: wrong average -- T1:-- T2: UPDATE Student SET GPA = 3.0 WHERE SID = 142;SELECT AVG(GPA) FROM Student; ROLLBACK; COMMIT;
22
8/30/2015Jinze Liu @ University of Kentucky22 READ COMMITTED All reads see a snapshot of the database (including all committed transactions) right before the beginning of the query No dirty reads, but non-repeatable reads possible Reading the same data item twice can produce different results Example: different averages -- T1:-- T2: SELECT AVG(GPA) FROM Student; UPDATE Student SET GPA = 3.0 WHERE SID = 142; COMMIT; SELECT AVG(GPA) FROM Student; COMMIT;
23
8/30/2015Jinze Liu @ University of Kentucky23 REPEATABLE READ Reads are repeatable, but may see phantoms Do not allow the modification of existing values New rows may be inserted in the mean time Example: different average (still!) -- T1:-- T2: SELECT AVG(GPA) FROM Student; INSERT INTO Student VALUES(789, ‘Nelson’, 10, 1.0); COMMIT; SELECT AVG(GPA) FROM Student; COMMIT;
24
8/30/2015Jinze Liu @ University of Kentucky24 SERIALIZABLE READ Reads see the snapshot of the database right before the beginning of the transaction Example: the same average -- T1:-- T2: SELECT AVG(GPA) FROM Student; INSERT INTO Student VALUES(789, ‘Nelson’, 10, 1.0); COMMIT; SELECT AVG(GPA) FROM Student; COMMIT;
25
8/30/2015Jinze Liu @ University of Kentucky25 Summary of SQL isolation levels Syntax: At the beginning of a transaction, SET TRANSACTION ISOLATION LEVEL isolation_level [ READ ONLY | READ WRITE ] ; READ UNCOMMITTED can only be READ ONLY Isolation level/anomalyDirty readsNon-repeatable readsPhantoms READ UNCOMMITTED Possible READ COMMITTED ImpossiblePossible REPEATABLE READ Impossible Possible SERIALIZABLE Impossible
26
8/30/2015Jinze Liu @ University of Kentucky26 Summary of SQL features covered so far Query Modification Constraints Triggers Views Transaction Next: Database programming
27
8/30/2015Jinze Liu @ University of Kentucky27 Indexes for Performance Tuning An index is an auxiliary persistent data structure Search tree (e.g. B-tree, R-tree) Lookup table (e.g., hash table) An index on R.A can speed up accesses of the form R.A = value (lookup) R.A > value (range query) An index on ( R.A 1, …, R.A n ) can speed up R.A 1 = value 1, …, R.A n = value n (R.A 1, … R.A n ) > (value 1, …, value n ) More on indexes in the second half of this course!
28
8/30/2015Jinze Liu @ University of Kentucky28 Examples of Hash Index SELECT * FROM Student WHERE name = ‘Kevin’ Without an index on Student.name: must scan the entire table if we store Student as a flat file of unordered rows With index: go “directly” to rows with name = ’Kevin’ sidnameagegpa 1234John213.5 1123Mary193.8 1011Bob222.6 1204Susan223.4 1306Kevin182.9 Kevin Susan Bob Mary John name A hash table
29
8/30/2015Jinze Liu @ University of Kentucky29 Creating and dropping indexes CREATE [ UNIQUE ] INDEX index_name ON table_name ( column_name 1, …, column_name n ); With UNIQUE, the DBMS will also enforce that {column_name 1, …, column_name n } is a key of table_name e.g CREATE INDEX name_index ON STUDENT (SName); DROP INDEX index_name ; Typically, the DBMS will automatically create indexes for PRIMARY KEY and UNIQUE constraint declarations
30
8/30/2015Jinze Liu @ University of Kentucky30 Choosing indexes to create More indexes = better performance? Indexes take space Indexes need to be maintained when data is updated Indexes have one more level of indirection Optimal index selection depends on both query and update workload and the size of tables
31
8/30/2015Jinze Liu @ University of Kentucky31 Recursion in SQL SQL2 had no recursion You can find Bart’s parents, grandparents, great grandparents, etc. SELECT p1.parent AS grandparent FROM Parent p1, Parent p2 WHERE p1.child = p2.parent AND p2.child = ’Bart’; But you cannot find all his ancestors with a single query SQL3 introduces recursion WITH clause Implemented in DB2 (called common table expressions) Unfortunately, pgSQL does not support it.
32
8/30/2015Jinze Liu @ University of Kentucky32 Ancestor query in SQL3 WITH Ancestor(anc, desc) AS ((SELECT parent, child FROM Parent) UNION (SELECT a1.anc, a2.desc FROM Ancestor a1, Ancestor a2 WHERE a1.desc = a2.anc)) SELECT anc FROM Ancestor WHERE desc = ’Bart’; Query using the relation defined in WITH clause Define a a relation recursively base case recursion step How do we compute such a recursive query?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.