DBMS U2 Answers
DBMS U2 Answers
Relational algebra is a procedural query language. It gives a step by step process to obtain the result of the
query. It uses operators to perform queries.
Types of Relational operation
1. Select Operation:
The select operation selects tuples that satisfy a given predicate.
It is denoted by sigma (σ).
Notation: σ p(r)
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
For example: LOAN Relation
BRANCH_NAME LOAN_NO AMOUNT
Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
Input:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUNT
Perryride L-15 1500
Perryride L-16 1300
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result. Rest of the
attributes are eliminated from the table.
o It is denoted by ∏.
1. Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION
NAME STREET CITY
Jones Main Harrison
Smith North Rye
Hays Main Harrison
Curry North Rye
Johnson Alma Brooklyn
Brooks Senator Brooklyn
Input:
∏ NAME, CITY (CUSTOMER)
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that are either in
R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
A union operation must hold the following condition:
o R and S must have the attribute of the same number.
o Duplicate tuples are eliminated automatically.
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in
both R & S.
o It is denoted by intersection ∩.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in
R but not in S.
o It is denoted by intersection minus (-).
Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the other table.
It is also known as a cross product.
o It is denoted by X.
Notation: E X D
Example:
EMPLOYEE
EMP_ID EMP_NAME EMP_DEPT
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
EMPLOYEE X DEPARTMENT
Output:
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
Tuple Relational Calculus is a non-procedural query language unlike relational algebra. Tuple
Calculus provides only the description of the query but it does not provide the methods to solve it.
Thus, it explains what to do but not how to do.
In Tuple Calculus, a query is expressed as
{t| P(t)}
where,
t = resulting tuples,
P(t) = known as Predicate and these are the conditions that are used to fetch t
Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
It also uses quantifiers:
∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.
Example:
Table-1: Customer
Customer name Street City
Saurabh A7 Patiala
Mehak B6 Jalandhar
Sumiti D9 Ludhiana
Ria A5 Patiala
Table-2: Account
Account number Branch name Balance
1111 ABC 50000
1112 DEF 10000
1113 GHI 9000
1114 ABC 7000
Table-3: Branch
Branch name Branch city
ABC Patiala
DEF Ludhiana
GHI Jalandhar
Table-4: Loan
Loan number Branch name Amount
L33 ABC 10000
L35 DEF 15000
L49 GHI 9000
L98 DEF 65000
Queries-1: Find the loan number, branch, amount of loans of greater than or equal to 10000 amount.
{t| t ∈ loan ∧ t[amount]>=10000}
Resulting relation:
Loan number Branch name Amount
L33 ABC 10000
L35 DEF 15000
L98 DEF 65000
Example-1 : Find the Faculty No, Faculty name, DNo, Qual and salaries of all the faculty, who
are drawing more than 30000.
Output :
FNo FName DNo Qual Salary
22 Riya 21 Ph.D 55000
24 Priya 22 M. Tech 50000
25 Zayn 22 M. Tech 42000
30 Sia 23 M. Tech 32000
33 Max 24 Ph.D 53000
35 Fred 24 Ph.D 52000
40 Trent 25 M. Tech 34000
Mapping Cardinality
It is expressed as the number of entities to which another entity can be associated via a relationship set.
For binary relationship set there are entity set A and B then the mapping cardinality can be one of the
following −
One-to-one
One-to-many
Many-to-one
Many-to-many
One-to-one relationship
One entity of A is associated with one entity of B.
Example
Given below is an example of the one-to-one relationship in the mapping cardinality. Here, one
department has one head of the department (HOD).
One-to-many relationship
An entity set A is associated with any number of entities in B with a possibility of zero and entity in B is
associated with at most one entity in A
Example
Given below is an example of the one-to-many relationship in the mapping cardinality. Here, one
department has many faculties.
Many-to-one relationship
An entity set A is associated with at most one entity in B and an entity set in B can be associated with
any number of entities in A with a possibility of zero.
Example
Given below is an example of the many-to-one relationship in the mapping cardinality. Here, many
faculties work in one department.
Many-to-many relationship
Many entities of A are associated with many entities of B.
An entity in A is associated with many entities of B and an entity in B is associated with many entities of
A.
Many to many=many to one + one to many
Example
Given below is an example of the many-to-many relationship in the mapping cardinality. Here, many
employees work on many projects.
Employee Payroll System
ER diagram for music database
We first draw up a clear list of requirements for our database:
The collection consists of albums.
An album is made by exactly one artist.
An artist makes one or more albums.
An album contains one or more tracks
Artists, albums, and tracks each have a name.
Each track is on exactly one album.
Each track has a time length, measured in seconds.
When a track is played, the date and time the playback began (to the nearest second)
should be recorded; this is used for reporting when a track was last played, as well as the
number of times music by an artist, from an album, or a track has been played.
16. Consider the following scenario:
A University registrar’s office maintains data about the following entities: (a) Courses, including number, title,
credits, syllabus and prerequisites (b) Course offerings, including course number, year, semester, section
number, instructor, timings and class room (c) students, including student_id, name, and program and (d)
instructors, including identification number, name, department and title
i) Model an ER diagram for the above scenario
ii) Map the ER diagram you have modeled to relations.