Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored.

Similar presentations


Presentation on theme: "1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored."— Presentation transcript:

1 1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored Procedures u Part 6:Performance Issues u Part 7:Database Integrity

2 2 Part 1: Views u Views are virtual tables which are created from the result set of a SELECT statement u Views are not separate copies of the data in the tables from which they are derived

3 3 Views -Continued u A View is essentially stored SQL Select statement u A View is named like a table when created (e.g. books_and_publishers)

4 4 Create VIEW Syntax u CREATE VIEW view_name [column_name [,column_name] … ] AS select_statement;

5 5 Example - Create View u CREATE VIEW books_and_publishers AS (SELECT title, pub_name FROM titles t, publishers p WHERE t.pub_id = p.pub_id);

6 6 What the User Sees u SELECT title, pub_name FROM books_and_publishers; u Statement first executes the SELECT associated with the View u User Select is against the View results (which are transparent to the User)

7 7 Why Views? u Saves the user from complex joins u Stops the user from seeing secure data

8 8 Deleting Views from the Database u DROP VIEW books_and_publishers; u Warning: If you DROP a table that the VIEW depends upon, the VIEW will no longer work

9 9 Naming View Columns u Assign alias names to View columns is optional… in most cases. u By default, VIEW will use the same columns names returned from the SELECT u Exceptions: If there is any ambiguity from a Join operation or View column is arithmetically derived u If you rename any columns, you must rename all of them!

10 10 Exercise u Create a view that displays the authors names, social security number and the average position they appear on the books they've written.

11 11 Solution u CREATE VIEW authors_position (ssn, lastn, firstn, average_order) AS SELECT a.au_id, au_lname, au_fname, AVG(au_ord) FROM authors a, titleauthors ta WHERE a.au_id = ta.au_id GROUP BY a.au_id, au_lname, au_fname;

12 12 Part 2: Security Issues u Two basic SQL security statements… –GRANT gives a user access rights to a table or columns on a table –REVOKE removes user access right to a table or columns on a table

13 13 GRANT/REVOKE Syntax u GRANT {ALL | privilege_list } ON table or view name [column_list] TO {PUBLIC | user_list} u REVOKE {ALL | privilege_list } ON table or view name [column_list] FROM {PUBLIC | user_list}

14 14 Examples u GRANT INSERT, UPDATE ON titles TO perrys, smithj; u REVOKE UPDATE ON titles(advance, price) FROM jonesk;

15 15 Views to restrict access u Payroll table: name, ssn, dept, salary u CREATE VIEW payroll_generalaccess (name, dept) AS SELECT name, dept FROM payroll;

16 16 Vendors Offer Security Extensions u ORACLE has GROUPs –allows you group user_ids under a group –allows you to assign access rights to that group

17 17 Exercise u Give the user ids perrys and landeyt the ability to make changes to existing data on the authors table (except the au_id column) and to add new rows of data but not delete them. Of course, they should be able to see the data.

18 18 Part 3: Transaction Management u A transaction is a logical unit of work u Transaction management means ensuring that a set of SQL statements is treated as a unit u Transaction management guarantees the either all the operations are completed or none of them are

19 19 Savings Changes to the Database u COMMIT statement saves all the changes made to the database since the last COMMIT. u ROLLBACK statement un-does all changes made to the database since the last COMMIT or ROLLBACK.

20 20 Example u Banking transaction: –Transfer $100 from Savings to Checking –First reduce the savings balance by $100 –Then increase the checking balance by $100 u What happens if the system crashes after reducing the savings balance, but before increasing the checking balance?

21 21 Status Checking u When SQL is embedded in computer applications, you must check for the success or failure of each SQL statement u You COMMIT changes only after all the SQL statements have completed successfully in a transaction.

22 22 Concurrency Control u How do you prevent simultaneous transactions on the same data from interfering with each other? u Concurrency control means making sure that data is never operated on by another user until a change is completed

23 23 The Lost Update Problem u Steve calls travel agent for a ticket to Tahiti u Agent checks computer: One ticket left u While I think about: Barbara calls her ticket agent and asks for a ticket to Tahiti u Her agent check computer: One ticket left u Steve buys ticket: Agent saves information u Barbara buys ticket: Agent saves information u Steve goes to the airport for his ticket…Who?

24 24 Row Locking is the Solution u When Steve's agent bring up the record a 'write' lock is placed u After, Steve's agent saves the transaction the lock is removed u When Barbara's agent tries to save, warned that the record has changed and must re- retrieve it. u When retrieved again, Barbara will be informed that the ticket has been sold.

25 25 Locking in Oracle u SELECT au_id, city FROM authors WHERE au_id = '144-11-2222' FOR UPDATE OF city; u Places a write lock on the column city for the row with a Primary key of '144-11-2222'

26 26 Part 4: Set Operations u SQL operates on Sets of row (i.e. Relations) u We perform mathematical Set operations on result sets

27 27 Oracle SET operators u UNION –combines all rows returned from each result set with an implicit DISTINCT u INTERSECT –combines only the rows that are in common to each result set u MINUS –Subtracts the rows from the first result set that are found in the second result set

28 28 Example u List all the authors and editors SELECT au_lname, au_fname FROM authors UNION SELECT ed_lname, ed_fname FROM editors;

29 29 Matching Select Lists u To perform SET operations the select lists must match from ALL result sets u They must have the same number of columns and the data types must match

30 30 Part 5: Triggers & Procedures u Triggers –Automatic updates set up to execute when an INSERT, UPDATE, or DELETE is performed against a table u Stored Procedures –Precompiled SQL statements that reside on the database server and may receive arguments and return data. May use procedural language –Generally called from within computer applications or Triggers

31 31 Example u CREATE TRIGGER record_users ON authors FOR UPDATE AS ;

32 32 Part 6: Performance Issues u Indexes u Database de-normalization u Vendor specific optimization tools –e.g. Oracle Analyzer, Hints u Use of Views to force limits on result size

33 33 Part 7: Database Integrity u Domain Constraints Integrity –Set of potential values for a column u Entity Integrity –e.g. No primary keys with NULL status u Referential Integrity –Logical consistency between Primary Keys and Foreign Keys. No Orphans.

34 34 Last Slide - Section 9 u Please complete Assignment 8


Download ppt "1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored."

Similar presentations


Ads by Google