(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
Assignment#06
1. Database System Sunita M. Dol
Page 1
HANDOUT#06
Aim:
a) Views: Creation of views and update it
b) Join Relation: Inner Join, Left Outer Join, Right Outer Join, Full Outer Join
Theory:
Views
We define a view in SQL by using the create view command. To define a view, we must give
the view a name and must state the query that computes the view. The form of the create view
command is
create view v as <query expression>
where <query expression> is any legal query expression. The view name is represented by v.
Join Relation
Oracle JOINS are used to retrieve data from multiple tables. An Oracle JOIN is performed
whenever two or more tables are joined in a SQL statement.
There are 4 different types of Oracle joins:
• Oracle INNER JOIN (or sometimes called simple join)
• Oracle LEFT OUTER JOIN (or sometimes called LEFT JOIN)
• Oracle RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
• Oracle FULL OUTER JOIN (or sometimes called FULL JOIN)
INNER JOIN (simple join)
Visual Illustration
In this visual diagram, the Oracle INNER JOIN returns the shaded area:
The Oracle INNER JOIN would return the records where table1 and table2 intersect.
LEFT OUTER JOIN
Another type of join is called an Oracle LEFT OUTER JOIN. This type of join returns all rows
from the LEFT-hand table specified in the ON condition and only those rows from the other
table where the joined fields are equal (join condition is met).
Visual Illustration
2. Database System Sunita M. Dol
Page 2
In this visual diagram, the Oracle LEFT OUTER JOIN returns the shaded area:
The Oracle LEFT OUTER JOIN would return the all records from table1 and only those records
from table2 that intersect with table1.
RIGHT OUTER JOIN
Another type of join is called an Oracle RIGHT OUTER JOIN. This type of join returns all rows
from the RIGHT-hand table specified in the ON condition and only those rows from the other
table where the joined fields are equal (join condition is met).
Visual Illustration
In this visual diagram, the Oracle RIGHT OUTER JOIN returns the shaded area:
The Oracle RIGHT OUTER JOIN would return the all records from table2 and only those
records from table1 that intersect with table2.
FULL OUTER JOIN
Another type of join is called an Oracle FULL OUTER JOIN. This type of join returns all rows
from the LEFT-hand table and RIGHT-hand table with nulls in place where the join condition is
not met.
Visual Illustration
In this visual diagram, the Oracle FULL OUTER JOIN returns the shaded area:
The Oracle FULL OUTER JOIN would return the all records from both table1 and table2.
3. Database System Sunita M. Dol
Page 3
Each of the variants of the join operations in SQL consists of a join type and a join condition.
The join condition defines which tuples in the two relations match and what attributes are present
in the result of the join. The join type defines how tuples in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
Figure 1 shows some of the allowed join types and join conditions. The first join type is the inner
join, and the other three are the outer joins.
Figure 1: Join types and join conditions.
Queries and Output:
Views
Create the view consisting of branch names and the names of customers who have either an
account or a loan at that branch.
SQL> create view all_customer as
2 (select branch_name, customer_name
3 from depositor, account
4 where depositor.account_number = account.account_number)
5 union
6 (select branch_name, customer_name
7 from borrower, loan
8 where borrower.loan_number = loan.loan_number);
View created.
SQL> select * from all_customer;
BRANCH_NAME CUSTOMER_NAME
--------------- --------------------
Brighton Johnson
Brighton Jones
Downtown Johnson
Downtown Jones
Downtown Williams
4. Database System Sunita M. Dol
Page 4
Mianus Curry
Mianus Smith
Perryridge Adams
Perryridge Hays
Redwood Lindsay
Redwood Smith
BRANCH_NAME CUSTOMER_NAME
--------------- --------------------
Round Hill Smith
Round Hill Turner
13 rows selected.
Find all customers of the Perryridge branch using view created in the above query.
SQL> select * from all_customer where branch_name='Perryridge';
BRANCH_NAME CUSTOMER_NAME
--------------- --------------------
Perryridge Adams
Perryridge Hays
Joined Relation:
Inner Join
Using NATURAL condition
SQL> select * from loan1;
LOAN_NUMBE BRANCH_NAME AMOUNT
---------- --------------- ----------
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
5. Database System Sunita M. Dol
Page 5
SQL> select * from borrower1;
CUSTOMER_NAME LOAN_NUMBE
-------------------- ----------
Jones L-170
Smith L-230
Hayes L-155
SQL> select * from loan1 natural inner join borrower1;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
Using ON condition
SQL> select * from loan1 inner join borrower1 on loan1.loan_number=borrower1.loan_number;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
LOAN_NUMBE
---------- --------------- ---------- -------------------- ----------
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
Using USING condition
SQL> select * from loan1 join borrower1 using (loan_number);
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
Left Outer Join
Using NATURAL condition
SQL> select * from loan1;
LOAN_NUMBE BRANCH_NAME AMOUNT
---------- --------------- ----------
6. Database System Sunita M. Dol
Page 6
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
SQL> select * from borrower1;
CUSTOMER_NAME LOAN_NUMBE
-------------------- ----------
Jones L-170
Smith L-230
Hayes L-155
SQL> select * from loan1 natural left outer join borrower1;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700
Using ON condition
SQL> select * from loan1 left outer join borrower1 on
loan1.loan_number=borrower1.loan_number;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
LOAN_NUMBE
---------- --------------- ---------- -------------------- ----------
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700
Using USING condition
SQL> select * from loan1 left outer join borrower1 using (loan_number);
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
7. Database System Sunita M. Dol
Page 7
L-230 Redwood 4000 Smith
L-260 Perryridge 1700
Right Outer Join
Using NATURAL condition
SQL> select * from loan1 natural right outer join borrower1;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 Hayes
Using ON condition
SQL> select * from loan1 right outer join borrower1 on
loan1.loan_number=borrower1.loan_number;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
LOAN_NUMBE
---------- --------------- ---------- -------------------- ----------
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
Hayes L-155
Using USING condition
SQL> select * from loan1 right outer join borrower1 using (loan_number);
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 Hayes
Full Outer Join
Using NATURAL condition
SQL> select * from loan1 natural full outer join borrower1;
8. Database System Sunita M. Dol
Page 8
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700
L-155 Hayes
Using ON condition
SQL> select * from loan1 full outer join borrower1 on
loan1.loan_number=borrower1.loan_number;
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
LOAN_NUMBE
---------- --------------- ---------- -------------------- ----------
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700
Hayes L-155
Using USING condition
SQL> select * from loan1 full outer join borrower1 using (loan_number);
LOAN_NUMBE BRANCH_NAME AMOUNT CUSTOMER_NAME
---------- --------------- ---------- --------------------
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700
L-155 Hayes
Conclusion:
We have written and executed queries in SQL using
• Views: Creation of views and update it
• Join Relation:
o Inner Join,
o Left Outer Join,
o Right Outer Join,
o Full Outer Join
9. Database System Sunita M. Dol
Page 9
References:
• Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) sixth edition.
• Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) fifth edition.
• http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
• http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
• http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/