2Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop ViewsOuter / Self JoinNested QueriesUseful Function (Decode, Case, Rownum)
3Database Indexes Similar to an index in a book Table with list of sorted data values and corresponding physical locationUsed to speed searchesPrimary key indexed automaticallyUnlimited number allowed, but more indexes means more processing time for action queries (insert, update, delete)
4Creating an Index Create index after table data is loaded CREATE INDEX index_name ON tablename (index_fieldname);Convention for naming index: tablename_fieldname.
5Composite Index Contains multiple (up to 16) sorted columns Used for queries with multiple search conditionsCREATE INDEX index_name ON tablename(index_fieldname1, index_fieldname2, …);
6Viewing Index Information Use data dictionary view USER_INDEXES
7Dropping an IndexIf an index is no longer needed or does not improve performance, delete itDROP INDEX index_name;
8Use an Index WhenTable contains a large number of records (a rule of thumb is that a large table contains over 100,000 records)The field contains a wide range of valuesThe field contains a large number of NULL valuesApplication queries frequently use the field in a search condition or join conditionMost queries retrieve less than 2% to 4% of the table rows
9Do Not Use an Index WhenThe table does not contain a large number of recordsApplications do not use the proposed index field in a query search conditionMost queries retrieve more than 2% to 4% of the table recordsApplications frequently insert or modify table data
10Database Views Logical table based on a query Does not physically exist in the databasePresents data in a different format from underlying tablesUses:SecuritySimplifying complex queries
11Database Views Creating a view: CREATE VIEW view_name AS SQL_command; Views can be queried just like tables:SELECT *FROM view_name;
12Simple ViewsBased on SQL query that retrieves data from only one table (limit data for different user)View can support all table DML operations:INSERTUPDATEDELETE
13Complex Views Based on query that retrieves data from multiple tables Can only be used to support SELECT operationsNo DML operations supportedFor reporting purposes. (Sales Report)Can create index on view.
14Join QueriesRetrieve data from multiple tables by joining tables using foreign key referencesJoin query types:OuterSelfInequality
15Inner Join with Group by Find the total inventory amount on hand for each itemDifferent price
16Outer JoinsLimitation of inner joins: some records may be omitted if corresponding records don’t exist in one of the tablesExample: retrieve records for all students, along with their corresponding ENROLLMENT information
17Outer JoinsStudent 105 (Michael Connoly) does not have any ENROLLMENT records
21A B A.id = B.id A B (+)A.id = B.id A B A.id = B.id(+) Note: Orange is the area that is returnedABA.id = B.idAB(+)A.id = B.idABA.id = B.id(+)
22Self JoinsUsed to join a table to itself when the table has a hierarchical relationship
23Self JoinsTo create a self-join, you need to create a table alias, which gives an alternate name to the table so you can create a join conditionSyntax to create table alias in FROM clause:FROM table1 alias1, table2 alias2
28Nested Queries Created when a subquery is nested within a main query Main query: first query listed in SELECT commandSubquery: retrieves one or more values that specify the main query’s search condition
29Nested Query Where Subquery Returns a Single Value Syntax:SELECT column1, column2, …FROM table1, table2, …WHERE join conditionsAND search_column1 =(SELECT column1FROM table1, table2, …WHERE search andjoin conditions)Subquerythat returnsone valueNote: subquery is just another sql command contained within a parenthesis
30Example Nested Query Where Subquery Returns a Single Value Find all the student that goes to Amanda classFind the class that Amanda goes to.
32Example Nested Query Where Subquery Returns Multiple Values Find all the students that enrolled in c_sec_id in (1010, 1011)Find all the course that Amanda enrolled in. which returnedc_sec_id: 1010, 1011
33Example of nested queries (cont…) select c_sec_id from student s, enrollment e, course_section c where s.s_id = e.s_id and s_last = 'Mobley‘ and s_first = 'Amanda‘ and c_sec_id in (1000, 1002, 1007, 1010);Find the c_sec_id that use bldg_code=‘CR’ which is 1000, 1002, 1007, 1010
34Remember previous example of Inner Join with Group by Find the total inventory amount on hand for each itemDifferent price
35Creating Table Alias from select statement Add an extra column for the percentage of inventory on hand
36Using Set Operators in Queries Performs set operations on outputs of two unrelated queriesBoth queries must have:same number of display fieldscorresponding display fields must have same data type
37Query Set Operators UNION: combines results, suppresses duplicate rows UNION ALL: combines results, displays duplicatesINTERSECT: finds matching rowsMINUS: returns the difference between returned record sets
38Decode FunctionFacilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statementSyntaxDecode(column/expression, search1, result1[, search2, result2, …, ][, default ] );
39The Decode FunctionThe DECODE function decodes an expression in a way similar to the IF-THEN-ELSE logic used in various languages. The DECODE function decodes expression after comparing it to each search value. If the expression is the same as search, result is returned.Note: If the default value is omitted, a null value is returned where a search value does not match any of the result value.