DBMS_Unit2
DBMS_Unit2
Introduction : This model uses the concept of mathematical relation that looks like a
table. This relation is a basic building block of this model. So this relational model
represents a database as a collection of relations. In the formal relational model, row is
called a tuple, a column is called an attribute and a table is called as a relation.
Tuple:
Each row in a table represents a record and is called a tuple .A table containing ‘n’
attributes in a record is called is called n-tuple.
Attributes:
The name of each column in a table is used to interpret its meaning and is called an
attribute.Each table is called a relation.
In the above table, account_number, branch name, balance are the attributes.
Domain : A domain D is a set of atomic values. It means that an each value in the
domain is indivisible as far as relational model is concerned. A common method to
specify a domain is to specify a data type. For example emp_age, phone_num, etc.
Relation:
A relation consist of
o Relational schema
o Relation instance
Relation : A relation r of a relation schema R(A1, A2, A3,……,An) is denoted by r(R), is a set
of n-tuples. That is r = {t1, t2, t3,…..,tn}. Each tuple t is an ordered list of n values i.e t = <v1,
v2, v3,…..,vn> where vi is element of dom (Ai) where 1 ≤ I ≤ n.
DBMS
2|U n it- II
Relational schema:
A relational schema specifies the relation’ name, its attributes and the domain
of each attribute. If R is the name of a relation and A1,A2,… and is a list of
attributes representing R then R(A1,A2,…,an) is called a relational schema.
Each attribute in this relational schema takes a value from some specific
domain called domain(Ai).
Example:
PERSON(PERSON_IDinteger,NAME:
STRING,AGE:INTEGER,ADDRESS:string)
Total number of attributes in a relation denotes the degree of a relation.since the
PERSON relation schemea contains four attributes ,so this relation is of degree 4.
Relation Instance:
A relational instance denoted as r is a collection of tuples for a given relational
schema at a specific point of time.
A relation state r to the relations schema R(A1,A2…,An) also denoted by r® is a
set of n-tuples
R{t1,t2,…tm}
DBMS
3|U n it- II
Relation instance:
Student:
Relational Model Constraints : In relational database, there may be many relations and
tuples of those relations are related in various ways. Generally there are many restrictions on
actual values in the tuples of database. They are called as constraints. These constraints on
databases can generally divided into 3 types. They are
Inherent model based or Implicit constraints : These are the constraints that are
inherent in the data model.
Schema based or Explicit constraints : These are the constraints that can be directly
expressed in schemas of data model. These can be specified in DDL.
Application based or Semantic constraints : These are the constraints that can’t be
expressed directly in schemas of data model. So these must be expressed and enforced by
application programs.
DBMS
4|U n it- II
There are three types of constraints on relational database of schema based constraints that
include
o DOMAIN CONSTRAINTS
o KEY CONSTRAINTS
o INTEGRITY CONSTRAINTS
These include
Domain Constraints : These constraints can specify that within each tuple, the value of
each attribute A must be atomic from the dom(A).
Key Constraints : These constraints can specify that no two tuples can have the same
value for their attributes. So there must be an attribute or set of attributes with property
that they should have unique values for all the tuples. i.e t1[sk] ≠ t2[sk] where sk is a
subset of attributes. Any such set of attributes sk is called super key. There are certain
keys that we can specify for a relation. They are
Super key : It is an attribute or set of attributes that uniquely identifies a tuple
within the relation.
Candidate Key : it is a minimal super key i.e., it is a super key with no proper
subset itself a super key.
Primary Key : It is a candidate key that was selected to identify tuples uniquely
within a relation.
Alternative Key : It is also a candidate key that was not selected to be primary
key. So these are also referred as secondary key.
Foreign Key : It is an attribute or set of attributes within one relation that should
match a candidate key of some other relation.
Entity Integrity Constraint : It states that every relation should have a primary
DBMS
5|U n it- II
key and its value can not be NULL. This is because Primary Key value is used to
identify individual tuples in a relation. So having NULL value for Primary Key
we can’t identify some typles.
Referential Integrity Constraint : It is specified between tow relations and it is
used to maintain consistency among tuples in the relations. However entity
integrity constraint can be specified on a single relation. This referential constraint
states that a tuple in one relation must refer to an existing tuple in another relation.
To define this constraint we use foreign key concept. So when we set a foreign
key between R1 and R2 then foreigh key of R1 refers to Primary key of R2 if it
satisfies the following conditions.
o Attributes of Foreign key have the same domain of Primary Key.
o The value of Foreign Key is either a value of Primary Key or NULL.
DBMS
6|U n it- II
Introduction : These are the two formal languages for relational model of data. The language
by which we can communicate with database is known as query language. This query language
can be either non-procedural or Procedural. Relational algebra is procedural language and
relational calculus is non-procedural language.
Traditional Set Operations : These can be further categorized into unary and binary
operations. Unary operations are the operations that can be operated on only one (single)
relation. However binary operations are those operations that can be operated on two
relations.
Unary Operations : These operations are
SELECT Operation : It is used to select subset of tuples from a relation that satisfies a
selection condition i.e., this operation results only those tuples that satisfy specified
condition. So it will produce horizontal partition (subset) of a relation. In general, this
operation is denoted by σ as follows :
DBMS
7|U n it- II
σ <select condition>(R)
where the symbol σ (sigma) is used to denote SELECT operation and <select condition>
is a Boolean expression specified on the attributes of relation R.
For Example : Select emps whose deptno is 4 or those whose salary is greater than
30,000
PROJECT Operation : This is used to select certain attributes from a relation while
discarding other attributes. Therefore the result of this operation can be visualized as
RENAME Operation : This can be used to rename a relation or attribute name or both.
This is denoted by ρ as follows:
1. ρ S(B1,B2,B3,….,Bn) (R)
2. ρ S (R)
3. ρ (B1,B2,B3,….,Bn) (R)
where ρ (read as rho) is the symbol used to denote rename operation. S is new relation
name and B1, B2, B3,…,Bn are new attribute names. The first form renames both
relation and attributes, 2nd form renames only the relation and 3rd form renames only the
attribute name of the relation R.
Binary Operations : These are the operations that can be applied on two relations. These
are related to mathematical set operations. The relations must be union compatible to
DBMS
8|U n it- II
apply these operations. Two relations R(A1, A2, A3,,….,An) and S(B1, B2, B3,….,Bn)
are said to be union compatible if they have same degree ‘n’ and dom(Ai)=dom(Bi) for
all i, 1 ≤ i ≤ n. These operations are given below :
UNION (U) : This operation between two relations R and S can be denoted by RUS and
it results all the tuples that are either in R or in S or in both R and S. However duplicate
tuples are eliminated.
For Example : Find the emps who have either salary >2000 or belong to computer
science dept.
Query : Π ENAME (σ (sal>2,000) (EMP)) UNION Π ENAME (σ (DNAME=’CS’)
(EMP_DEPT))
INTERSECTION (∩) : This operation between two relations R and S can be denoted by
R ∩ S and it results all the tuples that are in both R and S only. However duplicate tuples
are eliminated.
For Example : Find the emps whose salary >2000 and belong to computer science dept.
Query : Π ENAME (σ (sal>2,000) (EMP)) INTERSECTION Π ENAME (σ (DNAME=’CS’)
(EMP_DEPT))
SET DIFFERENCE (MINUS) : This operation is also referred as MINUS operation.
This operation between two relations R and S can be denoted by R - S and it results all
the tuples that are in R and but not in S.
For Example : Find the department numbers that don’t have employees.
DBMS
9|U n it- II
SPECIAL RELATIONAL OPERATIONS : In addition to traditional set operations, relational algebra
also supports some special operations which include
JOIN Operation( ) : This operation can be used to combine related tuples from two relations into single
tuples. This operation between two relations R and S is denoted by R
S. This operation not only include cartisian product but also includes a selection operation on the
result of cartisian product. This can be represented as follows :
Example : R <selection condition> S.
DIVISION Operation (÷) : This is special kind of operation which is denoted by R÷S. In general this
operation is applied on two relations R(Z)S(X) where X ⊆ Z, then it results the set of attributes Y of R
that are not arributes of S ( Y= Z – X).
Ex : Table P Table Q:
B
A B b1
a1 b1 b2
a1 b1
a2 b1
a2 b3
a3 b2
a4 b1
Then P ÷ Q is given as
A
a1
DBMS
10 | U n i t - I I
Example Queries
CN(BN=“Downtown”(depositor account))
CN(BN=“Uptown”(depositor account))
Query 2
customer-name, branch-name (depositor account)
temp(branch-name) ({(“Downtown”), (“Uptown”)})
Example Queries
DBMS
11 | U n i t - I I
Example Queries
n Find the loan number for each loan of an amount greater than
$1200
DBMS
12 | U n i t - I I
Example Queries
DBMS
13 | U n i t - I I
Example Queries
Find the names of all customers who have a loan at the Perryridge
branch.
customer-name (branch-name=“Perryridge”
(borrower.loan-number = loan.loan-number(borrower x loan)))
Find the names of all customers who have a loan at the Perryridge
branch but do not have an account at any branch of the bank.
DBMS
14 | U n i t - I I
Example Queries
n Find the names of all customers who have a loan at the Perryridge
branch.
Query 1
customer-name(branch-name = “Perryridge” (
borrower.loan-number = loan.loan-number(borrower x loan)))
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = “Perryridge”(loan)) x borrower))
Example Queries
Find the largest account balance n Rename account
relation as d n The query is:
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
DBMS
15 | U n i t - I I
Aggregate functions:
7
7
3
10
sum-C
g sum(c)( r ) 27
DBMS
16 | U n i t - I I
OUTER JOIN:
The outer join operation is an extension of the join operation to deal with missing
information.
There are three forms of outer join
left outer join
right outer join
full outer join
employee:
Empname Street City
Coyote Toon Hollywood
Rabbit Tunnel carrot
Smith Revolver Death valley
William Seaview Seattle
Ft_works:
Empname Branch name Salary
Coyote Mesa 1500
Rabbit Mesa 1300
Gates Redmond 5300
William Redmond 1500
Employee ft_works
DBMS
17 | U n i t - I I
Result of Employee ft_works
DBMS
18 | U n i t - I I
RELATIONAL CALCULUS
Introduction : It is also formal query language for relational model. This is non procedural
language because it specifies what is to be retrieved rather than how to retrieve it. In this, query
is written as formula that comprises variables. Generally Tuple calculus and Domain calculus are
collectively called as Ralational Calculus.
Tuple Calculus : It is based on specifying a number of tuple variables. Each type variable
ranges over a particular relation. It means that variable may take its value as any individual tuple
from that relation. A simple tuple relational calculus query is in the form of
{t | COND(t)}
Where ‘t’ is a tuple variable and COND(t) is conditional expression involving ‘t’. The result is
set of tuples that satisfy COND(t).
Quantifiers : Sometimes we need to use two special symbols in the formulas, called
Quantifiers. They are
1. Existential quantifier, denoted by symbol (∃)
2. Universal quantifier, denoted by symbol (∀)
Existential Quantifier : If ‘F’ is a formula then (∃t) (F) is true if ‘F’ evaluates true for some
(atleast) tuple assigned to free occurrence of t in F, otherwise (∃t) (F) is false.
For Example : Find employees whose salary >5000
{ t | (∃t)(EMP(t) and t.sal>5000}
Universal Quantifier: If ‘F’ is a formula then (∀t) (F) is true if F evaluates to true for every
tuple assigned to free occurrence of t in F, otherwise (∀t)(F) is false.
For Example : Find employees in department number 10.
DBMS
19 | U n i t - I I
Free and Bound Variables : A tuple variable is said to be bound variable when it is quantified
by a quantifier i.e., when it is used as (∃t) or (∀t), otherwise it is free variable.
Example 1 : List name, address of all employee who work for the ‘research’ department.
{ e.ename | EMP(e) and ((∃x) (∃w)(PROJECT (x) and WORK_ON(w) and x.dno=
5 and w.eno= e.no and x.pno = w.pno))}
Domain Calculus : There is another type of relational calculus, called Domain Relational
Calculus, simply Domain Calculus. This domain calculus differ from tuple calculus in only the
type of variable used in the formulas i.e., the variables in domain calculus range over the values
of domain of attributes rather than over tuple values. A simple domain calculus query is in the
form of
Where x1, x2, x3,…..,xn, xn+1, xn+2,….., xn+m are the domain variables in which the desired
attributes are x1, x2, x3,……..,xn , COND is a condition or formula of domain calculus.
DBMS
20 | U n i t - I I
For Example 1 : List birth date and address of employees whose name is John B Smith.
{ uv | (∃q) (∃r) (∃s) (∃t) (∃w) (∃x) (∃y) (∃z) (EMP(qrstuvwxyz) and q=’John’ and
r=’B’ and s =’Smith’ )}
Example 2 : Retrieve the name, address of all employees who work in the research dept.
{ qsv | (∃z) (∃l) (∃m) (EMP(qrstuvwxyz) and DEPT (lmno) and l= ‘research’ andm = z) }
Here the condition relating two domain variables from two relations such as m = z is
referred a join condition and condition that relates a domain variable to constant like l=
‘research’ is called selection condition.
DBMS