DBDev 03C
DBDev 03C
Chapter 3c
Using SQL Queries to Insert, Update, Delete, and View
Data
! In this chapter:
◦ Create SQL queries that join multiple tables
◦ Create nested SQL queries
● The output form query 1 serves as a search condition in
query 2.
◦ Combine query results using set operators
◦ Create and use database views
◦ Create DB indexes.
2 / 61
Joining Multiple Tables
! Strength of SQL
! Joins or combines tables using foreign key
references. To qualify a column name, specify the
name of the table that contains the
column, followed by a period , then
specify the column name.
! General Syntax
SELECT column1, column2, …
FROM table1, table2
WHERE table1.joincolumn = table2.joincolumn
AND search_condition(s);
Join condition: specifies the table names
to be joined and col names on which to
join the tables. It contains foreign key ref
in one table and primary key in the other.
3 / 61
Join Queries: Inner Joins
! The simplest type
! Also called:
◦ equality join
◦ Equijoin
◦ natural join
4 / 61
Join Queries: Inner Joins
5 / 61
Join Queries: Inner Joins The pri key & foreig
key col on which you
join the tables are
called join colm
7 / 61
Join Queries: Inner Joins
! 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.
8 / 61
Join Queries: Inner Joins
9 / 61
Join Queries: Inner Joins
! A linking table does not contribute as display
columns or search condition columns, but contains
join columns that link the other tables through
shared foreign key values.
10 / 61
Join Queries: Inner Joins
Even though the query does not display cols from the
COURSE_SECTION or include them in search conditions, this table
must be included in the FROM clause, and include join conditions to not
only specify the links between FACULTY and COURSE_SECTION, but
also the links between the COURSE_SECTION and TERM.
12 / 61
Join Queries: Inner Joins
! A query design diagram shows the display and
search columns in a SQL query that joins multiple
tables, as well as the required join columns and
their links.
13 / 61
Join Queries: Inner Joins
! Deriving the SQL query from the query design
diagram
14 / 61
Join Queries: Inner Joins
15 / 61
Join Queries: Inner Joins
! If you accidentally omit a join condition in a
multiple-table query, the output retrieves more
rows than you expect.
16 / 61
Join Queries: Inner Joins
17 / 61
Join Queries: Outer Joins
! 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.
18 / 61
Join Queries: Outer Joins
19 / 61
Join Queries: Outer Joins
! To retrieve information for all locations, regardless
of whether they have associated course sections
or not, Outer Joins must be used.
21 / 61
Join Queries: Outer Joins
! How to specify 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.
◦ Because you want to display the course section rows if they exist, you
specify COURSE_SECTION as the 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.
22 / 61
Join Queries: Outer Joins
23 / 61
Join Queries: Self-Joins
! Sometimes a relational DB table contains a foreign
key that references a column in the same table.
24 / 61
Join Queries: Self-Joins
! The ID stored in the F_SUPER col can be linked
back to the F_ID col in the table to identify the
name of the individual’s supervisor.
26 / 61
Join Queries: Self-Joins
! The syntax to create a table alias in the FROM clause is:
FROM table1 alias1,……
! When you create a table alias, you must then use the
table alias, rather than the table name, to qualify column
names in the SELECT clause and in join condition.
27 / 61
Join Queries: Self-Joins
28 / 61
Nested Queries
! Query that is based on the results of another
query.
29 / 61
Nested Queries
! The other alternative way is to use a single nested
query:
30 / 61
Nested Queries
! In a nested query, the DBMS executes the
subquery first, and the main query second.
31 / 61
Nested Queries
Subqueries that return a single value
! General Syntax
32 / 61
Nested Queries
Subqueries that return a single value
! If the subquery retrieves no rows or multiple rows,
an error message appears.
33 / 61
Nested Queries
Subqueries that return a single value
! Example: Retrieve first and last of all students who
have the same S_CLASS value as student Jorg
Perez. The main query retrieves
the student first and last
names
34 / 61
Nested Queries
Subqueries that return a single value
! To make the query easier to read and understand,
indent the subquery .
35 / 61
Nested Queries
Subqueries that return multiple values
! The subquery returns multiple results each of
which satisfies the search condition.
36 / 61
Nested Queries
Subqueries that return multiple values
You use the DISTINCT qualifier in the
main query’s SELECT clause to suppress
duplicate names.
37 / 61
Nested Queries Retrieve the names of all students who have
Multiple Subqueries the same S_CLASS value as Jorge Perez
and have also been enrolled in a course
section with him.
38 / 61
Nested Queries
Nested Subqueries
! Subquery that contains second subquery that specifies
its search expression.
! Example: Create a query to retrieve the names of
students who have taken courses with Jorge Perez in the
CR building.
! The innermost subquery retrieves the course section ID
values for all course sections located in the CR building.
! The outermost main query retrieves the names of all
students who enrolled in the course sections that satisfy
both of these conditions.
39 / 61
! SELECT distinct s_first, s_last
! from student,enrollment
! where student.s_id = enrollment.s_id
! and c_sec_id in ( select c_sec_id
! from student,enrollment
! where student.s_id = enrollment.s_id
! and s_first like 'Jorge'
! and c_sec_id in ( select c_sec_id
! from course_section, location
! where course_section.loc_id = location.loc_id
! and location.bldg_code = 'CR'
! )
! )
! ;
40 / 61
Nested Queries
Nested Subqueries
! Nested queries execute slower than queries that
join multiple tables, so you should probably not use
nested queries unless you cannot retrieve the
desired result using a non-nested query.
◦ See Figure 3-57
41 / 61
Using Set Operators to Combine Query
Results
! Combine results of 2 separate queries not specified by
foreign key relationships.
42 / 61
Using Set Operators to Combine Query
Results
! These rows don’t have any foreign key
relationships, so the only way to retrieve them is to
create multiple queries.
43 / 61
UNION and UNION ALL
! Joins the output of two unrelated queries into
single output result. Create a telephone directory of every student
and faculty member.
! General syntax
query1 UNION query2; Both queries must have the
same number of display
columns in their SELECT
clauses, and each display
column in the first query must
have the same data type as the
corresponding column in the
second query.
45 / 61
INTERSECT
! Returns only rows returned by both queries
46 / 61
INTERSECT
! Example: create a query that retrieves the courses
taken only by students who were not seniors, in
addition to courses that were offered in term 6.
(See Figure 3-61)
47 / 61
MINUS
! Finds the difference between two unrelated query
result lists.
48 / 61
MINUS
! Example: retrieve the courses that were taken by
freshmen, sophomores, and juniors, but were not
offered in term 6.
◦ You can use the previous statement MINUS any courses
offered in term 6.
◦ (See Figure 3-62)
49 / 61
Database Views
! Presents data in a different format than the one
stored in tables.
50 / 61
Database Views
! You can then retrieve data from the view the same
way you retrieve data from a table.
51 / 61
Database Views
! When users insert, update, or delete data values in
view’s source tables, the view reflects the updates
as well.
52 / 61
Database Views
! Once you create a view, you can use it to retrieve
rows just as with a table.
◦ This saves you from having to reenter complex query
commands.
● e.g. no need to retype the join or search conditions.
53 / 61
Database Views
! Syntax:
CREATE VIEW view_name
AS source_query;
54 / 61
Database Views
! Once created, you can grant or revoke privileges
to other users to perform operations such as
SELECT, INSERT, or UPDATE.
55 / 61
Database Views
! When creating an updatable view, the SQL query
has specific restrictions:
56 / 61
Database Views
! This view contains all of the FACULTY columns
except F_PIN and F_IMAGE
57 / 61
Database Views
! You can use a complex source query to create a
view that contains a listing of the courses being
taught by faculty members for term 6.
58 / 61
Database Views
CREATE OR REPLACE VIEW faculty_teaching AS
View created.
____________
Figure 3-64
59 / 61
Database Views
! After creating an updatable view you can use the view to
execute action queries that insert, update, or delete data
in the source tables, as far as you don’t violate any
constraints that exist for the source table. And provided
that you have sufficient object privileges for updating or
deleting rows in the view.
60 / 61
Example: create a query that joins
FACULTY_VIEW with the LOCATION table
61 / 61
Database Views
! To remove a view from your user schema:
DROP VIEW view_name;
62 / 61