Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Agenda for Class 03/07/2006  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access.

Similar presentations


Presentation on theme: "1 Agenda for Class 03/07/2006  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access."— Presentation transcript:

1 1 Agenda for Class 03/07/2006  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access a view.  Uses of a view.  Practice writing SQL without the use of a computer.

2

3 3 What is a SQL View?  A “virtual” table.  A set of SQL statements that creates a result table which can be accessed by other SQL statements.  Similar to a function in another programming language, except that the end-product of the view is a named result table.  A database object.  A view is stored in the database.  A view contains no data of its own.  A view relies on the data in the base tables used to create the view.  A set of stored SQL code.  Stores code; not data.

4 What is the name of the employee who has some time recorded in the TIME table, but has the least amount of time performing work with the word “java” in the description? SELECTemp.empid, emp.name, sum(amount/60) hours_worked FROMemp INNER JOINtime ONemp.empid = time.empid INNER JOINwork ONwork.worktypeid = time.worktypeid WHERElower(description) LIKE 'java%' GROUP BYemp.empid, emp.name HAVINGsum(amount/60) = (SELECT min(sum(amount/60)) FROM time INNER JOIN work ON time.worktypeid = work.worktypeid WHERE lower(description) LIKE 'java%' GROUP BY time.empid, time.worktypeid);

5 CREATE OR REPLACE VIEW view_hours AS SELECTemp.empid, emp.name, time.worktypeid, lower(description) description, nvl(sum(time.amount/60),0) hoursworked FROM emp INNER JOINtime ONemp.empid = time.empid INNER JOIN work ONtime.worktypeid = work.worktypeid GROUP BYemp.empid, emp.name, time.worktypeid, description; Create a View Use the View SELECTview_hours.empid, view_hours.name, hoursworked FROMview_hours WHEREdescription like 'java%' ANDhoursworked = (SELECT min(hoursworked) FROM view_hours WHERE description like 'java%');

6 6 Uses of views  Views are used to:  Provide easier access to data.  Enhance security.  Lessen the visible complexity of the database.  Views are usually created by the DBA for a defined workgroup of people.  Programmers.  Users.  Users in a specific functional area.

7 7 When should views be used?  Use to protect sensitive data.  Example: Can create a view of personnel data that does not include salary data.  Use to break down complicated queries into modular components.  Use for group/aggregate functions.  Use for multiple table joins.

8 8 Rules-of-thumb about views  Avoid extra joins. Avoid creating a view with an underlying table, and then using that view to join the result table from the view with that same table in a query. Try to use the view to replace the table, so that you don’t have to use the table again.  Make views flexible. Create views that can be used by more than one query. Anticipate how the data will be accessed so that one view could be used in more than one situation.  Make views useful. Be sure to include relevant foreign keys in a view.

9 9 Looking at the details of a view in Oracle You can look at all views or just one view. SELECTview_name FROMuser_views; Results from the statement above: VIEW_NAME EMP_TIME TIME_BY_WORK VIEW_HOURS The views above are in the account I used to show the results of the statement. Your names will be different.

10 10 Looking at the code in a view SELECT text FROM user_views WHERE LOWER(view_name) = ‘hours_by_emp' Results from the query above: TEXT ----------------------------------------- SELECT employee.empid, name, nvl(sum(amount/60),0) hoursworked FROM employ

11 11 Looking at all the code in a view  The column “text” is a long data type.  Long data types will truncate at a pre-determined size.  Sometimes you won’t see all your SQL code in the “text” column because of the truncation.  To enlarge the size of display for long data types, do the following: SET LONG 500 (or whatever number of characters in the text of the view that you want to see displayed.)


Download ppt "1 Agenda for Class 03/07/2006  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access."

Similar presentations


Ads by Google