Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Agenda for Class 03/02/2006  Do SQL join “quiet participation” exercise in class.  Learn to simplify queries for complex questions through the use.

Similar presentations


Presentation on theme: "1 Agenda for Class 03/02/2006  Do SQL join “quiet participation” exercise in class.  Learn to simplify queries for complex questions through the use."— Presentation transcript:

1 1 Agenda for Class 03/02/2006  Do SQL join “quiet participation” exercise in class.  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.

2

3 3 Examples of more complex questions  What is the description of the work type with the most time in the time table and how much time was reported for that work type description?  What is the name of the employee who worked some time, but still worked the least amount of total time worked of all employees during the month of December of the previous year?  What is the name of the client for whom we worked the most time during the month of January in the current year?  Which contracts have had more time worked than the estimated time?  Which contracts have more than 25% time worked than estimated time?

4 4 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.

5 Let’s say we frequently look at all the information about time in our database and we always convert the time to hours and we always like to include the name of the employee who worked the time as well as the description of the type of work performed: CREATE OR REPLACE VIEW emp_time AS SELECTNVL(emp.empid,time.empid) empid, emp.name, time.contractid, time.datetime, time.worktypeid, work.description, nvl(amount/60,0) hoursworked FROMemp FULL OUTER JOINtime ONemp.empid = time.empid LEFT OUTER JOINwork ONwork.worktypeid = time.worktypeid; Any derived column (calculated, aggregate and/or with a SQL function) must have an alias, such as hoursworked in the above example.

6 6 How is the view used via SQL?  Views are used just as tables are used in SQL. In this example, the join is predefined, so it is easier to write access queries. SELECT * FROMemp_time ORDER BYempid; SELECT * FROM emp_time WHEREhoursworked = 0; SELECTname FROMemp_time;

7 Can a view be joined with tables? Yes! SELECTemp_time.empid, emp_time.name, emp_time.datetime, emp_time.contractid, client.name FROMemp_time INNER JOINcontract ONcontract.contractid = emp_time.contractid INNER JOINclient ONcontract.clientid = client.clientid ORDER BYemp_time.empid, emp_time.datetime desc; Must have a “shared” column to access the data between tables and view – same as using a foreign key between tables.

8 8 So, what’s the big deal?  Views allow you to break down difficult queries into smaller pieces for easier design, coding and debugging.  Views allow you to create a layer of abstraction between the data structure and the user or programmer allowing greater security.  Programmers do not know the structure of the base tables. Less risk of fraud.  Users can see “pre-joined” tables for their queries.  Users don’t have to understand the complexity of the actual database.  No one sees data that is secure (salary, for example).  Views allow you to access the results of aggregate functions more easily.

9 9 Group Functions and Joins are Complex  Must have all non-group attributes that are in the SELECT list also in the GROUP BY statement.  Difficult to do a group function of a group function. Examples:  The maximum of the sum of hours.  The minimum of a count of products.  Joining multiple tables can yield full or partial cartesian products making it difficult to trouble-shoot the SQL code.

10 Which type of work did we do the most? SELECTwork.worktypeid, work.description, sum(time.amount/60) "Hours Worked" FROMwork INNER JOIN time ONwork.worktypeid = time.worktypeid GROUP BYwork.worktypeid, work.description HAVINGsum(time.amount/60) = max(sum(time.amount/60)); Doesn’t work! SELECTwork.worktypeid, work.description, sum(time.amount/60) "Hours Worked" FROMwork INNER JOIN time ONwork.worktypeid = time.worktypeid GROUP BYwork.worktypeid, work.description HAVINGsum(time.amount/60) = (SELECT max(sum(time.amount/60)) FROM time GROUP BYworktypeid); Works in Oracle, but is conceptually complicated

11 Try it with a view First, create a view. CREATE OR REPLACE VIEW time_by_work AS SELECTwork.worktypeid, work.description, sum(amount/60) hoursworked FROMwork INNER JOIN time ONwork.worktypeid = time.worktypeid GROUP BYwork.worktypeid, work.description; Second, use the view in a query with a sub-query. SELECTdescription, hoursworked FROMtime_by_work WHEREhoursworked = (SELECT MAX(hoursworked) FROM time_by_work);

12 What is the name of the employee who put in some time 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);

13 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%');

14 14 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.

15 15 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.

16 16 How should a view be used?  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.

17 17 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.

18 18 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

19 19 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/02/2006  Do SQL join “quiet participation” exercise in class.  Learn to simplify queries for complex questions through the use."

Similar presentations


Ads by Google