UNIT3 DBMS Chapter5 Jicm
UNIT3 DBMS Chapter5 Jicm
Chapter 5
Relational Model in DBMS
INTRODUCTION:
E.F. Codd proposed the relational Model to model data in the form of
relations or tables.
4 SURESH DELHI 18
Important Terminologies
Attribute: Attributes are the properties that define an entity.
e.g.; ROLL_NO, NAME, ADDRESS
Relation Schema: A relation schema defines the structure of the relation
and represents the name of the relation with its attributes. e.g.; STUDENT
(ROLL_NO, NAME, ADDRESS, PHONE, and AGE) is the relation
schema for STUDENT. If a schema has more than 1 relation, it is called
Relational Schema.
Tuple: Each row in the relation is known as a tuple. The above relation
contains 4 tuples, one of which is shown as:
Relation Instance: The set of tuples of a relation at a particular instance
of time is called a relation instance. Table 1 shows the relation instance of
STUDENT at a particular time. It can change whenever there is an
insertion, deletion, or update in the database.
Degree: The number of attributes in the relation is known as the 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: The column represents the set of values for a particular
attribute. The column ROLL_NO is extracted from the relation STUDENT.
ROLL_NO
1. Candidate Key: The minimal set of attributes that can uniquely identify a
tuple is known as a candidate key. For Example, STUD_NO in STUDENT
relation.
It is a minimal super key.
It is a super key with no repeated data is called a candidate key.
The minimal set of attributes that can uniquely identify a record.
It must contain unique values.
It can contain NULL values.
Every table must have at least a single candidate key.
A table can have multiple candidate keys but only one primary key (the
primary key cannot have a NULL value, so the candidate key with a NULL
value can’t be the primary key).
The value of the Candidate Key is unique and may be null for a tuple.
There can be more than one candidate key in a relationship.
Example:
STUD_NO is the candidate key for relation STUDENT.
Table STUDENT
STUD_NO SNAME ADDRESS PHONE
The candidate key can be simple (having only one attribute) or composite
as well.
Example:
{STUD_NO, COURSE_NO} is a composite
candidate key for relation STUDENT_COURSE.
Table STUDENT_COURSE
STUD_NO TEACHER_NO COURSE_NO
1 001 C001
2 056 C005
3. Super Key: The set of attributes that can uniquely identify a tuple is
known as Super Key. For Example, STUD_NO, (STUD_NO, STUD_NAME),
etc. A super key is a group of single or multiple keys that identifies rows in a
table. It supports NULL values.
Adding zero or more attributes to the candidate key generates the super
key.
A candidate key is a super key but vice versa is not true.
Example:
Consider the table shown above.
STUD_NO+PHONE is a super key.
4. Alternate Key: The candidate key other than the primary key is called an
alternate key.
All the keys which are not primary keys are called alternate keys.
It is a secondary key.
It contains two or more fields to identify two or more records.
These values are repeated.
Eg:- SNAME, and ADDRESS is Alternate keys
Example:
Consider the table shown above.
STUD_NO, as well as PHONE both,
are candidate keys for relation STUDENT but
PHONE will be an alternate key
(only one out of many candidate keys).
5. Foreign Key: If an attribute can only take the values which are present as
values of some other attribute, it will be a foreign key to the attribute to which
it refers. The relation which is being referenced is called referenced relation
and the corresponding attribute is called referenced attribute the relation
which refers to the referenced relation is called referencing relation and the
corresponding attribute is called referencing attribute. The referenced
attribute of the referenced relation should be the primary key to it.
It is a key it acts as a primary key in one table and it acts as
secondary key in another table.
It combines two or more relations (tables) at a time.
They act as a cross-reference between the tables.
For example, DNO is a primary key in the DEPT table and a non-key in
EMP
Example:
Refer Table STUDENT shown above.
STUD_NO in STUDENT_COURSE is a
foreign key to STUD_NO in STUDENT relation.
Table STUDENT_COURSE
STUD_NO TEACHER_NO COURSE_NO
1 005 C001
2 056 C005
It may be worth noting that, unlike the Primary Key of any given relation,
Foreign Key can be NULL as well as may contain duplicate tuples i.e. it need
not follow uniqueness constraint. For Example, STUD_NO in the
STUDENT_COURSE relation is not unique. It has been repeated for the first
and third tuples. However, the STUD_NO in STUDENT relation is a primary
key and it needs to be always unique, and it cannot be null.
123456789
01 Bikash Dutta
234456678
01 Bikash 6000000009
02 Paul 9000090009
01 Tuhin 9234567892
Explanation: In the above table, EID is the primary key, and the first and the
last tuple have the same value in EID ie 01, so it is violating the key
constraint.
3. Entity Integrity Constraints:
1. Entity Integrity constraints say that no primary key can take a NULL value,
since using the primary key we identify each tuple uniquely in a relation.
Example:
EID Name Phone
01 Bikash 9000900099
02 Paul 600000009
Explanation: In the above relation, EID is made the primary key, and the
primary key can’t take NULL values but in the third tuple, the primary key is
null, so it is violating Entity Integrity constraints.
4. Referential Integrity Constraints
1. The Referential integrity constraint is specified between two relations or
tables and used to maintain the consistency among the tuples in two
relations.
2. This constraint is enforced through a foreign key, when an attribute in
the foreign key of relation R1 has the same domain(s) as the primary key of
relation R2, then the foreign key of R1 is said to reference or refer to the
primary key of relation R2.
3. The values of the foreign key in a tuple of relation R1 can either take the
values of the primary key for some tuple in relation R2, or can take NULL
values, but can’t be empty.
Example:
EID Name DNO
01 Divine 12
02 Dino 22
04 Vivian 14
DNO Place
12 Jaipur
13 Mumbai
14 Delhi
Explanation: In the above tables, the DNO of Table 1 is the foreign key, and
DNO in Table 2 is the primary key. DNO = 22 in the foreign key of Table 1 is
not allowed because DNO = 22 is not defined in the primary key of table 2.
Therefore, Referential integrity constraints are violated here.
1 Badminton
2 Cricket
2 Badminton
4 Badminton
Table 2: EMPLOYEE
EMP_NO NAME ADDRESS PHONE AGE
Table 3: STUDENT
ROLL_NO NAME ADDRESS PHONE AGE
Extract students whose age is greater than 18 from STUDENT relation given
in Table 3
σ (AGE>18)(STUDENT)
[Note: SELECT operator does not show any result, the projection operator
must be called before the selection operator to generate or project the result.
So, the correct syntax to generate the result is: ∏(σ (AGE>18)(STUDENT))]
RESULT:
ROLL_NO NAME ADDRESS PHONE AGE
ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH
Note: If the resultant relation after projection has duplicate rows, it will be
removed. For Example ∏(ADDRESS)(STUDENT) will remove one duplicate row
with the value DELHI and return three rows.
Cross Product(X): Cross product is used to join two relations. For every row
of Relation1, each row of Relation2 is concatenated. If Relation1 has m
tuples and and Relation2 has n tuples, cross product of Relation1 and
Relation2 will have m X n tuples.
Syntax:
Relation1 X Relation2
To apply Cross Product on STUDENT relation given in Table 1 and
STUDENT_SPORTS relation given in Table 2,
STUDENT X STUDENT_SPORTS
RESULT:
ROLL_N AG ROLL_N
NAME ADDRESS PHONE SPORTS
O E O
945512345 Badminto
1 RAM DELHI 18 1
1 n
945512345
1 RAM DELHI 18 2 Cricket
1
945512345 Badminto
1 RAM DELHI 18 2
1 n
945512345 Badminto
1 RAM DELHI 18 4
1 n
915625313 Badminto
3 SUJIT ROHTAK 20 1
1 n
915625313
3 SUJIT ROHTAK 20 2 Cricket
1
915625313 Badminto
3 SUJIT ROHTAK 20 2
1 n
915676897 Badminto
4 SURESH DELHI 18 1
1 n
915676897
4 SURESH DELHI 18 2 Cricket
1
915676897 Badminto
4 SURESH DELHI 18 2
1 n
915676897 Badminto
4 SURESH DELHI 18 4
1 n
Intersection:
Suppose there are two relations R and S. The set intersection operation
contains all tuples that are in both R & S.
o It is denoted by intersection ∩.
Syntax: R ∩ S
STUDENT ∩ EMPLOYEE
Result: