DBMS Module 2
DBMS Module 2
MODULE 2
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.
• 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
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).
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
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
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
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
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