Professional Documents
Culture Documents
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.
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. 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.
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.
Properties of Relations
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 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.
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".
1 Google Active
2 Amazon Active
3 Apple Inactive
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:
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.
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
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.
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.
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)
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.
1. Intersection
2. Divide
3. Join
Let us consider two tables named as A and B.
A–
B–
1) Intersection
Syntax:
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 –
Subject –
Student Name
Machine Learning
Data Mining
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 –
Student2 –
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:
Output –
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:
Output –
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:
Output –
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.