Download presentation
1
ITBIS373 Database Development
Lecture3c - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g
2
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 Guide to Oracle 10g
3
Joining Multiple Tables
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
4
Joining Multiple Tables (continued)
Must qualify column name in SELECT clause Specify name of table that contains column followed by period then column name Join condition Specifies table names to be joined and column names on which to join tables SQL supports multiple types of join queries Guide to Oracle 10g
5
Inner Joins Simplest type of join
VALUES in one table equal to values in other table Also called: Equality join Equijoin Natural join Query design diagram Guide to Oracle 10g
6
Guide to Oracle 10g
7
Fig 3-43 to retrieve the student Id, student last and first names, advisor ID, and advisor last name. Guide to Oracle 10g
8
In SQL queries, you can join any number of tables in SELECT command
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
9
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
Visual Representation of the Northwoods University Database
Guide to Oracle 10g
11
Guide to Oracle 10g
12
Guide to Oracle 10g
13
Guide to Oracle 10g
14
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. Guide to Oracle 10g
15
STUDENT (to search for S_FIRST and S_LAST),
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
16
Guide to Oracle 10g
17
Deriving a SQL Query From a Query Design Diagram
Guide to Oracle 10g
18
Guide to Oracle 10g
19
If you accidentally omit a join condition in a multiple-table 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 student’s 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
20
Guide to Oracle 10g
21
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
22
Guide to Oracle 10g
23
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 Guide to Oracle 10g
24
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
25
Outer Joins Returns all rows from one table
Called inner table And matching rows from second table Called outer table 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. Guide to Oracle 10g
26
Guide to Oracle 10g
27
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 professor’s supervisor. Guide to Oracle 10g
28
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 self-join. 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
29
Guide to Oracle 10g
30
Guide to Oracle 10g
31
Self Joins PARENT_PROJECT SUB_PROJECT PROJECT Guide to Oracle 10g
32
Self Join Example Guide to Oracle 10g
33
Creating Nested Queries
Nested query Consists of main query and one or more subqueries Main query First query that appears in SELECT command Subquery Retrieves values that main query’s search condition must match Guide to Oracle 10g
34
Creating Nested Queries with Subqueries that Return a Single Value
Guide to Oracle 10g
35
The query in Fig 3-54 to retrieve the names of all students who have the same S_CLASS value as student Jorge Perez. Guide to Oracle 10g
36
Creating Subqueries that Return Multiple Values.
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 query’s search conditions uses the IN comparison operator, because the sub-query returns multiple values. Guide to Oracle 10g
37
ENROLLEMENT S_ID C_SEC_ID GRADE PE100 1 B PE100 5 A PE100 6 B
JO C JO C JO C MA B JO C JO C Guide to Oracle 10g
38
Guide to Oracle 10g
39
Using Multiple Subqueries Within a Nested Query
Use AND and OR operators 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 Guide to Oracle 10g
40
Guide to Oracle 10g
41
Creating Nested Subqueries
Nested subquery Subquery that contains second subquery that specifies its search expression Guide to Oracle 10g
42
USING SET Operator to Combine Query Results
UNION set operator 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. UNION ALL operator Same as UNION but includes duplicate rows Guide to Oracle 10g
43
Ex: Create a telephone directory of every student and faculty member at Northwood's University
Guide to Oracle 10g
44
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. Guide to Oracle 10g
45
Guide to Oracle 10g
46
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. Guide to Oracle 10g
47
MINUS To find difference between two unrelated query result list.
As with the UNION and INTERSECT operators, the MINUS operator requires that both queries have the same number of display columns in the SELECT statement, and that each column in the first query has the same data type as corresponding column in the second query. Guide to Oracle 10g
48
Guide to Oracle 10g
49
Guide to Oracle 10g
50
Creating and Using Database Views
Source query Used to create view Specify subset of single table’s columns or rows or join multiple tables Updatable views Can be used to update database Guide to Oracle 10g
51
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: Security Simplifying complex queries Guide to Oracle 10g
52
Database Views Creating a view: CREATE VIEW view_name AS SQL_command;
Views can be queried just like tables: SELECT * FROM view_name; Guide to Oracle 10g
53
Simple Views Based on SQL query that retrieves data from only one table View can support all table DML operations: INSERT UPDATE DELETE Can also execute update action queries and delete action queries using view Just as with database table Guide to Oracle 10g
54
Complex Views Based on query that retrieves data from multiple tables
Can only be used to support SELECT operations No DML operations supported Guide to Oracle 10g
55
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; Guide to Oracle 10g
56
Ex: This view contains all of the FACULTY columns except F_PIN and F_IMAGE
Guide to Oracle 10g
57
Executing Action Queries Using Views
1- To insert a row using the FACULTY_VIEW INSERT INTO faculty_view VALUE (6,’may’,’Lisa,’I’,11,’ ’,’INST’) 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. Guide to Oracle 10g
58
Retrieving Rows from Views.
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 member’s office. Guide to Oracle 10g
59
Guide to Oracle 10g
60
Removing Views DROP VIEW command Remove view from user schema Syntax
DROP VIEW view_name; Ex: DROP VIEW faculty_view; Guide to Oracle 10g
61
Selecting Records For Update
Syntax: SELECT column1, column2, … FROM table1, table2, … WHERE search and join conditions FOR UPDATE OF column1, column2, … NOWAIT; Guide to Oracle 10g
62
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. Guide to Oracle 10g
63
Guide to Oracle 10g
64
Dynamic SQL Queries Queries that allow users to specify search conditions at runtime Approaches Substitution Values Runtime Variables Guide to Oracle 10g
65
Using Substitution Values
Created when search expression is prefaced with an ampersand (&) System then prompts user for value Guide to Oracle 10g
66
Using Runtime Variables
Runtime variable: variable defined in SQL*Plus environment Syntax: DEFINE variable_name = variable_value; You can then substitute the variable name for a query search condition value Guide to Oracle 10g
67
Using Runtime Variables
Example: Guide to Oracle 10g
68
Indexes Index: Separate table is maintained that shows index keys and physical locations of corresponding records In Oracle, ROWID is translated to physical location of row on disk Improves response time of searches and joins SLName ROWID Brown Jones Smith Helgeson Guide to Oracle 10g
69
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 Guide to Oracle 10g
70
Indexing Strategies A table can have indexes on multiple fields
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 Guide to Oracle 10g
71
Creating Indexes Syntax: CREATE INDEX index_name
ON tablename(index_field); Guide to Oracle 10g
72
Summary INSERT action query SQL search condition UPDATE action query
Match one or more database rows UPDATE action query DELETE command SELECT query DISTINCT qualifier Single row and group functions Guide to Oracle 10g
73
Summary (continued) Can change appearance of SQL*Plus environment
Join multiple tables in SELECT query Inner join Outer join Nested queries Set operators Views Guide to Oracle 10g
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.