0% found this document useful (0 votes)
17 views11 pages

Relational Model

Uploaded by

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

Relational Model

Uploaded by

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

Data Models

A Database model defines the logical design and structure of a database and
defines how data will be stored, accessed and updated in a database
management system. While the Relational Model is the most widely used
database model, there are other models too:

 Hierarchical Model

This database model organizes data into a tree-like-structure, with a


single root, to which all the other data is linked. The hierarchy starts
from the Root data, and expands like a tree, adding child nodes to the
parent nodes.
In this model, a child node will only have a single parent node. This
model efficiently describes many real-world relationships like index of
a book, recipes etc.

Network Model

This is an extension of the Hierarchical model. In this model data is


organized more like a graph, and are allowed to have more than one
parent node. In this database model data is more related as more
relationships are established in this database model. Also, as the data
is more related, hence accessing the data is also easier and fast. This
database model was used to map many-to-many data relationships.
This was the most widely used database model, before Relational
Model was introduced.
 Entity-relationship Model
 Relational Model

Relational Model

RELATIONAL MODEL (RM) represents the database as a collection of


relations. A relation is nothing but a table of values. Every row in the table
represents a collection of related data values. These rows in the table denote
a real-world entity or relationship.

The table name and column names are helpful to interpret the meaning of
values in each row. The data are represented as a set of relations. In the
relational model, data are stored as tables. However, the physical storage of
the data is independent of the way the data are logically organized.

Some popular Relational Database management systems are:

 DB2 - IBM
 Oracle - Oracle
 Access – Microsoft

Relational Model Concepts

1. Attribute: Each column in a Table. Attributes are the properties which


define a relation. e.g., Student_Rollno, NAME,etc.
2. Tuple – It is a single row of a table, which contains a single record.
3. Tables – In the Relational model the, relations are saved in the table
format. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
4. Relation Schema - A relation schema represents the name of the
relation with its attributes.
5. Cardinality - Total number of rows present in the Table.
6. Relation instance – Relation instance is a finite set of tuples in the
RDBMS system. Relation instances never have duplicate tuples.
7. Relation key - Every row has one, two or multiple attributes, which is
called relation key.
8. Attribute domain – Every attribute has some pre-defined value and
scope which is known as attribute domain
Keys
1. In the relational model, keys are important because they are used to
ensure that each row in a table is uniquely identifiable.
2. They establish relationships among tables and to ensure the integrity
of the data.
3. A key consists of one or more attributes that determines other
attributes.
4. A key’s role is based on a concept known as determination.
5. In a database table statement “A determines B” indicate with the value
of attribute A the value of attribute B is determined.
6. The notation is A?B

Primary Key – It is a key that uniquely identifies a row in a table. Primary


Key cannot contain null values. Ex: st_no
Candidate Key – The candidate key is a key that is ready to become a
primary key. Even the candidate keys cannot contain null values. It is used
for theoretical purpose. Ex: st_email
Foreign Key – An attribute or combination of attributes in one table whose
values must either match the primary key in another table or be null. Ex:
st_no in another table.
Composite Key – A key may be composed of more than one attribute such
a multi-attribute key is called a composite key. Ex: st_fname, st_mname,
st_lname:
Secondary Key – An attribute or combination of attributes used strictly for
data retrieval purposes. Secondary Key can contain redundant values. Ex:
select statement
Super Key – An attribute or combination of attributes that uniquely
identifies each row in a table. Ex: st_no, st_name, st_phone.

Relational Integrity constraints

Relational Integrity constraints is referred to conditions which must be


present for a valid relation. These integrity constraints are derived from the
rules in the mini-world that the database represents.

There are many types of integrity constraints. Constraints on the Relational


database management system is mostly divided into three main categories
are:

1. Domain constraints - pre-defined values


2. Integrity Key constraints – Primary Key
3. Referential integrity constraints - Foreign Key
Domain Constraints

Domain constraints can be violated if an attribute value is not appearing in


the corresponding domain or it is not of the appropriate data type.

Domain constraints specify that within each tuple, and the value of each
attribute must be unique. This is specified as data types which include
standard data types integers, real numbers, characters, Booleans, variable
length strings, etc.

Example:

Create DOMAIN CustomerName


CHECK (value not NULL)

The example shown demonstrates creating a domain constraint such that


CustomerName is not NULL

Key constraints

An attribute that can uniquely identify a tuple in a relation is called the key
of the table. The value of the attribute for different tuples in the relation has
to be unique.

Example:

In the given table, CustomerID is a key attribute of Customer Table. It is most


likely to have a single key for one customer, CustomerID =1 is only for the
CustomerName =" Google".

CustomerID CustomerName Status

1 Google Active

2 Amazon Active

3 Apple Inactive

Referential integrity constraints

Referential integrity constraints is based on the concept of Foreign Keys. A


foreign key is an important attribute of a relation which should be referred to
in other relationships. Referential integrity constraint state happens where
relation refers to a key attribute of a different or same relation. However,
that key element must exist in the table.

Example:

In the above example, we have 2 relations, Customer and Billing.

Tuple for CustomerID =1 is referenced twice in the relation Billing. So we


know CustomerName=Google
has billing amount $300

Violation of constraints in relational database

Here, we will learn about the violations that can occur on a database as a
result of any changes made in the relation.
There are mainly four operations that have the ability to change the state of
relations, these modifications are given below:

Operations in Relational Model

Four basic update operations performed on relational database model are

Insert, update, delete and select.

 Insert is used to insert data into the relation


 Delete is used to delete tuples from the table.
 Modify allows you to change the values of some attributes in existing
tuples.
 Select allows you to choose a specific range of data.
Whenever one of these operations are applied, integrity constraints specified
on the relational database schema must never be violated.

Insert Operation

The insert operation gives values of the attribute for a new tuple which
should be inserted into a relation.

Update Operation

The update operation will modify the value which is already present in the
table

Delete Operation

To specify deletion, a condition on the attributes of the relation selects the


tuple to be deleted.

Select Operation

This operation helps in retrieving the data from the table.

Relationships within the Relational Database


Relatonships are classified as one-to-one (1:1), one-to-many (1:M), and
many-to-many (M:N).

The 1:M relationship is the relational modeling ideal.


The 1:1 relationship should be rare in any relational database design.
M:N relationships cannot be implemented in the relational model. M:N
relationships can be changed to two 1:M relationships.
1:M Relationship
The 1:M relationship is the relational database norm.
Each painting is painted by one and only painter, but each painter could
have painted many paintings.
The 1:M relationship is found in any database environment.
1:1 Relationship
One entity can be related to only one other entity, and vice versa.
One department can have only one department chair headed by a professor.
The entity professor and department exhibit 1:1 relationship.
The use of a 1:1 relationship ensures that two entity sets are not placed in
the same table whey should not be.
M:N Relationship
A M:N relationship is not supported directly in the relational environment. It
can be implemented by creating a new entity in 1:M relationships with the
original entities.
Consider, Students can take many classes and each class can contain many
students
1: m m:1
Example
Table Name -Students
Stu_id Stu_fn Stu_ln Stu_pno Stu_city Stu_email
220 Beena Rao 1234567 Hyderaba [email protected]
d
225 Beena Das 2345678 Mumbai [email protected]
m
230 Shilpa Rao 8765432 Hyderaba [email protected]
d
235 Ramesh Khanna 6754321 Chennai [email protected]
m

Table Name – Course


Cr_id Cr_name Cr_duration Stu_id
18 Python 6 months 230
11 Accounts 3 months 230
14 Tally 8 months 220
16 C++ 5 months 225
18 Java 6 months 235
22 Statistics 4 months 220

Primary Key – stu_id (unique values , no null, no duplicates)


Candidate Key – stu_email (ready to become a primary key)
Secondary Key – retrieving data from the table (Select statement)
Composite Key – stu_fn, stu_ln (Combining more than one attribute)
Super Key – stu_id, stu_pno, stu_fn (Combination of more than one key
attribute)
Foreign Key – stu_id( primary key in one table, normal column in another
table)
This key builds a relationship with one or more tables.

Exercise:
Table Name – Employee Details

emp_id emp_fn emp_ln emp_pno emp_city emp_email


220 Beena Rao 1234567 Hyderaba [email protected]
d
225 Beena Das 2345678 Mumbai [email protected]
m
230 Shilpa Rao 8765432 Hyderaba [email protected]
d
235 Ramesh Khanna 6754321 Chennai [email protected]
m
Table Name – Employee Department Details

dept_id dept_nam Dept_loc Emp_id


e
10 Computer Hyd 220
20 Marketing Mum 225
30 Sales Mum 225
40 Service Chennai 240
1. Identify the Primary Keys in both tables
2. Identify Candidate Key, Super Key, Composite Key from the first
table
3. Identify Foreign Key
4. Can Table Employee Department Details be updated for empid
240
5. Can 220 be deleted from employee details

Relational Algebra
Relational Algebra is a set of basic operations used to manipulate the data in
relational model. These operations enable the user to specify basic retrieval
request. The result of retrieval is a new relation, formed from one or more
relations.
These operations can be classified in two categories:
 Basic Set Operations
1) Union
2) Intersection
3) Set Difference (Minus)
4) Cartesian Product

 Relational Operations
1) Select
2) Project
3) Join
4) Division

UNION
If R1 and R2 are two union compatible relations then R3 = R1  R2 is the
relation containing tuples that are either in R1 or in R2 or in both.

Table R1 Table R2 Union Table R3

A B X Y A B
A1 B1 A1 B1 A1 B1
A2 B2 A7 B7 A2 B2
A3 B3 A3 B3 A3 B3
A4 B4 A5 B5 A4 B4
A7 B7
Intersection A5 B5
If R1 and R2 are two union compatible functions or relations, then the result
of R3 = R1 R2 is the relation that includes all tuples that are in both the
relations
Table R1 Table R2 Intersection Table R3

A B X Y A B
A1 B1 A1 B1 A1 B1
A2 B2 A7 B7 A3 B3
A3 B3 A3 B3 A4 B4
A4 B4 A4 B4

Set Difference

If R1 and R2 are two union compatible relations or relations then result of R3


=R1– R2 is the relation that includes only those tuples that are in R1 but not
in R2.

Table R1 Table R2

A B X Y
A1 B1 A1 B1
A2 B2 A7 B7
A3 B3 A3 B3
A4 B4 A4 B4
Difference (R1-R2)
A B
A2 B2

Difference (R2-R1)
A B
A7 B7

Cartesian Product
Cartesian Product If R1 and R2 are two functions or relations, then the result
of R3 = R1  R2 is the combination of tuples that are in R1 and R2.

Table R1 Table R2

A B C
A1 B1 C1
A2 B2 C2
A3 B3
A4 B4

Product
A B C
A1 B1 C1
A2 B2 C1
A3 B3 C1
A4 B4 C1
A1 B1 C2
A2 B2 C2
A3 B3 C2
A4 B4 C2

SELECT (particular row or all rows)


The select operation is used to select some specific records from the
database based on some criteria.

Select stu_name from students;

Project (particular column)

The project operation is used to select the records with specified attributes
while discarding the others based on some specific criteria. This is denoted
as  .
Join

related tuples from two given relation join is used. This is denoted as ⋈. The
The JOIN operation is applied on two relations. When we want to select

join operation requires that both the joined relations must have at least one
domain compatible attributes.

Division
To perform the division operation R1  R2, R2 should be a proper subset of
R1.

Table R1 Table R2

A B B
A1 B1 B1
A1 B2 B2
A3 B1
A4 B2
A5 B1
A3 B2

R3=R1R2
A
A1
A3

You might also like