SQL Lecture-7
SQL Lecture-7
Lecture Week 7
Introduction to Oracle 9i SQL
Last Lecture
Joining Tables
Multiple Table Queries
Simple Joins
Complex Joins
Cartesian Joins
Outer Joins
Multi table Joins
Other Multiple Table Queries
The
The
Simple Joins
The
Example:
Select emp.empno, emp.ename, dept.dname
from emp,dept
Where emp.deptno = dept.deptno
[email protected]
Another Example
Select emp.*, dept.dname, dept.loc
from emp, dept
Where emp.deptno = dept.deptno
Remember that the asterisk (*) represents all columns of a table.
Cross Join
Complex Joins
Apart
Joins of Non-Equality
TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME !=
TABLE2.COLUMN_NAME ]
Natural Join
Select empno,
empno, ename,
ename, dname from
emp NATURAL JOIN dept;
Joins - Explanation
Outer Joins
An
The
The
table with the (+) should be the table that does not
have matching rows.
In
Outer Join
The Oracle syntax is
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE TABLE1.COLUMN_NAME[(+)] = TABLE2.COLUMN_NAME[(+)]
[ AND TABLE1.COLUMN_NAME[(+)] = TABLE3.COLUMN_NAME[(+)]]
Outer Join
The next example accomplishes the desired output
through the use of an OUTER JOIN. Oracle's syntax is
used for the OUTER JOIN (right).
SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P, ORDERS_TBL O
WHERE P.PROD_ID = O.PROD_ID(+);
O.PROD_ID(+);
It will display all records of Products table and only
matching records from Orders Table.
Note:
IN KEYWORD
RIGHT OUTER JOIN
AND
LEFT OUTER JOIN
The word OUTER
Is Optional.
SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P RIGHT OUTER JOIN ORDERS_TBL O
on P.PROD_ID = O.PROD_ID;
O.PROD_ID;
SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P RIGHT join ORDERS_TBL O
USING (PROD_ID
); [email protected]
(PROD_ID);
SELECT P.PROD_DESC,
P.PROD_DESC, O.QTY FROM
PRODUCTS_TBL P Left join ORDERS_TBL O
USING (PROD_ID
); [email protected]
(PROD_ID);
Self Join
Self Joins
The SELF JOIN is used to join a table to itself,
as if the table were two tables, temporarily
renaming at least one table in the SQL
statement. The syntax is as follows.
on Multiple Keys
We may have a table that has a primary
key that is comprised of more than one
column. You may also have a foreign key
in a table that consists of more than one
column, which references the multiple
column primary key.
Null?
--------------NOT NULL
NOT NULL
NOT NULL
NOT NULL
Type
-------NUMBER(10)
NUMBER(10)
VARCHAR2(30)
NUMBER(8,2)
Type
---------------------------NUMBER(10)
NUMBER(10)
NUMBER(10)
NUMBER(5)
DATE
Creating VIEWS
Improve Security Through Views
A view
is often referred to as a virtual table
allows a user to see a customized selection of one or
more tables
is stored as an SQL query, which is executed
whenever the view is used
reflects the current state of the database
can be treated as another table (with special
restrictions on modifying the data within the view)
[email protected]
A view
is created using the create view command
is displayed using a normal select command
can be referred to by a select, insert, update
or delete command
is dropped using the drop view command
view name
create view dept30_emps
as
(select empno, ename, job, mgr
from emp
where deptno = 30);
query
emp table
dept30_emps view
view name
create view annual_costs
as
(select empno, ename, comm,
sal*12
annual_sal
from emp);
query
emp table
annual_costs view
[email protected]
10
11
12
order by
13
14
15
16
A view
provides an additional level of security
different groups of users have different
database privileges
a view can be used to control the
information the user has access to
21
A view
can be used to convert units
e.g. to see salary in terms of rather than $,
a view can be used to convert the values
24
A view
can be treated as a table in its own right
can be used in a query
can be joined to another table or view
27
29
The END
Introduction to SQL
Chapter 1
SQL Fundamentals
Chapter 2
SQL Plus Overview
Chapter 3
Single row functions
Chapter 4
Aggregating data and single row functions
Chapter 5
Joins and sub queries
[email protected]
Chapter 6
Modifying Data (Insert, update, delete)
Chapter 7
Managing tables (alter table, constraints)
Chapter 8
Working with views
Chapter 10
Creating users, granting access to users etc