Difference Between The Following
Difference Between The Following
Syntax: - A ⋈θ B
Theta join can use any condition in the selection criteria
Consider the following tables
Table A Table B
1 1 1 1
1 2 1 3
For example:
Column 1 Column 2
1 2
b) EQUI JOIN
EQUI JOIN is done when a Theta join uses only the equivalence condition.
EQUI join is the most difficult operation to implement efficiently in an
RDBMS, and one reason why RDBMS have essential performance
problems.
For example:
A⋈ A. column 2 = B.column 2 (B)
Column 1 Column 2
1 2
c) NATURAL JOIN
NATURAL JOIN does not utilize any of the comparison operators. In
this type of join, the attributes should have the same name and
domain. In Natural Join, there should be at least one common
attribute between two relations.
It performs selection forming equality on those attributes which
appear in both relations and eliminates the duplicate attributes.
For example: -
Table C Table D
2 4 2 8
3 9 3 18
C⋈D
C⋈D
Num Square Cube
2 4 8
3 9 18
d) Outer JOIN
An OUTER JOIN doesn’t require each record in the two join tables to
have a matching record. In this type of join, the table retains each
record even if no other matching record exists.
There are three types of Outer joins : -
Left Outer join
Right Outer join
Full Outer join
Define the five basic operators of relational algebra with an example each.
I. SELECT(σ)
The SELECT operation is used for selecting a subset of the tuples according to a
given selection condition. It denotes sigma(σ) symbol. It is used as an
expression to choose tuples which meet the selection condition. Select operator
selects tuples that satisfy a given predicate.
σp(r) where ,
σ is the predicate
‘r’ stands for relation which is the name of the table
P is prepositional logic.
Example -
σtopic=”Database” (Tutorials)
II. PROJECTION(∏)
The projection eliminates all attributes of the input relation but those
mentioned in the projection list. The projection method defines that contains a
vertical subset of Relation.
This helps to extract the value of specified attributes to eliminate duplicate
values. (Pi) symbol is used to choose attribute from a relation. This operator
helps you to keep specific columns from a relation and discards the other
columns.
Example of projection :
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active
Table A Table B
Column 1 Column 2 Column 1 Column 2
1 1 1 1
1 2 1 3
AՍB
OUTPUT -
Table A Ս B
Column 1 Column 2
1 1
1 2
1 3
A-B
Table A Table B
Table A – B
Column 1 Column 2
1 2
V. INTERSECTION
An intersection is defined by the symbol ∩.
It defines a relation consisting of a set of all tuple that are in both A and
B. However, A and B must be Union-compatible.
Example –
A∩B
Table A Table B
OUTPUT-
Table A ∩ B
Column 1 Column 2
1 1
2. Explain Relational Algebra using the operators{∂ ,∏,∪,-,X and show that :
A∩B-((A-B)∪(B-A))
First part:
The select operation is to identify a set of tuples which is a part
of a relation and to extract only these tuple out. The select
operation selects tuples that satisfy a given predicate or
condition.
It is a unary operation defined on a single relation.
It is denoted as σ.
Example 1:- Select from the relation “Book” all the books whose
year of publication is 1992.
σYr-pub=1992(Book)
Example 2:- Select from the relation “Book” all the books whose
Ace-no is greater than equal to 56782.
σAcc-no>=56782(Book)
The project Operation returns its argument relation with certain
attributes left out.
It is unary operation defined on a single relation.
It is denoted as ∏.
Example:- List all the Title and Ace-no of the “Book” relation.
∏Ace-no,Title(Book)
The Union operation is used when we need some attributes that appear
in either or both of the two relations.
It is denoted as ∪.
Example:-
Borrower (customer-name, loan-number)
Depositor (customer-name, account-number)
Customer (customer-name, street-number, customer-city)
List all the customers who have either an account or a loan or both
codes:
∏customer-name (Borrower)∪ ∏ customer-name (Depositor)
For a Union operation r ∪ s to be valid, two conditions must hold:
The relation r ∪s must be same,i.e. they must have the same
number of attributes.
The domains of the ith attributes of r and the ith attribute of s
must be the same for all i.
The set difference operation finds tuples in one relation but not in
other.
It is denoted as –
Example:-
Find the names of all customers who have an account but not a
loan.
∏customer-name (Depositor)-∏customer-name (Borrower)
The Cartesian product operation allows combining information from
two relations.
It is denoted as r X s where r and s are relations.
A B
a 1
b 2
c 2
B C
3 1a
2 2b
Therefore r Χ s gives:
Second Part
From the Venn diagram,the intersection of the two sets A &B in the
shaded part i.e., C A ∪ B=((A-B)∪ (B-A))∪ C
(A ∪B)-((A-B)∪(B-A))=C
A∩B-(A∩B)-((A-B)∩(B-A))[proved]
A C B
3. Let R=(A,B) and S=(A,C) and let r® and r(S) be relations. Write relational
algebra expression equivalent to the domain relational calculus
expressions:
a) {<a>| there exist b(<a,b> belongs to r^b=7}
∏˄(σB=7(r))
b) {<a,b,c> |<a,b. belongs to r^<a,c> belongs to s}
r1s
5. Give expression using the tuple relational calculus and the domain relational
calculus to express each of the following queries:
a. Find the names of all employees who work for First Bank Corporation.
Tuple relation calculus-
{t|Ǝ s Є works(t[person-name]=s[person-name]˄ s[company-
name]=”First Bank Corporation”)}
Domain relational calculus-
{<p>|Ǝ c,s (<p,c,s> Є works ˄ c=”First Bank Cororation”)}
b. Find the names and cities of residence of all employees who work for First Bank
Corporation.
Tuple relation calculus-
{t|Ǝ r Є employee Ǝ s Є works (t[person-name]=r[person-name] ˄
t[city]=r[city] ˄ r[person-name]=s[person-name] ˄ s[company-
name]=”First Bank Corporation”)}
Domain relational calculus-
{<p,c>|Ǝ co,sa,st(<p,co,sa> Є works ˄ <p,st,c> Є employee ˄ co=”First
Bank Corporation”)}
c. Find the names, street address, and cities of residence of all employees who
work for First Ban Corporation and earn more than $10,000 per annum.
Tuple relation calculus-
{t|t Є employee ˄ (Ǝ s Є works(s[person-name]=t[person-name] ˄
s[company-name]=”First Bank Corporation” ˄ s[salary]>10000))}
Domain relational calculus-
{<p,s,c>|<p,s,c> Є employee ˄ Ǝ co,sa(<p,co,sa> Є works ˄ co=”First
Bank Corporation” ˄ sa>10000)}
d. Find the names of all employees in the database who live in the same city as
the company for which they work.
Tuple relational calculus-
{t|Ǝ e Є employee Ǝ w Є works Ǝ c Є company(t[person-name]=e[person-
name] ˄ e[person-name]=w[person-name] ˄ w[company-
name]=c[company-name] ˄ e[city]=c[city])}
Domain relational calculus-
{<p>|Ǝ st,c,co,sa(<p,st,c> Є employee ˄ <p,co,sa> Є works ˄ <co,c> Є
company)}
e. Find the names of all employees who live in the same city and on the same
street as do their managers.
Tuple relational calculus-
{t|Ǝ l Є employee Ǝ m Є manages Ǝ r Є employee(l[person-
name]=m[person-name] ˄ m[manager-name]=r[person-name] ˄
l[street]=r[street] ˄ l[city]=r[city] ˄ t[person-name]=l[person-name)}
Domain relational calculus-
{<t>|Ǝ s,c,m(<t,s,c> Є employee ˄ <t,m> Є manages ˄ <m,s,c> Є
employee)}
f. Find the names of all employees in this database who do not work for First
Bank Corporation.
Tuple relational calculus-
{t|Ǝ w Є works(w[company-name]≠ ‘First Bank Corporation’ ˄ t[person-
name]=w[person-name])}
Domain relational calculus-
{<p>|Ǝ c,s(<p,c,s> Є works ˄ c ≠ ‘First Bank Corporation’)}
g. Find the names of all employees who earn more than every employee of Small
Bank Corporation.
Tuple relational calculus-
{t|Ǝ w Є works(t[person-name]=w[person-name] ˄ ∀ s Є
works(s[company-name]= ‘Small Bank Corporation’⟹
w[salary]>s[salary]))}
Domain relational calculus-
{<p>|Ǝ c,s(<p,c,s> Є works ˄ ∀ p2,c2,s2(<p2,c2,s2> ˄ works ˄ c2=
‘Small Bank Corporation’ ⟹ s>s2))}
h. Assume the companies may be located in several cities. Find all companies
located in every city in which Small Bank Corporation is located.
Tuple relational calculus-
{t| ∀ s Є company(s[company-name]= ‘Small Bank Corporation’ ⇒ Ǝ r Є
company(t[company-name]=r[company-name] ˄ r[city]= s[city]))}
Domain relational calculus-
{<co>| ∀ co2,ci2(<co2,ci2> Є company ˄ co2= ‘Small Bank Corporation’
⇒ <co2,ci2> Є company)}