using sql queries to insert, update, delete, and view data: joining multiple tables © abdou illia...
TRANSCRIPT
![Page 1: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/1.jpg)
Using SQL Queries to Insert, Update, Delete, and View Data:
Joining Multiple Tables
© Abdou Illia MIS 4200 - Spring 2014
Monday 2/24/2014
![Page 2: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/2.jpg)
2
Lesson C Objectives
After completing this lesson, you should be able to:Create SQL queries that join multiple tablesCreate nested SQL queriesCombine query results using SET operatorsCreate and use database views
![Page 3: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/3.jpg)
3
Joining Multiple Tables
a Join– Combines data from multiple tables using foreign key
references Syntax
SELECT column1, column2, …
FROM table1, table2
WHERE table1.joincolumn = table2.joincolumn
AND search_condition(s);
SELECT s_id, s_last, f_lastFROM student, facultyWHERE student.f_id = faculty.f_idAND f_last IN (‘Marx’, ‘Zhulin’);
![Page 4: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/4.jpg)
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
– Example: SELECT s_id, s_last, student.f_idJoin condition
– Specifies table names to be joined and column names on which to join tables
– Example: WHERE student.f_id = faculty.f_id
![Page 5: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/5.jpg)
5
Inner JoinsSimplest type of join Also called: Equality join, Equijoin, Natural joinVALUES in one table equal to values in other tableQuery design diagram helps get the query right
SELECT s_id, s_last, s_first, student.f_id, f_lastFROM student, facultyWHERE student.f_id = faculty.f_id;
Could be replaced by:
FROM Student NATURAL JOIN faculty;
![Page 6: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/6.jpg)
6
Display column, search column, join column
Display columns: appear in SELECT clause Search columns: appear in search conditionJoin columns: primary key and foreign key column
on which you join the tables.Linkage table: contains join column to link other
tables through foreign key values.
SELECT f_lastFROM faculty, course_section, termWHERE faculty.f_id = course_section.f_idAND course_section.term_id = term.term_idAND term_desc = 'Summer 2007';
![Page 7: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/7.jpg)
7
Deriving a SQL Query From a Query Design Diagram
4 tables, 3 links All 4 tables must be named in the
FROM clause Query must have 3 join conditions
because there are 3 links Always 1 fewer join condition than
number of tables that query joins. If you omit one join condition, the
query creates a Cartesian product (every row in one table is joined with every row in other table) with more row than expected.
SELECT course_name, gradeFROM student, enrollment, course_section, courseWHERE student.s_id = enrollment.s_idAND enrollment.c_sec_id = course_section.c_sec_idAND course_section.course_no = course.course_noAND s_last = 'Jones'AND s_first = 'Tammy';
Search conditions
![Page 8: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/8.jpg)
8
Outer JoinsInner joins return row only if values exist in all joined
tablesOuter joins return
all rows from one table (called inner table) andonly matching rows from second table (outer table)
Syntax: inner_table.join_col = outer_table.join_col(+)
(+) operator signals Oracle to insert NULL for columns from the outer table with no matching rows in the inner table.
![Page 9: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/9.jpg)
9
Self-join
Query that joins table to itselfMust create table alias
– Alternate name assigned to table in query’s FROM clause– Syntax: FROM table1 alias1, table1 alias2 …
![Page 10: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/10.jpg)
10
Creating Nested Queries Nested query
– Consists of a 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
Subquery is evaluated first. Then, DBMS substitute subquery’s output into main query.
![Page 11: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/11.jpg)
Creating Nested Queries
11
Q: What would happen if a subquery generated more values than the main query is expecting?
![Page 12: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/12.jpg)
12
Creating subqueries that return multiple values
![Page 13: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/13.jpg)
13
Using Multiple Subqueries Within a Nested Query
Use AND and OR operators – To join search conditions associated with subqueries
![Page 14: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/14.jpg)
14
Using SET operators to combine Query Results
UNION– Queries must have
same number of display column in their SELECT clause
– Corresponding display columns must have same data type
Note: S_LAST, S_FIRST, S_PHONE used as display title even though there are faculty members names displayed along with students.
![Page 15: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/15.jpg)
15
Using SET operators to combine Query Results
INTERSECT– Queries must have
same number of display column in their SELECT clause
– Corresponding display columns must have same data type
– Suppresses duplicates
![Page 16: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/16.jpg)
16
Using SET operators to combine Query Results
MINUS– Queries must have
same number of display column in their SELECT clause
– Corresponding display columns must have same data type
– Suppresses duplicates– Finds difference
between two query results
![Page 17: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/17.jpg)
17
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 Syntax
CREATE VIEW view_name AS source_query;– OrCREATE OR REPLACE VIEW view_name AS source_query;
![Page 18: Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables © Abdou Illia MIS 4200 - Spring 2014 Monday 2/24/2014](https://reader035.vdocuments.site/reader035/viewer/2022062312/5519b18a5503467a578b4602/html5/thumbnails/18.jpg)
18
Removing Views
DROP VIEW command – Remove view from user schema
– Syntax•DROP VIEW view_name;