ITBIS373 Database Development: Guide To Oracle 10g
ITBIS373 Database Development: Guide To Oracle 10g
ITBIS373 Database Development: Guide To Oracle 10g
Lecture3c - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Lesson C Objectives
After completing this lesson, you should be able to: Create SQL queries that join multiple tables Create nested SQL queries Combine query results using set operators Create and use database views
Join
Combine data from multiple database tables using foreign key references
Syntax
SELECT column1, column2, FROM table1, table2 WHERE table1.joincolumn = table2.joincolumn AND search_condition(s);
Guide to Oracle 10g 3
Specify name of table that contains column followed by period then column name Specifies table names to be joined and column names on which to join tables
Join condition
Inner Joins
Simplest type of join VALUES in one table equal to values in other table Also called:
Fig 3-43 to retrieve the student Id, student last and first names, advisor ID, and advisor last name.
In SQL queries, you can join any number of tables in SELECT command. When join tables, the name of each table in the query must appear in the FROM clause. This includes tables whose columns are display columns, which are columns that appear in the SELECT clause, and whose columns are search columns, which appear in search condition. The primary key and foreign key columns on which you join the tables are called join columns.
Guide to Oracle 10g
When you join multiple tables, sometimes you must join the tables using an intermediary table whose columns are not display or search columns, but whose columns are join columns that serve to join the two tables. For example, suppose you want to create a query that lists the last names of all faculty members who teach during the Summer 2007 term.
Guide to Oracle 10g
10
11
12
13
A linking table does not contribute any as display columns or search condition columns, but contains join columns that link the other tables through shared foreign key values.
14
Some times queries that join multiple tables can become complex. For example, suppose that you want to create a query to display the COURSE_NO and GRADE values for each of student Tammy Jones courses. This query requires you to join four table: STUDENT (to search for S_FIRST and S_LAST), ENROLLMENT (to display GRADE), COURSE to display COURSE_NO), and COURSE_SECTION (to join ENROLLMENT to COURSE using the C_SEC_ID join column).
Guide to Oracle 10g 15
16
17
18
If you accidentally omit a join condition in a multipletable query, the output retrieves more rows than you expect. When you omit a join condition, the query creates a Cartesian product, whereby every row in one table is joined with every row in the other table. For example , suppose you repeat the query to show each student row, along with each students advisor (see Fig 3-43), but you omit the join condition. Every row in the STUDENT table (six rows) is joined with every row in the FACULTY table (five rows). The result is 6 times 5 rows, or 30 rows.
Guide to Oracle 10g 19
20
Outer Join
An inner join returns rows only if values exist in all tables that are joined. If no values exist for a row in one of the joined tables, the inner join does not retrieve the row. For example suppose you want to retrieve the different locations of the courses included in the COURSE_SECTION table. This query requires joining rows in the LOCATION and COURSE_SECTION tables. Not every location in the LOCATION table has an associated COURSE_SECTION row, so the query retrieves rows only for locations that have associated COURSE_SECTION rows.
Guide to Oracle 10g 21
22
SQL> SELECT c_sec_id, location.loc_id 2 From course_section, location 3 Where course_section.loc.id = location.loc_id; C_SEC_ID LOC_ID ----------------------1 1 11 1 3 2 8 3 6 5 7 5 10 5 13 5 9 5 4 6 12 6 5 6 2 7
23
An outer join returns all rows from one table , which is called the inner table. An outer join also retrieves matching rows from a second table, which is called the outer table. The query designer specifies which table is the inner table and which table is the outer table. In this case, because you want to retrieve all of the rows in the LOCATION table, you specify LOCATION as the inner table.
Guide to Oracle 10g 24
Outer Joins
Syntax
inner_table.join_column = outer_table.join_column(+)
The outer join operator (+) signals the DBMS to insert a NULL value for the columns in the outer table that do not have matching rows in the inner table.
25
26
Self-Joins
Sometimes a relational database table contains a foreign key that references a column in the same table. For example, at Northwood's University each assistant and associate professor is assigned to full professor who serves as the junior professors supervisor.
27
Self-Joins
To create a query that lists the names of each junior faculty member and the names of their supervisor, you must join the FACULTY table to itself. When you create a query that joins a table to it self, you create a selfjoin. To create a self-join, you must create a table alias and structure the query as if you are joining the table to a copy of itself. The syntax to create a table alias in the FORM clause is: FROM table1 alias1, When you create a table alias, you must the use the table alias, rather than the table name, to qualify column names in the SELECT clause and in join condition. T o make the process easier to understand, you create two table aliases _FAC for faculty version of the table and SUPER for the supervisor version of the same table, as shown in Fig 3-51.
Guide to Oracle 10g 28
29
30
PARENT_PROJECT
P_ID
Self Joins
SUB_PROJECT
P_ID PROJECT_NAME 1 Hardware Support Intranet 2 Hardware Support Interface 3 Hardware Support Database 4 T eller Support System 5 Internet Advertising 6 Network Design 7 Exploration Database CLIENT_ID 2 2 2 4 6 6 5 MGR_ID 105 103 102 105 105 104 102 5 1 1 PARENT_P_ID
PROJECT_NAME 1 Hardware Support Intranet 2 Hardware Support Interface 3 Hardware Support Database 4 T eller Support System 5 Internet Advertising 6 Network Design 7 Exploration Database
CLIENT_ID 2 2 2 4 6 6 5
PARENT_P_ID 1 1
PROJECT
P_ID PROJECT_NAME 1 Hardware Support Intranet 2 Hardware Support Interface 3 Hardware Support Database 4 T eller Support System 5 Internet Advertising 6 Network Design CLIENT_ID 2 2 2 4 6 6 5 MGR_ID 105 103 102 105 105 104 102 5 1 1 PARENT_P_ID
31
7 Exploration Database
32
Nested query
First query that appears in SELECT command Retrieves values that main querys search condition must match
Subquery
33
34
The query in Fig 3-54 to retrieve the names of all students who have the same S_CLASS value as student Jorge Perez.
35
To create a nested query in which the subquery retrieves multiple values. You must use IN comparison operator instead of the equal to comparison operator.
Ex: To retrieve the names of all students who have enrolled in the same course sections as Jorge Perez. Note that the main querys search conditions uses the IN comparison operator, because the sub-query returns multiple values.
36
ENROLLEMENT S_ID C_SEC_ID PE100 1 PE100 5 PE100 6 PE100 9 JO100 1 JO100 6 JO100 9 MA100 1 JO101 5 JO101 9
GRADE
B A B B C C C B C C
37
38
To join search conditions associated with subqueries. Ex: Fig 3-53 is used to retrieve the names of all students who have the same S_CLASS value as Jorge Perez and who have also been enrolled in course section with him
39
40
Nested subquery
Subquery that contains second subquery that specifies its search expression
41
Joins output of two unrelated queries into single output result Syntax
query1 UNION query2; Both queries must have the
same number of display columns in their SELECT clauses must have the same data type
For example, if the display columns retuned by query1 are a NUMBER data column and then a VARCHAR2 data column, then the display columns returned by query2 must also be a NUMBER data column followed by a VARCHAR2 data column.
43
INTERSECT
Finds intersection in two queries Requires that both queries have same number and data types of display columns in SELECT statement Automatically suppresses duplicate rows.
44
45
INTERSECT Some queries require an output that finds the intersection, or matching rows, in two unrelated queries. For example, suppose you need to find a list of faculty members whose offices are in the BUS building and who have also taught a course in the BUS building.
46
MINUS
47
48
49
Source query
Used to create view Specify subset of single tables columns or rows or join multiple tables
Updatable views
50
Database Views
Logical table based on a query Does not physically exist in the database as table Presents data in a different format from underlying tables Uses:
Database Views
Simple Views
Based on SQL query that retrieves data from only one table View can support all table DML operations:
Can also execute update action queries and delete action queries using view
Complex Views
Based on query that retrieves data from multiple tables Can only be used to support SELECT operations
54
Creating Views
If there is possibility that you have already created a view using a specific name, you can use the following command to create or replace the existing view according to this syntax. CREATE OR REPLACE VIEW view_name AS source_query;
55
Ex: This view contains all of the FACULTY columns except F_PIN and F_IMAGE
56
2- Type SELECT * FROM faculty_view; to determine whether the new faculty member is included in the table. Type DELETE FROM faculty_view WHERE f-last =MAY; to delete the new faculty member.
57
You can query a view using a SELECT statement, just as with a database table, and use the view in complex queries that involve join operations and subqueries. You create a query that joins FACULTY_VIEW with the LOCATION to list the names of each faculty member, along with the building code and room number of the faculty members office.
Guide to Oracle 10g 58
59
Removing Views
60
61
The NOWAIT command causes the system to generate an error message immediately if another user has previously locked the selected records. If the NOWAIT command is omitted, the system forces the user to wait until the requested records are unlocked, and the user can do no further processing.
62
63
64
Created when search expression is prefaced with an ampersand (&) System then prompts user for value
65
Syntax:
You can then substitute the variable name for a query search condition value
66
Example:
67
Indexes
Index: Separate table is maintained that shows index keys and physical locations of corresponding records
SLName Brown
ROWID 13387289
Jones
Smith Helgeson
13879872
58925789 29875018
68
Using Indexes
Create table index AFTER table is populated with data Indexes make INSERT, UPDATE, and DELETE operations slower because index must also be maintained
Indexing Strategies
Create indexes based on fields used for search or join operations Typically, indexes only speed retrievals when <15% of the table records are involved
Each additional index adds processing overhead for INSERT, UPDATE, and DELETE operations In Oracle, primary keys are automatically indexed
70
Creating Indexes
Syntax: CREATE INDEX index_name ON tablename(index_field);
71
Summary
DISTINCT qualifier
Summary (continued)
Can change appearance of SQL*Plus environment Join multiple tables in SELECT query