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

UNIT3 DBMS Chapter5 Jicm

Bca 2nd sem

Uploaded by

Jyotsna Yadav
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)
21 views

UNIT3 DBMS Chapter5 Jicm

Bca 2nd sem

Uploaded by

Jyotsna Yadav
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/ 18

Unit 3

Chapter 5
Relational Model in DBMS

INTRODUCTION:

E.F. Codd proposed the relational Model to model data in the form of
relations or tables.

After designing the conceptual model of the Database using ER diagram, we


need to convert the conceptual model into a relational model which can be
implemented using any RDBMS language like Oracle SQL, MySQL, etc.

So we will see what the Relational Model is.

What is the Relational Model?


The relational model represents how data is stored in Relational Databases.
A relational database consists of a collection of tables, each of which is
assigned a unique name. Consider a relation STUDENT with attributes
ROLL_NO, NAME, ADDRESS, PHONE, and AGE shown in the table.
Table 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 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:

1 RAM DELHI 9455123451 18


 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

 NULL Values: The value which is not known or unavailable is called a


NULL value. It is represented by blank space. e.g.; PHONE of STUDENT
having ROLL_NO 4 is NULL.
 Relation Key: These are basically the keys that are used to identify the
rows uniquely or also help in identifying tables. These are of the following
types.
 Primary Key
 Candidate Key
 Super Key
 Foreign Key
 Alternate Key
 Composite Key
Different Types of Keys in the Relational Model
1. Candidate Key
2. Primary Key
3. Super Key
4. Alternate Key
5. Foreign Key
6. Composite Key

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

1 Shyam Delhi 123456789

2 Rakesh Kolkata 223365796


STUD_NO SNAME ADDRESS PHONE

3 Suraj Delhi 175468965

 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

Note: In SQL Server a unique constraint that has a nullable


column, allows the value ‘null‘ in that column only once. That’s why the
STUD_PHONE attribute is a candidate here, but can not be a ‘null’ value in
the primary key attribute.
2. Primary Key: It is the first key used to identify one and only one instance
of an entity uniquely.
There can be more than one candidate key in relation out of which one can
be chosen as the primary key. For Example, STUD_NO, as well as
STUD_PHONE, are candidate keys for relation STUDENT but STUD_NO
can be chosen as the primary key (only one out of many candidate keys).
 It is a unique key.
 It can identify only one tuple (a record) at a time.
 It has no duplicate values, it has unique values.
 It cannot be NULL.
 Primary keys are not necessarily to be a single column; more than one
column can also be a primary key for a table.
Example:
STUDENT table -> Student(STUD_NO, SNAME,
ADDRESS, PHONE) , STUD_NO is a primary key
Table STUDENT
STUD_NO SNAME ADDRESS PHONE

1 Shyam Delhi 123456789

2 Rakesh Kolkata 223365796

3 Suraj Delhi 175468965

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.

Relation between Primary Key, Candidate Key, and 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).

Primary Key, Candidate Key, and Alternate Key

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.

Relation between Primary Key and Foreign Key


6. Composite Key: Sometimes, a table might not have a single
column/attribute that uniquely identifies all the records of a table. To uniquely
identify rows of a table, a combination of two or more columns/attributes can
be used. It still can give duplicate values in rare cases. So, we need to find
the optimal set of attributes that can uniquely identify rows in a table.
 It acts as a primary key if there is no primary key in a table
 Two or more attributes are used together to make a composite key.
 Different combinations of attributes may give different accuracy in terms
of identifying the rows uniquely.
Example:
FULLNAME + DOB can be combined
together to access the details of a student.

Different Types of Keys

Constraints on Relational database model


In DBMS (Database Management Systems), constraints are
guidelines or limitations imposed on database tables to
maintain the integrity, correctness, and consistency of the data.
Database Constraints can be categorized into 3 main categories:
1. Constraints that are applied in the data model are called Implicit
Constraints.
2. Constraints that are directly applied in the schemas of the data model, by
specifying them in the DDL(Data Definition Language). These are called
as Schema-Based Constraints or Explicit Constraints.
3. Constraints that cannot be directly applied in the schemas of the data
model. We call these Application based or Semantic Constraints.
So here we are going to deal with Implicit constraints.
Relational Constraints
These are the restrictions or sets of rules imposed on the database contents.
It validates the quality of the database. It validates the various operations like
data insertion, updation, and other processes which have to be performed
without affecting the integrity of the data. It protects us against
threats/damages to the database. Mainly Constraints on the relational
database are of 4 types
1. Domain constraints
2. Key constraints or Uniqueness Constraints
3. Entity Integrity constraints
4. Referential integrity constraints

Types of Relational Constraints

Let’s discuss each of the above constraints in detail.


1. Domain Constraints
1. Every domain must contain atomic values(smallest indivisible units) which
means composite and multi-valued attributes are not allowed.
2. We perform a datatype check here, which means when we assign a data
type to a column we limit the values that it can contain. Eg. If we assign
the datatype of attribute age as int, we can’t give it values other than int
datatype.
Example:
EID Name Phone

123456789
01 Bikash Dutta
234456678

Explanation: In the above relation, Name is a composite attribute and


Phone is a multi-values attribute, so it is violating domain constraint.
2. Key Constraints or Uniqueness Constraints
1. These are called uniqueness constraints since it ensures that every tuple
in the relation should be unique.
2. A relation can have multiple keys or candidate keys(minimal superkey), out of
which we choose one of the keys as the primary key, we don’t have any
restriction on choosing the primary key out of candidate keys, but it is
suggested to go with the candidate key with less number of attributes.
3. Null values are not allowed in the primary key, hence Not Null constraint
is also part of the key constraint.
Example:
EID Name Phone

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

NULL Sony 9234567892

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.

Anomalies in the Relational Model


An anomaly is an irregularity or something which deviates from the expected
or normal state. When designing databases, we identify three types of
anomalies: Insert, Update, and Delete.

Insertion Anomaly in Referencing Relation


We can’t insert a row in REFERENCING RELATION if referencing attribute’s
value is not present in the referenced attribute value. e.g.; Insertion of a
student with BRANCH_CODE ‘ME’ in STUDENT relation will result in an
error because ‘ME’ is not present in BRANCH_CODE of BRANCH.
Deletion/ Updation Anomaly in Referenced Relation:
We can’t delete or update a row from REFERENCED RELATION if the value
of REFERENCED ATTRIBUTE is used in the value of REFERENCING
ATTRIBUTE. e.g; if we try to delete a tuple from BRANCH having
BRANCH_CODE ‘CS’, it will result in an error because ‘CS’ is referenced by
BRANCH_CODE of STUDENT, but if we try to delete the row from BRANCH
with BRANCH_CODE CV, it will be deleted as the value is not been used by
referencing relation. It can be handled by the following method:
On Delete Cascade
It will delete the tuples from REFERENCING RELATION if the value used by
REFERENCING ATTRIBUTE is deleted from REFERENCED RELATION.
e.g.; For, if we delete a row from BRANCH with BRANCH_CODE ‘CS’, the
rows in STUDENT relation with BRANCH_CODE CS (ROLL_NO 1 and 2 in
this case) will be deleted.
On Update Cascade
It will update the REFERENCING ATTRIBUTE in REFERENCING
RELATION if the attribute value used by REFERENCING ATTRIBUTE is
updated in REFERENCED RELATION. e.g;, if we update a row from
BRANCH with BRANCH_CODE ‘CS’ to ‘CSE’, the rows in STUDENT relation
with BRANCH_CODE CS (ROLL_NO 1 and 2 in this case) will be updated
with BRANCH_CODE ‘CSE’.

Introduction of Relational Algebra


 Relational Algebra is a procedural query language.
 Relational algebra mainly provides a theoretical foundation for
relational databases and SQL.
 The main purpose of using Relational Algebra is to define operators
that transform one or more input relations into an output relation.
Fundamental Operators

These are the basic/fundamental operators used in Relational Algebra.


1. Selection(σ)
2. Projection(π)
3. Union(U)
4. Set Difference(-)
5. Set Intersection(∩)
6. Rename(ρ)
7. Cartesian Product(X)

There are some basic operators which can be applied in relation to


producing the required results which we will discuss one by one. We will use
STUDENT_SPORTS, EMPLOYEE, and STUDENT relations as given in
Table 1, Table 2, and Table 3 respectively to understand the various
operators.
Table 1: STUDENT_SPORTS
ROLL_NO SPORTS

1 Badminton
2 Cricket

2 Badminton

4 Badminton

Table 2: EMPLOYEE
EMP_NO NAME ADDRESS PHONE AGE

1 RAM DELHI 9455123451 18

5 NARESH HISAR 9782918192 22

6 SWETA RANCHI 9852617621 21

4 SURESH DELHI 9156768971 18

Table 3: 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 9156768971 18

Selection operator (σ): Selection operator is used to selecting tuples from a


relation based on some condition. Syntax:
σ (Cond)(Relation Name)

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

3 SUJIT ROHTAK 9156253131 20

Projection Operator (∏): Projection operator is used to project particular


columns from a relation. Syntax:
∏(Column 1,Column 2….Column n)(Relation Name)

Extract ROLL_NO and NAME from STUDENT relation given in Table 3


∏(ROLL_NO,NAME)(STUDENT)
RESULT:

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

RAMES GURGAO 965243154 Badminto


2 18 1
H N 3 n

RAMES GURGAO 965243154


2 18 2 Cricket
H N 3

RAMES GURGAO 965243154 Badminto


2 18 2
H N 3 n

RAMES GURGAO 965243154 Badminto


2 18 4
H N 3 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

3 SUJIT ROHTAK 915625313 20 4 Badminto


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

Union (U): Union on two relations R1 and R2 can only be computed if R1


and R2 are union compatible (These two relations should have the same
number of attributes and corresponding attributes in two relations have the
same domain). Union operator when applied on two relations R1 and R2 will
give a relation with tuples that are either in R1 or in R2. The tuples which are
in both R1 and R2 will appear only once in the result relation.
Syntax:
Relation1 U Relation2
Find the person who is either student or employees, we can use Union
operators like:
STUDENT U EMPLOYEE
RESULT:
ROLL_NO NAME ADDRESS PHONE AGE

1 RAM DELHI 9455123451 18

2 RAMESH GURGAON 9652431543 18

3 SUJIT ROHTAK 9156253131 20

4 SURESH DELHI 9156768971 18

5 NARESH HISAR 9782918192 22

6 SWETA RANCHI 9852617621 21


Minus (-): Minus on two relations R1 and R2 can only be computed if R1 and
R2 are union compatible. Minus operator when applied on two relations as
R1-R2 will give a relation with tuples that are in R1 but not in R2.
Syntax:
Relation1 - Relation2
Find the person who is a student but not an employee, we can use minus
operator like:
STUDENT - EMPLOYEE
RESULT:
ROLL_NO NAME ADDRESS PHONE AGE

2 RAMESH GURGAON 9652431543 18

3 SUJIT ROHTAK 9156253131 20

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:

ROLL_NO NAME ADDRESS PHONE AGE

1 RAM DELHI 9455123451 18

4 SURESH DELHI 9156768971 18

You might also like