You are on page 1of 18

Unit-2

ER model
o ER model stands for an Entity-Relationship model. It is a high-level data model. This
model is used to define the data elements and relationship for a specified system.
o It develops a conceptual design for the database. It also develops a very simple and
easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an entity-
relationship diagram.

For example, Suppose we design a school database. In this database, the student will be an
entity with attributes like address, name, id, age, etc. The address can be another entity
with attributes like city, street name, pin code, etc and there will be a relationship between
them.

Component of ER Diagram
1. Entity:

An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.

Consider an organization as an example- manager, product, employee, department etc. can


be taken as an entity.

a. Weak Entity

An entity that depends on another entity called a weak entity. The weak entity doesn't
contain any key attribute of its own. The weak entity is represented by a double rectangle.

2. Attribute

The attribute is used to describe the property of an entity. Eclipse is used to represent an
attribute.

For example, id, age, contact number, name, etc. can be attributes of a student.

a. Key Attribute
The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.
b. Composite Attribute

An attribute that composed of many other attributes is known as a composite attribute. The
composite attribute is represented by an ellipse, and those ellipses are connected with an
ellipse.

c. Multivalued Attribute

An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute.

For example, a student can have more than one phone number.
d. Derived Attribute

An attribute that can be derived from other attribute is known as a derived attribute. It can
be represented by a dashed ellipse.

For example, A person's age changes over time and can be derived from another attribute
like Date of birth.

3. Relationship

A relationship is used to describe the relation between entities. Diamond or rhombus is


used to represent the relationship.

Types of relationship are as follows:

a. One-to-One Relationship

When only one instance of an entity is associated with the relationship, then it is known as
one to one relationship.

For example, A female can marry to one male, and a male can marry to one female.
b. One-to-many relationship

When only one instance of the entity on the left, and more than one instance of an entity on
the right associates with the relationship then this is known as a one-to-many relationship.

For example, Scientist can invent many inventions, but the invention is done by the only
specific scientist.

c. Many-to-one relationship

When more than one instance of the entity on the left, and only one instance of an entity on
the right associates with the relationship then it is known as a many-to-one relationship.

For example, Student enrolls for only one course, but a course can have many students.

d. Many-to-many relationship

When more than one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then it is known as a many-to-many
relationship.

For example, Employee can assign by many projects and project can have many employees.

Relational Model concept


Relational model can represent as a table with columns and rows. Each row is known as a
tuple. Each table of the column has a name or attribute.

Domain: It contains a set of atomic values that an attribute can take.

Attribute: It contains the name of a column in a particular table. Each attribute Ai must have
a domain, dom(Ai)

Relational instance: In the relational database system, the relational instance is represented
by a finite set of tuples. Relation instances do not have duplicate tuples.

Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.

Relational key: In the relational key, each row has one or more attributes. It can identify the
row in the relation uniquely.

Example: STUDENT Relation

NAME ROLL_NO PHONE_NO ADDRESS AGE

anisha 14 7305758992 ctc 20

biswajit 12 9026288936 bbsr 21

Laxman 33 8583287182 ctc 20

Mahesh 27 7086819134 bbsr 22

Ganesh 17 9028 9i3988 ctc 23


o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the
attributes.
o The instance of schema STUDENT has 5 number of tuples.
o t3 = <Laxman, 33, 8583287182, ctc, 20>

Properties of Relations

o Name of the relation is distinct from all other relations.


o Each relation cell contains exactly one atomic (single) value
o Each attribute contains a distinct name
o Attribute domain has no significance
o tuple has no duplicate value
o Order of tuple can have a different sequence

Relational Model Concepts


1. Attribute: Each column in a Table. Attributes are the properties which define a
relation. Ex., Student_Rollno, Name,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of
the relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Relation key - Every row has one, two or multiple attributes, which is called relation
key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is
known as attribute domain

Relational Integrity Constraints

Relational Integrity constraints in DBMS are referred to conditions which must be present
for a valid relation. These Relational constraints in DBMS are derived from the rules in the
mini-world that the database represents.

There are many types of Integrity Constraints in DBMS. Constraints on the Relational
database management system are mostly divided into three main categories are:

1. Domain Constraints
2. Key Constraints
3. Referential Integrity Constraints

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 in DBMS are 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

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

You can see that in the below-given relation table CustomerName= 'Apple' is updated from

Inactive to Active.

Delete Operation

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

deleted.

In the above-given example, CustomerName= "Apple" is deleted from the table.

The Delete operation could violate referential integrity if the tuple which is deleted is
referenced by foreign keys from other tuples in the same database.

Select Operation

In the above-given example, CustomerName="Amazon" is selected

Best Practices for creating a Relational Model

 Data need to be represented as a collection of relations


 Each relation should be depicted clearly in the table
 Rows should contain data about instances of an entity
 Columns must contain data about attributes of the entity
 Cells of the table should hold a single value
 Each column should be given a unique name
 No two rows can be identical
 The values of an attribute should be from the same domain

Advantages of using Relational Model


 Simplicity: A Relational data model in DBMS is simpler than the hierarchical and
network model.
 Structural Independence: The relational database is only concerned with data and
not with a structure. This can improve the performance of the model.
 Easy to use: The Relational model in DBMS is easy as tables consisting of rows and
columns are quite natural and simple to understand
 Query capability: It makes possible for a high-level query language like SQL to avoid
complex database navigation.
 Data independence: The Structure of Relational database can be changed without
having to change any application.
 Scalable: Regarding a number of records, or rows, and the number of fields, a
database should be enlarged to enhance its usability.

Disadvantages of using Relational Model

 Few relational databases have limits on field lengths which can't be exceeded.
 Relational databases can sometimes become complex as the amount of data grows,
and the relations between pieces of data become more complicated.
 Complex relational database systems may lead to isolated databases where the
information cannot be shared from one system to another.

Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as
input and yields instances of relations as output.It uses operators to perform queries. An
operator can be either unary or binary. They accept relations as their input and yield
relations as their output. Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −

 Select
 Project
 Union
 Set different
 Cartesian product
 Rename
We will discuss all these operations in the following sections.

Select Operation (σ)

It selects tuples that satisfy the given predicate from a relation.


Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < ,  >,  ≤.
For example − σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.

Project Operation (∏)

It projects column(s) that satisfy a given predicate.


Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example − ∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.

Union Operation (∪)

It performs binary union between two given relations and is defined as −


r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −

 r, and s must have the same number of attributes.


 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or
both.
Set Difference (−)

The result of set difference operation is tuples, which are present in one relation but are
not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.

Cartesian Product (Χ)

Combines information of two different relations into one.


Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.

Rename Operation (ρ)

The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with
small Greek letter rho ρ.
Notation − ρ x (E)
Q.Query to rename the relation student as male_student and the attributes of the
student –rollno,sname as(sno,name)
Ans- ρ male_student(sno,name) ∏rollno,sname(σ condition(student))
Where the result of expression E is saved with name of x.
Additional operations are −

 Set intersection
 Assignment
 Natural join

Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language,
that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)

Filtering variable ranges over tuples


Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on 'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
{ R| ∃T   ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.

Domain Relational Calculus (DRC)

In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as
done in TRC, mentioned above).
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is
database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra.

Extended Operators in Relational Algebra


In Relational Algebra, Extended Operators are those operators that are derived from the
basic operators. We already have discussed Basic Operators in the previous section. Now let
us discuss the Extended Operators and how they are beneficial in Relational Algebra. There
are mainly three types of Extended Operators, namely:

1. Intersection
2. Divide
3. Join
Let us consider two tables named as A and B.

A–

RollNo Name Marks


1 Anisha 98
3 Anjali 79
4 Biswajit 88

B–

RollNo Name Marks


1 Anisha 98
2 Abhishek 87
3 Anjali 79
4 Biswajit 88

1) Intersection

Intersection works on the relation as 'this and that'. In relational algebra, A ∩ B returns a


relation instance that contains every tuple that occurs in relation to instance A and relation
instance B (both together). Here, A and B need to be union-compatible, and the schema of
both result and A must be identical.

Syntax:

SELECT * FROM A INTERSECT SELECT * FROM B;


RollNo Name Marks
1 Anisha 98
3 Anjali 79
4 Biswajit 88
2) Divide

Divide operator is used for the queries that contain the keyword ALL.
For e.g. – Find all the students who has chosen additional subjects Machine Learning and
Data Mining.

Student –

Student Name Subject


Ashish Machine Learning
Ashish Data Mining
Shivam Network Security
Shivam Data Mining
Tarun Network Security
Tarun Machine Learning
Yash Machine Learning
Yash Data Mining

Subject –

Student Name
Machine Learning
Data Mining

Output: Student ÷ Subject

Student
Ashish
Yash
3) Join

Join operation is as its name suggest, to join or combine two or more relations’ information.
Join can also be defined as a cross-product followed by selection and projection. There are
several varieties of Join operation. Let’s discuss all of them one by one.

Student1 –

RollNo Name Marks


1 Ashish 98
2 Shivam 72
3 Tarun 53
4 Yash 89

Student2 –

RollNo Name Marks


1 Anjali 99
4 Dinesh 79
5 Harsh 95
7 Kartik 88
a. Condition Join

When you want to join two relations based on the given condition, it is termed as Condition
Join. It is denoted by the symbol ⋈c.

For e.g. – Select the students from Student1 table whose RollNo is greater than the RollNo
of Student2 table.

Student1⋈cStudent1.RollNo>Student2.RollNoStudent2

Syntax:

SELECT * FROM Student1, Student2 WHERE Student1.RollNo > Student2.RollNo;

Output –

RollNo Name Marks RollNo Name Marks


2 Shivam 72 1 Anjali 99
3 Tarun 53 1 Anjali 99
4 Yash 89 1 Anjali 99
b. Equi Join

It is a special case of Condition Join. When you want to join two relations based on the
equality condition, it is termed as Equi Join. It is denoted by the symbol ⋈.

For e.g. - Select the students from Student1 table whose RollNo is equalto the RollNo of
Student2 table.

Student1⋈Student1.RollNo=Student2.RollNoStudent2
Syntax:

SELECT * FROM Student1, Student2 WHERE Student1.RollNo=Student2.RollNo;

Output –

RollNo Name Marks RollNo Name Marks


1 Ashish 98 1 Anjali 99
4 Yash 89 4 Dinesh 79
c. Natural Join

Natural Join is that type of join in which equi join is by default applied to all the attributes in
two or more relation. Its specialty is if you want to consider the equality between two
relations, you don’t need to define the equality; it is predefined for all the attributes if you
use Natural Join. It is denoted by the symbol ⋈.

For e.g. - Select the students from Student1 table whose RollNo is equal to the RollNo of
Student2 table.

Student1⋈Student2

Syntax:

SELECT * FROM Student1 NATURAL JOIN Student2;

Output –

RollNo Name Marks RollNo Name Marks


1 Ashish 98 1 Anjali 99
4 Yash 89 4 Dinesh 79

There is also one more type of Join Outer Join which further gets divided into Left Outer
Join, Right Outer Join and Full Outer Join.

You might also like