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:
21
Embed
UNIT- II (Relational Data Model & Introduction to SQL)pclsoft.weebly.com/uploads/2/9/8/3/298350/unit_ii_dbms.pdfUNIT- II (Relational Data Model & Introduction to SQL) 2.1 INTRODUCTION:
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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.
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
(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
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
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.
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:-
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