UNIT- II (Relational Data Model & Introduction to...
Transcript of UNIT- II (Relational Data Model & Introduction to...
Database Management System 1 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
UNIT- II (Relational Data Model & Introduction to SQL)
2.1 INTRODUCTION:
2.1.1 Database Concept:
2.1.2 Database Schema
2.2 INTEGRITY CONSTRAINTS:
2.2.1 Domain Integrity:
2.2.2 Entity Integrity Constraint:
2.2.3 Referential Integrity Constraint:
2.2.4 Foreign Key Integrity Constraint:
2.3 THE RELATIONAL ALGEBRA:
2.3.1 Select Operation:
2.3.2 The Project Operation
2.3.2 The Union Operation
2.3.3 The Set Difference Operation
2.3.4 The Cartesian-Product Operation
2.3.5 The Rename Operation
2.3.6 Additional Operations
2.3.6.1 The Set-Intersection Operation
3.3.6.2 The Natural-Join Operation
2.3.6.3 The Division Operation
2.3.6.4 The Assignment Operation
2.3.7 Extended Relational-Algebra Operations
2.3.7.1 Generalized Projection
2.3.7.2 Aggregate Functions
2.3.7.3 Outer Join
2.3.8 Modification of the Database
2.3.8.1 Deletion
2.3.8.2 Insertion
2.3.8.3 Updating
2.4 TUPLE RELATIONAL CALCULUS:
2.5 THE DOMAIN RELATIONAL CALCULUS:
Database Management System 2 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
2.1 INTRODUCTION:
2.1.1 Database Concept:
A relational database consists of a collection of tables, each of which is assigned a unique name. A
row in a table represents a relationship among a set of values. Since a table is a collection of such
relationships, there is a close correspondence between the concept of table and the mathematical
concept of relation, from which the relational data model takes its name.
A tuple variable is a variable that stands for a tuple; in other words, a tuple variable is a variable
whose domain is the set of all tuples. t[account-number] = “A-101,” and t[branch-name] =
“Downtown”. Alternatively, we may write t[1] to denote the value of tuple t on the first attribute
(account-number), t[2] to denote branch-name, and so on.
A domain is atomic if elements of the domain are considered to be indivisible units. For example,
the set of integers is an atomic domain, but the set of all sets of integers is a nonatomic domain.
2.1.2 Database Schema
Database schema can be defined as:
Account-schema = (account-number, branch-name, balance)
relation instance
Branch-schema = (branch-name, branch-city, assets)
Let R be a relation schema. If we say that a subset K of R is a superkey for R, we are restricting
consideration to relations r(R) in which no two distinct tuples have the same values on all
attributes in K. That is, if t1 and t2 are in r and t1≠ t2, then t1[K] ≠ t2[K].
• Strong entity set. The primary key of the entity set becomes the primary key of the relation.
• Weak entity set. The table, and thus the relation, corresponding to a weak entity set includes
The attributes of the weak entity set
The primary key of the strong entity set on which the weak entity set depends
• Relationship set. The union of the primary keys of the related entity sets becomes a superkey of
the relation.
Database Management System 3 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
2.2 INTEGRITY CONSTRAINTS:
Before one can start to implement the database tables, one must define the integrity constraints.
Intergrity means something like 'be right' and consistent. The data in a database must be right and
in good condition.
2.2.1 Domain Integrity:
Domain integrity means the definition of a valid set of values for an attribute. You define
data type, length or size is null value allowed is the value unique or not
for an attribute. You may also define the default value, the range (values in between) and/or
specific values for the attribute. Some DBMS allow you to define the output format and/or input
mask for the attribute. These definitions ensure that a specific attribute will have a right and proper
value in the database.
2.2.2 Entity Integrity Constraint:
The entity integrity constraint states that primary keys can't be null. There must be a proper value
in the primary key field. The entity integrity constraints assure that a specific row in a table can be
identified.
2.2.3 Referential Integrity Constraint:
The referential integrity constraint is specified between two tables and it is used to maintain the
consistency among rows between the two tables. The rules are:
1. You can't delete a record from a primary table if matching records exist in a related table.
2. You can't change a primary key value in the primary table if that record has related
records.
3. You can't enter a value in the foreign key field of the related table that doesn't exist in the
primary key of the primary table.
4. However, you can enter a Null value in the foreign key, specifying that the records are
unrelated.
2.2.4 Foreign Key Integrity Constraint:
There are two foreign key integrity constraints: cascade update related fields and cascade delete
related rows. These constraints affect the referential integrity constraint.
Database Management System 4 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
Cascade Update Related Fields
Any time you change the primary key of a row in the primary table, the foreign key values are
updated in the matching rows in the related table. This constraint overrules rule 2 in the referential
integrity constraints.
Cascade Delete Related Rows
Any time you delete a row in the primary table, the matching rows are automatically deleted in the
related table. This constraint overrules rule 1 in the referential integrity constraints.
2.3 THE RELATIONAL ALGEBRA:
The fundamental operations in the relational algebra are select, project, union, set difference,
Cartesian product, and rename. In addition to the fundamental operations, there are several other
operations—namely, set intersection, natural join, division, and assignment.
The select, project, and rename operations are called unary operations, because they
operate on one relation. The other three operations operate on pairs of relations and are, therefore,
called binary operations.
2.3.1 Select Operation:
We use the lowercase Greek letter sigma (σ) to denote selection.
We can find all tuples in which the amount lent is more than $1200 by writing
σamount>1200 (loan)
Database Management System 5 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
we allow comparisons using =, _=, <, ≤, >, ≥ in the selection predicate. Furthermore, we can combine
several predicates into a larger predicate by using the connectives and (∧), or (∨), and not (¬).
to find those tuples pertaining to loans of more than $1200 made by the Perryridge branch
σbranch-name =“Perryridge”∧∧∧∧ amount>1200 (loan)
2.3.2 The Project Operation
Suppose we want to list all loan numbers and the amount of the loans, but do not care about the
branch name. The project operation allows us to produce this relation.
The project operation is a unary operation that returns its argument relation, with certain
attributes left out.
Find those customers who live in Harrison
Πcustomer-name (σcustomer-city =“Harrison” (customer))
2.3.2 The Union Operation
Consider a query to find the names of all bank customers who have either an account or a loan or
both. We know how to find the names of all customers with a loan in the bank:
Πcustomer-name (borrower )
We also know how to find the names of all customers with an account in the bank:
Πcustomer-name (depositor)
To answer the query, we need the union of these two sets;
Πcustomer-name (borrower ) ∪∪∪∪ Πcustomer-name (depositor)
2.3.3 The Set Difference Operation
The set-difference operation, denoted by −, allows us to find tuples that are in one relation but are
not in another. The expression r − s produces a relation containing those tuples in r but not in s.
Πcustomer-name (depositor) − Πcustomer-name (borrower )
2.3.4 The Cartesian-Product Operation
The Cartesian-product operation, denoted by a cross (×), allows us to combine information
from any two relations. We write the Cartesian product of relations r1 and r2 as r1 × r2.
Database Management System 6 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
Suppose that we want to find the names of all customers who have a loan at the Perryridge branch
2.3.5 The Rename Operation
Find the largest account balance in the bank.
Step 1: To compute the temporary relation, we need to compare the values of all account balances.
We do this comparison by computing the Cartesian product account × account and forming a
selection to compare the value of any two balances appearing in one tuple.
Πaccount .balance (σaccount .balance < d.balance (account × ρd (account))).
The result contains all balances except the largest one.
Step 2: The query to find the largest account balance in the bank can be written as:
Πbalance (account) − Πaccount .balance (σaccount .balance < d.balance (account × ρd (account)))
Ex. Find the names of all customers who live on the same street and in the same city as Smith
Πcustomer.customer-name (σcustomer.customer-street=smith-addr.street ∧ customer.customer-
city=smith-addr.city (customer × ρsmith-addr(street,city) (Πcustomer-street, customer-city
(σcustomer-name = “Smith”(customer)))))
Database Management System 7 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
2.3.6 Additional Operations
2.3.6.1 The Set-Intersection Operation
Suppose that we wish to find all customers who have both a loan and an account.
Πcustomer-name (borrower ) ∩ Πcustomer-name (depositor)
Note that we can rewrite any relational algebra expression that uses set intersection by replacing
the intersection operation with a pair of set-difference operations as:
r ∩ s = r − (r − s)
3.3.6.2 The Natural-Join Operation
Find the names of all customers who have a loan at the bank, along with the loan number and the
loan amount.
Πcustomer-name, loan.loan-number, amount (σborrower .loan-number =loan.loan-number
(borrower × loan))
The natural join is a binary operation that allows us to combine certain selections and a Cartesian
product into one operation. It is denoted by the “join” symbol
“Find the names of all customers who have a loan at the bank, and find the amount of the loan
Πcustomer-name, loan-number, amount (borrower loan)
Find the names of all branches with customers who have an account in the bank and who live in
Harrison.
Πbranch-name(σcustomer-city =“Harrison” (customer account depositor)).
Find all customers who have both a loan and an account at the bank.
Πcustomer-name (borrower depositor )
2.3.6.3 The Division Operation
The division operation, denoted by ÷, is suited to queries that include the phrase “for all.”
Suppose that we wish to find all customers who have an account at all the branches located in
Brooklyn.
We can obtain all branches in Brooklyn by the expression
r1 = Πbranch-name (σbranch-city =“Brooklyn” (branch))
Database Management System 8 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
We can find all (customer-name, branch-name) pairs for which the customer has an account at a
branch by writing
r2 = Πcustomer-name, branch-name (depositor account)
Πcustomer-name, branch-name (depositor account) ÷ Πbranch-name (σbranch-city
=“Brooklyn” (branch))
2.3.6.4 The Assignment Operation
It is convenient at times to write a relational-algebra expression by assigning parts of it to
temporary relation variables. The assignment operation, denoted by ←, works like assignment in a
programming language.
temp1 ← ΠR−S (r)
temp2 ← ΠR−S ((temp1 × s) − ΠR−S,S(r))
result = temp1 − temp2
2.3.7 Extended Relational-Algebra Operations
2.3.7.1 Generalized Projection
The generalized-projection operation extends the projection operation by allowing arithmetic
functions to be used in the projection list. The generalized projection operation has the form
ΠF1,F2,...,Fn(E)
where E is any relational-algebra expression, and each of F1, F2, . . . , Fn is an arithmetic expression
involving constants and attributes in the schema of E.
If we want to find how much more each person can spend, we can write the following expression:
Πcustomer-name, (limit − credit-balance) as credit-available (credit-info)
Database Management System 9 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
2.3.7.2 Aggregate Functions
Aggregate functions take a collection of values and return a single value as a result.
The general form of the aggregation operation G is as follows:
G1,G2,...,GnGF1(A1), F2(A2),..., Fm(Am)(E)
where E is any relational-algebra expression; G1,G2, . . .,Gn constitute a list of attributes on which to
group; each Fi is an aggregate function; and each Ai is an at tribute name.
we shall use the pt-works relation in above example, for part-time employees.
Suppose that we want to find out the total sum of salaries of all the part-time employees in the
bank.
Gsum(salary)(pt-works)
Find the number of branches appearing in the pt-works relation.
Gcount-distinct(branch-name)(pt-works)
Suppose we want to find the total salary sum of all part-time employees at each branch of the bank
separately, rather than the sum for the entire bank.
branch-nameGsum(salary)(pt-works)
if we want to find the maximum salary for part-time employees at each branch
branch-nameGsum(salary),max(salary)(pt-works)
Database Management System 10 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
attributes of an aggregation operation can be renamed as:
branch-nameGsum(salary) as sum-salary,max(salary) as max-salary (pt-works)
2.3.7.3 Outer Join
The outer-join operation is an extension of the join operation to deal with missing information.
Suppose that we have the relations with the following schemas, which contain data on full-time
employees:
employee (employee-name, street, city)
ft-works (employee-name, branch-name, salary)
The left outer join ( ) takes all tuples in the left relation that did not match with any tuple in the
right relation, pads the tuples with null values for all other attributes from the right relation, and
adds them to the result of the natural join.
Database Management System 11 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
The right outer join ( ) is symmetric with the left outer join: It pads tuples from the right relation
that did not match any from the left relation with nulls and adds them to the result of the natural
join.
The full outer join does both of those operations, padding tuples from the left relation that did not
match any from the right relation, as well as tuples from the right relation that did not match any
from the left relation, and adding them to the result of the join.
2.3.8 Modification of the Database
2.3.8.1 Deletion
we remove the selected tuples from the database. We can delete only whole tuples; we cannot
delete values on only particular attributes. In relational algebra a deletion is expressed by r ← r − E
where r is a relation and E is a relational-algebra query.
Delete all of Smith’s account records.
depositor ← depositor − σcustomer-name =“Smith” (depositor)
Delete all loans with amount in the range 0 to 50.
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham.
r1 ← σbranch-city =“Needham” (account � branch)
r2 ← Πbranch-name, account -number, balance (r1)
account ← account − r2
Database Management System 12 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
2.3.8.2 Insertion
r ← r ∪ E
where r is a relation and E is a relational-algebra expression.
Suppose that we wish to insert the fact that Smith has $1200 in account A-973 at the Perryridge
branch.
account←account ∪ {(A-973, “Perryridge”, 1200)}
depositor←depositor ∪ {(“Smith”, A-973)}
2.3.8.3 Updating
If we want to select some tuples from r and to update only them, we can use the following
expression; here, P denotes the selection condition that chooses which tuples to update:
r ← ΠF1,F2,...,Fn(σP (r)) ∪ (r − σP (r))
To illustrate the use of the update operation, suppose that interest payments are being made, and
that all balances are to be increased by 5 percent.
account ← Πaccount-number, branch-name, balance * 1.05 (account)
2.4 TUPLE RELATIONAL CALCULUS:
The tuple relational calculus, by contrast, is a nonprocedural query language. It describes the
desired information without giving a specific procedure for obtaining that information. A query in
the tuple relational calculus is expressed as
{t | P(t)}
that is, it is the set of all tuples t such that predicate P is true for t. Following our earlier notation, we
use t[A] to denote the value of tuple t on attribute A, andwe use t ∈ r to denote that tuple t is in
relation r.
∃ t ∈ r (Q(t)) means “there exists a tuple t in relation r such that predicate Q(t) is true.”
Say that we want to find the branch-name, loan-number, and amount for loans of over $1200:
{t | t ∈∈∈∈ loan ∧∧∧∧ t[amount] > 1200}
Find the loan number for each loan of an amount greater than $1200”
{t | ∃∃∃∃ s ∈∈∈∈ loan (t[loan-number] = s[loan-number] ∧∧∧∧ s[amount] > 1200)}
Database Management System 13 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
To find all customers who have a loan, an account, or both at the bank (we used the union
operation in the relational algebra. In the tuple relational calculus, we shall need two “there exists”
clauses, connected by or (∨): )
{t | ∃∃∃∃ s ∈∈∈∈ borrower (t[customer-name] = s[customer-name])
∨∨∨∨∃∃∃∃ u ∈∈∈∈ depositor (t[customer-name] = u[customer-name])}
If we now want only those customers who have both an account and a loan at the bank, all we need
to do is to change the or (∨) to and (∧) in the preceding expression.
{t | ∃∃∃∃ s ∈∈∈∈ borrower (t[customer-name] = s[customer-name])
∧∧∧∧∃∃∃∃ u ∈∈∈∈ depositor (t[customer-name] = u[customer-name])}
Find all customers who have an account at the bank but do not have a loan from the bank.”
{t | ∃∃∃∃ u ∈∈∈∈ depositor (t[customer-name] = u[customer-name])
∧∧∧∧ ¬¬¬¬ ∃∃∃∃ s ∈∈∈∈ borrower (t[customer-name] = s[customer-name])}
The formula
P ⇒ Q means “P implies Q”; that is, “if P is true, then Q must be true.” Note that
P ⇒ Q is logically equivalent to ¬P ∨ Q.
Find all customers who have an account at all branches located in Brooklyn.
{t | ∃∃∃∃ r ∈∈∈∈ customer (r[customer-name] = t[customer-name]) ∧∧∧∧ ( ∀∀∀∀ u ∈∈∈∈ branch (u[branch-city] =
“ Brooklyn” ⇒⇒⇒⇒ ∃∃∃∃ s ∈∈∈∈ depositor (t[customer-name] = s[customer-name]
∧∧∧∧∃∃∃∃ w ∈∈∈∈ account (w[account-number] = s[account-number ]
∧∧∧∧ w[branch-name] = u[branch-name]))))}
three rules:
1. P1 ∧ P2 is equivalent to ¬ (¬(P1) ∨ ¬(P2)).
2. ∀ t ∈ r (P1(t)) is equivalent to ¬ ∃ t ∈ r (¬P1(t)).
3. P1 ⇒ P2 is equivalent to ¬(P1) ∨ P2.
Database Management System 14 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
2.5 THE DOMAIN RELATIONAL CALCULUS:
An expression in the domain relational calculus is of the form
{< x1, x2, . . . , xn > | P(x1, x2, . . . , xn)}
where x1, x2, . . . , xn represent domain variables. P represents a formula composed of atoms, as was
the case in the tuple relational calculus. An atom in the domain relational calculus has one of the
following forms:
• < x1, x2, . . . , xn > ∈ r, where r is a relation on n attributes and x1, x2, . . . , xn are domain variables
or domain constants.
• x Θ y, where x and y are domain variables and Θ is a comparison operator (<, ≤, =, _=, >, ≥). We
require that attributes x and y have domains that can be compared by Θ.
• x Θ c, where x is a domain variable, Θ is a comparison operator, and c is a constant in the domain
of the attribute for which x is a domain variable.
We build up formulae from atoms by using the following rules:
• An atom is a formula.
• If P1 is a formula, then so are ¬P1 and (P1).
• If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.
• If P1(x) is a formula in x, where x is a domain variable, then ∃ x (P1(x)) and ∀ x (P1(x)) are also
formulae.
Find the loan number, branch name, and amount for loans of over $1200:
{< l, b, a > | < l, b, a > ∈∈∈∈ loan ∧∧∧∧ a > 1200}
Find all loan numbers for loans with an amount greater than $1200:
{< l > | ∃∃∃∃ b, a (< l, b, a > ∈∈∈∈ loan ∧∧∧∧ a > 1200)}
Find the names of all customers who have a loan from the Perryridge branch and find the loan
amount:
{< c,a > | ∃∃∃∃ l (< c, l >∈∈∈∈ borrower
∧∧∧∧∃∃∃∃ b (< l, b, a > ∈∈∈∈ loan ∧∧∧∧ b = “Perryridge”))}
Database Management System 15 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
Find the names of all customers who have a loan, an account, or both at the Perryridge branch:
{< c > | ∃∃∃∃ l (< c, l >∈∈∈∈ borrower
∧∧∧∧∃∃∃∃ b, a (< l, b, a >∈∈∈∈ loan ∧∧∧∧ b = “Perryridge”))
∨∨∨∨∃∃∃∃ a (< c,a >∈∈∈∈ depositor
∧∧∧∧∃∃∃∃ b, n (< a, b, n >∈∈∈∈ account ∧∧∧∧ b = “Perryridge”))}
Find the names of all customers who have an account at all the branches located in Brooklyn:
{< c > | ∃∃∃∃ n (< c,n >∈∈∈∈ customer) ∧∧∧∧
∀∀∀∀ x, y, z (< x, y, z > ∈∈∈∈ branch ∧∧∧∧ y = “Brooklyn” ⇒⇒⇒⇒
∃∃∃∃ a, b (< a, x, b > ∈∈∈∈ account ∧∧∧∧ < c,a >∈∈∈∈ depositor))}
Database Management System 16 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
PRACTICE QUESTIONS
Problem # 1 Consider the schema:- given below, EMPLOYEE (E-NAME, STREET, CITY)
WORKS-FOR (E-NAME, COMPANY-NAME, SALARY) COMPANY (COMPANY-NAME , CITY) MANAGES (E-NAME, MANAGER-NAME) Express the following queries in relational algebra and SQL :- (a) Find names of employees working for Infosys.
RA: ∏∏∏∏E-NAME (σσσσCOMPANY-NAME = “Infosys” (works-for)) SQL: SELECT E-NAME FROM works-for WHERE COMPAY-NAME = ‘Infosys’; TRC: { t ∃ u ∈ works-for ( t[E-NAME] = u [E-NAME] ∧ u[COMPANY-NAME] =
“Infosys” )} DRC: { < en > ∃ sal ( < en , “Infosys” , sal> ∈ works-for) } (b) Find the names and the cities of residence of employees working for TCS RA: ∏∏∏∏E-NAME, CITY (σσσσCOMPANY-NAME = “TCS” (works_for * employee)) SQL: SELECT E.E-NAME, CITY FROM works-for W, employee E WHERE W. E-NAME = E. E-NAME AND COMPAY-NAME = ‘TC S’; TRC: { t ∃ u ∈ works-for ( t[E-NAME] = u [E-NAME] ∧ u[COMPANY-NAME] = “TCS”
∧ ∃ v ∈ employee ( v[E-NAME] = u [E-NAME] ∧ t[CITY] = v [CITY] ))} DRC: { < en, city > ∃ sal ( < en , “TCS” , sal> ∈ works-for
∧ ∃ st ( < en , st , city> ∈ employee )) }
(c) Find name, street and city of residence of employees working for Infosys and earning more than 20,000
Database Management System 17 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
RA: ∏E-NAME, STREET, CITY (σCOMPANY-NAME = “Infosys” ∧ SALARY > 20000 (works_for * employee)) SQL: SELECT E.E-NAME, STREET, CITY FROM works-for W, employee E WHERE W.E-NAME = E. E-NAME AND COMPAY-NAME = ‘Inf osys’
AND SALARY > 20000;
TRC: { t ∃ u ∈ works-for ( t[E-NAME] = u [E-NAME] ∧ u[COMPANY-NAME] = “infosys”
∧ u[SALARY] > 20000 ∧ ∃ v ∈ employee ( v[E-NAME] = u [E-NAME] ∧ t[STREET] = v [STREET]
∧ t[CITY] = v [CITY] ))}
DRC: { < en, st, city > < en , st , city> ∈ employee
∧ ∃ salary ( < en , “Infosys” , salary> ∈ works-for ∧ salary > 20000 ) }
(d) Find names of employees working in the same city where they live RA: ∏∏∏∏e-name, city (employee * works-for * company) SQL: SELECT E.E-NAME FROM works-for W, employee E, company C WHERE W. E-NAME = E. E-NAME
AND W. COMPANY-NAME = C. COMPAY-NAME AND E. CITY = C. CITY;
TRC: { t ∃ u ∈ employee ( t[E-NAME] = u [E-NAME]
∧ ∃ v ∈ works-for ( v[E-NAME] = u [E-NAME] ∧ ∃ w ∈ company ( w[COMPANY-NAME] = v [COMPANY-NAME] ∧ w[CITY] = u [CITY] ))) }
DRC: { < en > ∃ st, city ( < en , st , city> ∈ employee
∧ ∃ cn, salary ( < en , cn , salary> ∈ works-for ∧ < cn, city > ∈ company ))}
(e) Find the names of employees, who are not working for WIPRO
RA: ∏∏∏∏e-name (employee) - ∏∏∏∏e-name (σσσσcompany-name = “Wipro” (works-for)) SQL: SELECT E-NAME
FROM employee
Database Management System 18 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
MINUS SELECT E-NAME
FROM works-for WHERE COMPANY-NAME = ‘WIPRO’; TRC: { t ∃ u ∈ employee ( t[E-NAME] = u [E-NAME])
∧ ¬ ∃ v ∈ works-for ( t[E-NAME] = v [E-NAME] ∧ v[COMPANY-NAME] = “WIPRO” )}
DRC: { < en > ∃ st, city ( < en , st , city> ∈ employee ) ∧ ¬ ∃ sal ( <en, “WIPRO”, sal> ∈ works-for )} (f) Find the Total and Average Salary Paid by each Company.
RA: Company-Name G G G G MAX (SALARY), AVG (SALARY) (works-for) SQL: SELECT COMPANY-NAME, SUM (SALARY), AVG (SALARY)
FROM works-for GROUP BY COMPANY-NAME;
(g) Find the names of employees, who earn more than every employee of TCS
RA: MAX-TCS ←←←← G G G G MAX (SALARY) (σσσσCOMPANY-NAME = “TCS” (works-for)) ∏∏∏∏e-name(σσσσSALARY > MAX-TCS (works-for))
SQL: SELECT E-NAME
FROM works-for WHERE SALARY > ( SELECT MAX (SALARY) FROM works-for WHERE COMPANY-NAME = ‘TCS’); (h) Find names of employees, who live in the same street and city as their managers RA: emp ←←←← employee * manages
∏∏∏∏emp.E-NAME (σσσσemp.MANAGER-NAME= mgr.E-NAME ∧∧∧∧ emp.CITY =mgr.CITY ∧∧∧∧ emp.STREET
=mgr.STREET (emp x ρρρρmgr (emp)))
SQL: SELECT E.E-NAME FROM employee E, manages K, employee M
WHERE E. E-NAME = K. E-NAME AND K. MANAGER-NAME = M . E-NAME AND E. STREET = M. STREET AND E. CITY = M. CITY;
(i) Find the company with maximum number of employe es.
RA: r ←←←← COMPANY-NAME GGGG COUNT (E-NAME) AS NUMB (works-for)
Database Management System 19 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
Max-Numb ←←←← G G G G MAX (NUMB) (r) ∏∏∏∏COMPANY-NAME (σσσσNUMB = MAX-NUMB (r))
SQL: SELECT COMPANY-NAME
FROM works-for GROUP BY COMPANY-NAME HAVING COUNT (E-NAME) = ( SELECT MAX (COUNT (E-N AME)) FROM works-for GROUP BY COMPANY-NAME); (j) Find the Company with the smallest total salary paid to employees.
RA: r ←←←← COMPANY-NAME GGGG SUM (SALARY) AS TOTAL-SAL (works-for) MIN-TOTAL ←←←← G G G G MIN (TOTAL-SAL) (r) ∏∏∏∏COMPANY-NAME (σσσσTOTAL-SAL = MIN-TOTAL (r))
SQL: SELECT COMPANY-NAME
FROM works-for GROUP BY COMPANY-NAME HAVING SUM (SALARY) = ( SELECT MIN (SUM (SALA RY)) FROM works-for GROUP BY COMPANY-NAME); (k) Find the company whose employees earn more salary on the average than the average salary of TCS.
RA: r ←←←← COMPANY-NAME GGGG AVG (SALARY) AS AVG-SAL (works-for) AVG-TCS ←←←← ∏∏∏∏AVG-SAL (σσσσCOMPANY-NAME = “TCS” (r)) ∏∏∏∏ COMPANY-NAME (σσσσ AVG-SAL > AVG-TCS (r))
SQL: SELECT COMPANY-NAME
FROM works-for GROUP BY COMPANY-NAME HAVING AVG ( SALARY) >( SELECT AVG (SALARY) FROM works-for WHERE COMPANY-NAME = ‘TCS’); Problem # 2 Answer the following queries in RA, SQL, TRC and DRC for the schema given below:-
CUSTOMER (CUSTOMER-NAME, CUSTOMER-STREET, CUSTOMER-CITY) BRANCH (BRANCH-NAME , BRANCH-CITY, ASSETS) ACCOUNT (ACCOUNT-NUMBER , BRANCH-NAME, BALANCE) LOAN (LOAN-NUMBER , BRANCH-NAME, AMOUNT) DEPOSITOR (CUSTOMER-NAME, ACCOUNT-NUMBER ) BORROWER (CUSTOMER-NAME, LOAN-NUMBER )
Database Management System 20 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
(a) Find the names & cities of customers having a loan from the bank.
RA: ∏∏∏∏CUSTOMER-NAME, CUSTOMER-CITY (customer * borrower)
SQL: SELECT C. CUSTOMER-NAME, CUSTMER-CITY FROM customer C, borrower B WHERE C. CUSTOMER-NAME = B. CUSTMER-NAME; TRC: { t ∃ u ∈ customer ( t[CUSTOMER-NAME] = u [CUSTOMER-NAME] ∧ t[CUSTOMER-CITY] = u [CUSTOMER-CITY] ∧ v ∈ borrower ( v[CUSTOMER-NAME] = u [CUSTMER-NAME] )) } DRC: { < cn , cc > ∃ cs ( < cn , cs , cc> ∈ customer ∧ ∃ ln ( < cn , ln > ∈ borrower ))
}
(b) Find names of customers having account in all branches of “Noida” RA: ∏∏∏∏CUSTOMER-NAME, BRANCH-NAME (depositor * account) ÷÷÷÷∏∏∏∏BRANCH-NAME (σσσσBRANCH-CITY=”Noida” (branch)) SQL: SELECT D. CUSTOMER-NAME FROM depositor D
WHERE NOT EXISTS (( SELECT BRANCH-NAME
FROM branch WHERE BRANCH-CITY = ‘NOIDA’) MINUS (SELECT A.BRANCH-NAME FROM depositor K, account A WHERE K.CUSTOMER_NAME = D.CUSTOMER_NAME AND K.ACCOUNT_NUMBER = A.ACCOUNT_NUMBER));
(c) Find Average Balance at each branch
RA: BRANCH-NAME GGGG AVG (BALANCE) AS AVG-BAL (account )
SQL: SELECT BRANCH-NAME, AVG (BALANCE) FROM account GROUP BY BRANCH-NAME;
(d) Find the accounts held by more than two customers.
RA: r ←←←←ACCOUNT-NUMBER GGGG COUNT DISTINCT (CUSTOMER-NAME) AS NO-OF-CUST
(depositor ) ∏∏∏∏ACCOUNT-NUMBER (σσσσ NO-OF-CUST > 2 (r))
Database Management System 21 (NCS-502)
Prepared By: Dr. Shailender Kr. Gaur & Mr. Pawan Pandey RKGIT
SQL: SELECT ACCOUNT-NUMBER
FROM depositor GROUP BY ACCOUNT-NUMBER
HAVING COUNT(CUSTOMER-NAME) > 2;