0% found this document useful (0 votes)
37 views8 pages

DBMS Module 2

The document discusses the relational model and its key concepts like relations, attributes, tuples, domains, keys, and constraints. It also introduces various relational algebra operators like selection, projection, join, union, difference etc and provides examples of each.

Uploaded by

vigneshdev2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views8 pages

DBMS Module 2

The document discusses the relational model and its key concepts like relations, attributes, tuples, domains, keys, and constraints. It also introduces various relational algebra operators like selection, projection, join, union, difference etc and provides examples of each.

Uploaded by

vigneshdev2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATABASE MANAGEMENT SYSTEM

MODULE 2

What is Relational Model?


Relational Model represents how data is stored in Relational Databases. A relational database stores
data in the form of relations (tables). Consider a relation STUDENT with attributes ROLL_NO,
NAME, ADDRESS, PHONE and AGE as :
STUDENT
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 9847118991 18

IMPORTANT TERMINOLOGIES
• Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME.

• Domain of an attribute :Possible values ,an attribute can take in a relation.eg : domain of
AGE of STUDENT relation is from 18 to 40.

• Relation Schema: A relation schema represents name of the relation with its attributes. e.g:
STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation schema for
STUDENT.

• Tuple: Each row in the relation is known as tuple. The above relation contains 4 tuples, one
of which is shown as:
1 RAM DELHI 9455123451 18

• Relation Instance: The set of tuples of a relation at a particular instance of time is called as
relation instance. Above Table shows the relation instance of STUDENT at a particular time.
It can change whenever there is insertion, deletion or updation in the database.

• Degree: The number of attributes in the relation is known as degree of the relation. The
STUDENT relation defined above, has degree 5.

• Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT


relation defined above has cardinality 4.

• Column: Column represents the set of values for a particular attribute. The column
ROLL_NO is extracted from relation STUDENT as :
ROLL_NO
1
2
3
4

CONSTRAINTS IN RELATIONAL MODEL

While designing Relational Model, we define some conditions which must hold for data present in
database called Constraints. These constraints are checked before performing any operation
(insertion, deletion and updation) in database. If there is a violation in any of constraints, operation
will fail.
A). Domain Constraints:
These are attribute level constraints. An attribute can only take values which lie inside the domain
range. eg: If a constraint AGE>0 is applied on STUDENT relation, inserting negative value of AGE
will result in failure.
B). Key Integrity:
Every relation in the database should have atleast one set of attributes which defines a tuple
uniquely. Those set of attributes is called key. e.g: ROLL_NO in STUDENT is a key. No two
students can have same roll number. So a key has two properties:
• It should be unique for all tuples.
• It can’t have NULL values.
C). Referential Integrity:
When one attribute of a relation can only take values from other attribute of same relation or any
other relation, it is called referential integrity. Let us suppose we have 2 relations.
STUDENT
ROLL_NO NAME ADDRESS PHONE AGE BRANCH_CODE
1 RAM DELHI 9455123451 18 CS
2 RAMESH GURGAON 9652431543 18 CS
3 SUJIT ROHTAK 9156253131 20 ECE
4 SURESH DELHI 9847118991 18 IT

BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
ECE ELECTRONICS AND COMMUNICATION ENGINEERING
CV CIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present in BRANCH_CODE
of BRANCH which is called referential integrity constraint. The relation which is referencing to
other relation is called Referencing relation (STUDENT in this case) and the relation to which other
relations refer is called Referenced relation (BRANCH in this case).

INTRODUCTION OF RELATIONAL ALGEBRA IN DBMS


Relational Algebra is procedural query language, which takes Relation as input and generate
relation as output. Relational algebra mainly provides theoretical foundation for relational databases
and SQL.

Operators in Relational Algebra


1). Projection (π)
Projection is used to project required column data from a relation.
Example :

R = (A B C)
----------
1 2 4
2 2 3
3 2 3
4 3 4

π (BC)

B C
-----
2 4
2 3
3 4

Note: By Default, projection removes duplicate data.


2). Selection (σ)
Selection is used to select required tuples of the relations. For the above relation,
σ (c>3)R will select the tuples which have c more than 3.
Note: selection operator only selects the required tuples but does not display them. For displaying,
data, projection operator is used.
For the above selected tuples to be displayed, we need to use projection also.
π (σ (c>3)R ) will show following tuples.

A B C
-------
1 2 4
4 3 4
3). Union (U)
Union operation in relational algebra is same as union operation in set theory. Both relation must
have same set of Attributes. 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.
• It eliminates the duplicate tuples. It is denoted by ∪.
• Notation: R ∪S
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).Intersection (∩)
Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in
both R & S.
• 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). Rename (ρ)


Rename is a unary operation used for renaming attributes of a relation.
ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’.
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)

6). Cross Product ( X )


Cross product between two relations , lets say A and B, so cross product, A X B will results all the
attributes of A followed by each attribute of B. Each record of A will pairs with every record of B.
Eg:
A B
(Name Age Sex ) (Id Course)
------------------ -------------
Ram 14 M 1 DS
Sona 15 F 2 DBMS
kim 20 M

A X B
Name Age Sex Id Course
---------------------------------
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS

Note: if A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘n*m’ tuples.
7).Natural Join (⋈)
Natural join is a binary operator. Natural join between two or more relations will result set of all
combination of tuples where they have equal common attribute.Eg:

Emp Dep
(Name Id Dept_name ) (Dept_name Manager)
------------------------ ---------------------
A 120 IT Sale Y
B 125 HR Prod Z
C 110 Sale IT A
D 111 IT

Emp ⋈ Dep

Name Id Dept_name Manager


-------------------------------
A 120 IT A
C 110 Sale Y
D 111 IT A

8).Conditional Join
Conditional join works similar to natural join. In natural join, by default condition is ‘equal’
between common attribute. While in conditional join, we can specify any condition such as greater
than, less than, not equal. Eg:
R S
(ID Sex Marks) (ID Sex Marks)
------------------ --------------------
1 F 45 10 M 20
2 F 55 11 M 22
3 F 60 12 M 59

Join between R And S with condition R.marks >= S.marks

R.ID R.Sex R.Marks S.ID S.Sex S.Marks


-----------------------------------------------
1 F 45 10 M 20
1 F 45 11 M 22
2 F 55 10 M 20
2 F 55 11 M 22
3 F 60 10 M 20
3 F 60 11 M 22
3 F 60 12 M 59

9).Set Difference (-)


Set Difference in relational algebra is same set difference operation as in set theory.
Suppose there are two tuples R and S. The set intersection operation contains all tuples that are in R
but not in S.
• 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

10). Divide ( ÷)
Division operator A÷B can be applied if and only if:
• Attributes of B is proper subset of Attributes of A.
• The relation returned by division operator will have attributes = (All attributes of A – All
Attributes of B)
• The relation returned by division operator will return those tuples from relation A which are
associated to every B’s tuple.
Consider the relation STUDENT_SPORTS and ALL_SPORTS
STUDENT_SPORTS
ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton

ALL_SPORTS
SPORTS
Badminton
Cricket

STUDENT_SPORTS ÷ ALL_SPORTS
• The operation is valid as attributes in ALL_SPORTS is a proper subset of attributes in
STUDENT_SPORTS.
• The attributes in resulting relation will have attributes {ROLL_NO,SPORTS}-
{SPORTS}=ROLL_NO
• The tuples in resulting relation will have those ROLL_NO which are associated with all B’s
tuple {Badminton, Cricket}. ROLL_NO 1 and 4 are associated to Badminton only.
ROLL_NO 2 is associated to all tuples of B. So the resulting relation will be:
ROLL_NO
2

You might also like