0% found this document useful (0 votes)
10 views

DBMS_Unit2

Uploaded by

anju143guna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DBMS_Unit2

Uploaded by

anju143guna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1|U n it- II

RELATIONAL DATA MODEL

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.

Domain, attributes tuples and relational:

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.

Relational Database Schema : A relational database schema S is a set of relation schemas


S={R1, R2, R3,…..,Rn} and set of integrity constraints (ICs). For example

COMPANY ={ EMP, DEPT, PROJECT, WORKS_ON, DEPENDENT}

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

Where each n-tuple is an ordered list of n values T=<v1,v2,….vn>


Where each vi belongs to domain (Ai) or contains null values.
The relation schema is also called ‘intension’ and the relation state is also called
‘extension’.
Eg:

Relation schema for student:


STUDENT(rollno:strinhg,name:string,city:string,age:integer)

Relation instance:
Student:

Rollno Name City Age


101 Sujit Bam 23
102 kunal Bbsr 22

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.

RELATIONAL CONSTRAINTS (SCHEMA based):

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.

 Integrity Constraints : There are two types of Integrity Constraints in this


model andthese constraints include

 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

RELATIONAL ALGEBRA & RELATIONAL CALCULUS

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.

Relational Algebra : It is a method of specifying a sequence of operations to be performed


on the database to compute the desired results. It consists of a set of operations that take one or
two relations as input and produce a new relation as result. Relational calculus provides a higher
level declarative notation for specifying relation queries. It specifies what information the result
should contain i.e., there is no order of operations to specify how to retrieve the query result. A
relational calculus produces a new relation which is specified in terms of variables that range
over rows (tuple calculus) or over columns (domain calculus). This is the main distinguishing
feature between relational algebra and relational calculus.

Relational Algebra : Actually it is considered to be an integral part of relational data model.


It is a collection of operations to manipulate the relations. It specifies the operations to be
performed on existing relations to derive resultant relations. The set of operations can be
classified into two types. They are

 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

Query : σ (dno = 4 or sal > 30,000) (EMP)

 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

vertical partition of a relation. In general, this operation can be denoted by π as follows :

Π <attribute list> (R)


Where π is the symbol used to represent PROJECT operation and attribute list is the
desired list of attributes from the relation R. The result has attribute list in the same order
they appear in the list.
For Example : To list each employee’s first name, last name and salary, we use
following

Query : Π lname, fname, sal (EMP)

 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.

Query : Π dno (DEPT) MINUS Π dno (EMP)


 CARTESIAN PRODUCT (X) : This operation between two relations R and S is
denoted by R X S and it results a relation that contains all possible combination of tuples
of both relations R and S. If ‘n’ is the number of tuples in R and ‘m’ is the number of
tuples in S then the resultant relation has n X m tuples. Usually this operation is used
along with a selection condition.
For Example : Find emp numbers who are in computer science department.

Π eno (σ (DNAME=’CS’) (EMP X EMP_DEPT))

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

n Find all customers who have an account from at least the


“Downtown” and the Uptown” branches.
Query 1

CN(BN=“Downtown”(depositor account)) 

CN(BN=“Uptown”(depositor account))

where CN denotes customer-name and BN denotes


branch-name.

Query 2
customer-name, branch-name (depositor account)
 temp(branch-name) ({(“Downtown”), (“Uptown”)})

Example Queries

n Find all customers who have an account at all branches located


in Brooklyn city.

customer-name, branch-name (depositor account)


 branch-name (branch-city = “Brooklyn” (branch))

DBMS
11 | U n i t - I I

branch (branch-name, branch-city, assets)

customer (customer-name, customer-street, customer-only)

account (account-number, branch-name, balance)

loan (loan-number, branch-name, amount)

depositor (customer-name, account-number)

borrower (customer-name, loan-number)

Example Queries

n Find all loans of over $1200

amount > 1200 (loan)

n Find the loan number for each loan of an amount greater than
$1200

loan-number (amount > 1200 (loan))

DBMS
12 | U n i t - I I

Example Queries

n Find the names of all customers who have a loan, an account, or


both, from the bank

customer-name (borrower)  customer-name (depositor)

n Find the names of all customers who have a loan and an


account at bank.

customer-name (borrower)  customer-name (depositor)

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.

customer-name (branch-name = “Perryridge”


(borrower.loan-number = loan.loan-number(borrower x loan))) –
customer-name(depositor)

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:

Aggregation function takes a collection of values and returns a single value as


a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1, G2, …, Gn g F1( A1), F2( A2),…,
Fn( An) (E)
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group (can be empty)
Each Fi is an aggregate function.
Each Ai is an attribute name

Find sum of sal fo all emp records


Gsum(sal) (emp)

Find maximum salalry from emp table


Gmax(salary) (emp)

Find branch name and maximum salalry from emp table


Branch_name G,max(salary) (emp)

Aggregate Operation – Example


n Relation r:
A B C

  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

Empname Street City Branch Salary


name
Coyote Toon Hollywood Mesa 1500
Rabbit Tunnel carrot Mesa 1300
William Seaview Seattle Redmond 1500

Left outer join:


It 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. The right relation
and adds them to the result of the natural join. In tuple (smith, Revolcer, Death
valley, null, null) is such a tuple. All information from the left relation is present
in the result of the left outer join.
Empname Street City Branch Salary
name
Coyote Toon Hollywood Mesa 1500
Rabbit Tunnel carrot Mesa 1300
William Seaview Seattle Redmond 1500
Smith Revolver Death valley Null null

DBMS
17 | U n i t - I I
Result of Employee ft_works

Right outer join:


It 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. tuple(Gates,null,null,Redmond,5300) is such a tuple. Thus, all
information from the right relation is present in the result of the right outer join.
Empname Street City Branch Salary
name
Coyote Toon Hollywood Mesa 1500
Rabbit Tunnel carrot Mesa 1300
William Seaview Seattle Redmond 1500
gates Null null Redmond 5300

Full outer join:


It 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.
Figure 3.35 shows the result of a full outer join.
Since outer join operations may generate results containing null values, we need
to specify how the different relation-algebra operations deal with null values. It is
interesting to note that the outer join operations can be expressed by the basic
relational algebra operations. For instance the left outer join operation
Employee ft_works

Empname Street City Branch Salary


name
Coyote Toon Hollywood Mesa 1500
Rabbit Tunnel carrot Mesa 1300
William Seaview Seattle Redmond 1500
Gates Null null Redmond 5300

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).

Example : Find the employee whose salary is > 5000

We can write a tuple calculus query for this as


{t | EMP(t) and t.sal>5000}
Example : Find the employee’s first and last names whose salary > 5000.
{t.fname, t.lname | EMP(t) and t.sal>5000}

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

{ t | (∀t) (EMP(t) and t.dno = 10) }

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.

Some Example Queries in Tuple Calculus :

Example 1 : List name, address of all employee who work for the ‘research’ department.

{ t.fname, t.lname, t.address | EMP(t) and (∃d) (DEPT(d) and d.dname=’research’


and d.dno=t.dno)}

Example 2 : List the employee names with their manager names.

{ e.ename, m.ename | EMP(e) and EMP(m) and e.mgr=m.eno}


Example 3 : List the name of employees who works on some project controlled by the dept = 5.

{ 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

{ x1, x2, x3,……..,xn | COND(x1,x2,x3,…..,xn,xn+1,xn+2,…..,xn+m}

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

You might also like