DBMS Tutorial
DBMS Tutorial
DBMS Tutorial provides basic and advanced concepts of Database. Our DBMS Tutorial
is designed
for beginners and professionals both.
Our DBMS Tutorial includes all topics of DBMS such as introduction, ER model, keys,
relational
model, join operation, SQL, functional dependency, transaction, concurrency control,
etc.
What is Database
The database is a collection of inter-related data which is used to retrieve, insert and
delete the data
efficiently. It is also used to organize the data in the form of a table, schema, views, and
reports, etc.
51. vs Java
For example: The college Database organizes the data about the admin, staff, students
and faculty
etc.
Using the database, you can easily retrieve, insert, and delete the information.
Advantages of DBMS
o Controls database redundancy: It can control data redundancy because it stores
all the data
in one single database file and that recorded data is placed in the database.
o Data sharing: In DBMS, the authorized users of an organization can share the
data among
multiple users.
o Easily Maintenance: It can be easily maintainable due to the centralized
nature of the
database system.
o Reduce time: It reduces development time and maintenance need.
o Backup: It provides backup and recovery subsystems which create automatic
backup of data
from hardware and software failures and restores the data if required.
o multiple user interface: It provides different types of user interfaces like
graphical user
interfaces, application program interfaces
Disadvantages of DBMS
o Cost of Hardware and Software: It requires a high speed of data processor and
large
memory size to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them efficiently.
o Complexity: Database system creates additional complexity and requirements.
o Higher impact of failure: Failure is highly impacted the database because in
most of the
organization, all the data stored in a single database and if the database is
damaged due to
electric failure or database corruption then the data may be lost forever.
Database
What is Data?
Data is a collection of a distinct small unit of information. It can be used in a variety
of forms like text, numbers, media, bytes, etc. it can be stored in pieces of paper or
electronic memory, etc.
Word 'Data' is originated from the word 'datum' that means 'single piece of
information.' It is plural of the word datum.
In computing, Data is information that can be translated into a form for efficient
movement and processing. Data is interchangeable.
What is Database?
A database is an organized collection of data, so that it can be easily accessed and
managed.
53.7M
1.1K
You can organize data into tables, rows, columns, and index it to make it easier to find
relevant information.
Database handlers create a database in such a way that only one set of software
program provides access of data to all the users.
The main purpose of the database is to operate a large amount of information by storing,
retrieving, and managing data.
There are many dynamic websites on the World Wide Web nowadays which are
handled through databases. For example, a model that checks the availability of rooms
in a hotel. It is an example of a dynamic website that uses a database.
There are many databases available like MySQL, Sybase, Oracle, MongoDB, Informix,
PostgreSQL, SQL Server, etc.
SQL or Structured Query Language is used to operate on the data stored in a database.
SQL depends on relational algebra and tuple relational calculus.
The Evolution
File-Based
1968 was the year when File-Based database were introduced. In file-based
databases, data was maintained in a flat file. Though files have many advantages,
there are several limitations.
One of the major advantages is that the file system has various access methods, e.g.,
sequential, indexed, and random.
1968-1980 was the era of the Hierarchical Database. Prominent hierarchical database
model was IBM's first DBMS. It was called IMS (Information Management System).
Below diagram represents Hierarchical Data Model. Small circle represents objects.
Like file system, this model also had some limitations like complex implementation,
lack structural independence, can't easily handle a many-many relationship, etc.
In this model, files are related as owners and members, like to the common network
model.
Network data model identified the following components:
This model also had some limitations like system complexity and difficult to design and
maintain.
Relational Database
Schema specifies the structure like name of the relation, type of each column
and name. This model uses some mathematical concept like set theory and
predicate logic.
During the era of the relational database, many more models had introduced like
object-oriented model, object-relational model, etc.
Cloud database
Cloud database facilitates you to store, manage, and retrieve their structured,
unstructured data via a cloud platform. This data is accessible over the Internet. Cloud
databases are also called a database as service (DBaaS) because they are offered as a
managed service.
o Snowflake Computing
Advantages of cloud
Generally, company provider does not have to invest in databases. It can maintain and
support one or more data centers.
Automated
Cloud databases are enriched with a variety of automated processes such as recovery,
failover, and auto-scaling.
Increased accessibility
You can access your cloud-based database from any location, anytime. All you need
is just an internet connection.
NoSQL Database
A NoSQL database is an approach to design such databases that can accommodate a
wide variety
of data models. NoSQL stands for "not only SQL." It is an alternative to traditional
relational
databases in which data is placed in tables, and data schema is perfectly designed
before the
database is built.
Advantage of NoSQL
High Scalability
NoSQL can handle an extensive amount of data because of scalability. If the data
grows, NoSQL database scale it to handle that data in an efficient manner.
High Availability
NoSQL supports auto replication. Auto replication makes it highly available because, in
case of any failure, data replicates itself to the previous consistent state.
Disadvantage of NoSQL
Open source
Management challenge
GUI tools for NoSQL database are not easily available in the market.
Backup
Backup is a great weak point for NoSQL databases. Some databases, like MongoDB,
have no powerful approaches for data backup.
o Objects
o Classes
o Inheritance
o Polymorphism
o Encapsulation
o Atomicity
o Consistency
o Integrity
o Durability
o Concurrency
o Query processing
Graph Databases
A graph database is a NoSQL database. It is a graphical representation of data. It
contains nodes and edges. A node represents an entity, and each edge represents a
relationship between two edges. Every node in a graph database represents a unique
identifier.
Graph databases are beneficial for searching the relationship between data because
they highlight the relationship between relevant data.
Graph databases are very useful when the database contains a complex relationship
and dynamic
schema.
o DBMS provides the interface to perform the various operations like creation, deletion,
modification,
etc.
o DBMS allows the user to create their databases as per their requirement.
o DBMS accepts the request from the application and provides specific data through the
operating
system.
o DBMS contains the group of programs which acts according to the user instruction.
Advantage of DBMS
Controls redundancy
It stores all the data in a single database file, so it can control data redundancy.
Data sharing
Backup
It providesBackup and recovery subsystem. This recovery system creates automatic data
from system failure and restores data if required.
Disadvantage of DBMS
Size
Cost
DBMS requires a high-speed data processor and larger memory to run DBMS software, so
it is costly.
Complexity
o Table
o Record/ Tuple
o Instance
o Schema
o Keys
An RDBMS is a tabular DBMS that maintains the security, integrity, accuracy, and
consistency of the
data.
Types of Databases
There are various types of databases used for storing different varieties of data:
1) Centralized Database
It is the type of database that stores data at a centralized database system. It comforts
the users to
access the stored data from different locations through several applications. These
applications
contain the authentication process to let users access data securely. An example of a
Centralized
database can be Central Library that carries a central database of each library in a
college/university.
o It is less costly because fewer vendors are required to handle the data sets.
o If any server failure occurs, entire data will be lost, which could be a huge loss.
2) Distributed Database
Unlike a centralized database system, in distributed systems, data is distributed
among different database systems of an organization. These database systems are
connected via communication links. Such links help the end-users to access the data
easily. Examples of the Distributed database are Apache Cassandra, HBase, Ignite, etc.
o Homogeneous DDB: Those database systems which execute on the same operating
system and use
the same application process and carry the same hardware devices.
A means Atomicity: This ensures the data operation will complete either with success or
with failure.
It follows the 'all or nothing' strategy. For example, a transaction will either be
committed or will
abort.
C means Consistency: If we perform any operation over the data, its value before and
after the operation should be preserved. For example, the account balance before and
after the transaction should be correct, i.e., it should remain conserved.
I means Isolation: There can be concurrent users for accessing data at the same time
from the database. Thus, isolation between the data should remain isolated. For
example, when multiple transactions occur at the same time, one transaction effects
should not be visible to the other transactions in the database.
D means Durability: It ensures that once it completes the operation and commits the
data, data changes should remain permanent.
4) NoSQL Database
Non-SQL/Not Only SQL is a type of database that is used for storing a wide range of
data sets. It is not a relational database as it stores data not only in tabular form but in
several different ways. It came into existence when the demand for building modern
applications increased. Thus, NoSQL presented a wide variety of database
technologies in response to the demands. We can further divide a NoSQL database
into the following four types:
a. Key-value storage: It is the simplest type of database storage where it stores every
single item as a
key (or attribute name) holding its value, together.
b. Document-oriented Database: A type of database used to store data as JSON-like
document. It
helps developers in storing data by using the same document-model format as used in the
application
code.
c. Graph Databases: It is used for storing vast amounts of data in a graph-like
structure. Most
commonly, social networking websites use the graph database.
o Users can quickly access data from the database through key-value.
5) Cloud Database
A type of database where data is stored in a virtual environment and executes over
the cloud computing platform. It provides users with various cloud computing services
(SaaS, PaaS, IaaS, etc.) for accessing the database. There are numerous cloud
platforms, but the best options are:
o Microsoft Azure
o Kamatera
o PhonixNAP
o ScienceSoft
6) Object-oriented Databases
The type of database that uses the object-based data model approach for storing
data in the database system. The data is represented and stored as objects which are
similar to the objects used in the object-oriented programming language.
7) Hierarchical Databases
It is the type of database that stores data in the form of parent-children relationship
nodes. Here, it organizes data in a tree-like structure.
Data get stored in the form of records that are connected via links. Each child record in
the tree will
contain only one parent. On the other hand, each parent record can have multiple child
records.
8) Network Databases
It is the database that typically follows the network data model. Here, the
representation of data is
in the form of nodes connected via links between them. Unlike the hierarchical
database, it allows
each record to have multiple children and parent nodes to form a generalized graph
structure.
9) Personal Database
Collecting and storing data on the user's system defines a Personal Database. This
database is basically designed for a single user.
How it works
Data is represented in terms of tuples (rows) in RDBMS. of India (1947-2020)
A relational database is the most commonly used database. It contains several tables,
and each table has its primary key.
Due to a collection of an organized set of tables, data can be accessed easily in RDBMS.
invention.
What is table/Relation?
Everything in a relational database is stored in the form of relations. The RDBMS
database uses tables to store data. A table is a collection of related data entries and
contains rows and columns to store data. Each table represents some real-world
objects such as person, place, or event about which information is collected. The
organized collection of data into a relational table is known as the logical view of the
database.
Properties of a Relation:
o All attributes in a relation are atomic, i.e., each cell of a relation contains exactly one
value.
A table is the simplest example of data stored in RDBMS.
1 Ajeet 24 B.Tech
2 aryan 20 C.A
3 Mahesh 21 BCA
4 Ratan 22 MCA
5 Vimal 26 BSC
Properties of a row:
o All tuples of the relation have the same format and the same number of entries.
o The order of the tuple is irrelevant. They are identified by their content, not by
1 Ajeet 24 B.Tech
What is a column/attribute?
A column is a vertical entity in the table which contains all information associated
with a specific field in a table. For example, "name" is a column in the above table
which contains all information about a student's name.
Properties of an Attribute:
o Attributes that uniquely identify each tuple of a relation are the primary key.
Name
Ajeet
Aryan
Mahesh
Ratan
Vimal
o The data items for an attribute should be drawn from the same domain.
In the below example, the data item in the student table consists of Ajeet, 24 and
Btech, etc.
1 Ajeet 24 B.Tech
Degree:
The total number of attributes that comprise a relation is known as the degree of the
table.
For example, the student table has 4 attributes, and its degree is 4.
2 aryan 20 C.A
3 Mahesh 21 BCA
4 Ratan 22 MCA
5 Vimal 26 BSC
Cardinality:
The total number of tuples at any one time in a relation is known as the table's
cardinality. The relation whose cardinality is 0 is called an empty table.
For example, the student table has 5 rows, and its cardinality is 5.
1 Ajeet 24 B.Tech
2 aryan 20 C.A
3 Mahesh 21 BCA
4 Ratan 22 MCA
5 Vimal 26 BSC
Domain:
The domain refers to the possible values each attribute can contain. It can be
specified using standard data types such as integers, floating numbers, etc. For
example, An attribute entitled Marital_Status may be limited to married or unmarried
values.
NULL Values
The NULL value of the table specifies that the field has been left blank during record
creation. It is different from the value filled with zero or a field that contains space.
Data Integrity
There are the following categories of data integrity exist with each RDBMS:
Entity integrity: It specifies that there should be no duplicate rows in a table.
Domain integrity: It enforces valid entries for a given column by restricting the type, the
format, or the range of values.
Referential integrity specifies that rows cannot be deleted, which are used by other
records.
The main differences between DBMS and RDBMS are given below:
1) DBMS applications store data as file. RDBMS applications store data in a tabular form.
2) In DBMS, data is generally stored in either In RDBMS, the tables have an identifier called
primary
a hierarchical form or a navigational form.key and the data values are stored in the form
of tables.
4) DBMS does not apply any security with RDBMS defines the integrity constraint for
the
regards to data manipulation. purpose of ACID (Atomocity, Consistency, Isolation
and
Durability) property.
5) DBMS uses file system to store data, so in RDBMS, data values are stored in the form of
tables,
there will be no relation between the so a relationship between these data values will
be
tables. stored in the form of a table as well.
6) DBMS has to provide some uniform RDBMS system supports a tabular structure of
the data
methods to access the stored and a relationship between them to access the
stored
information. information.
After observing the differences between DBMS and RDBMS, you can say that RDBMS is
an extension of DBMS. There are many software products in the market today who are
compatible for both DBMS and RDBMS. Means today a RDBMS application is DBMS
application and vice-versa.
Consider an example of a student's file system. The student file will contain
information regarding
the student (i.e. roll no, student name, course etc.). Similarly, we have a subject file
that contains
information about the subject and the result file which contains the information
regarding the result.
Some fields are duplicated in more than one file, which leads to data redundancy. So to
overcome this problem, we need to create a centralized system, i.e. DBMS approach.
DBMS:
A database approach is a well-organized collection of data that are related in a
meaningful way which can be accessed by different users but stored only once in a
system. The various operations performed by the DBMS system are: Insertion, deletion,
selection, sorting etc.
In the above figure,
There are the following differences between DBMS and File systems:
Data DBMS gives an abstract view of data The file system provides the
detail of
Abstraction that hides the details. the data representation and
storage
of data.
Security and DBMS provides a good protection It isn't easy to protect a file
under the
Protection mechanism. file system.
Recovery DBMS provides a crash recovery The file system doesn't have a
crash
Mechanism mechanism, i.e., DBMS protects the user mechanism, i.e., if the system
crashes
from system failure. while entering some data, then
the
content of the file will be lost.
Manipulation DBMS contains a wide variety ofThe file system can't efficiently
store
Techniques sophisticated techniques to store andand retrieve the data.
retrieve the data.
Concurrency DBMS takes care of Concurrent access In the File system, concurrent
access
Problems of data using some form of locking. has many problems like
redirecting
the file while deleting
some
information or updating
some information.
Where to use Database approach used in large File system approach used in
large
systems which interrelate many files. systems which interrelate many
files.
Structure The database structure is complex toThe file system approach has
a
design. simple structure.
Data Models In the database approach, 3 types In the file system approach,
of there is no concept of data
data models exist: models exists.
o Hierarchal data models
o Network data models
o Relational data models
Flexibility Changes are often a necessity to theThe flexibility of the system is less
as
content of the data stored in any compared to the DBMS approach.
system, and these changes are
more easily with a database
approach.
o DBMS architecture depends upon how users are connected to the database to get their
request done.
Database architecture can be seen as a single tier or multi-tier. But logically, database
architecture is of two types like: 2-tier architecture and 3-tier architecture.
1-Tier Architecture
o In this architecture, the database is directly available to the user. It means the user can
directly sit on
the DBMS and uses it.
o Any changes done here will directly be done on the database itself. It doesn't provide a
handy tool
for end users.
o The 1-Tier architecture is used for development of the local application, where
programmers can
directly communicate with the database for the quick response.
2-Tier Architecture
o The 2-Tier architecture is same as basic client-server. In the two-tier architecture,
applications on the
client end can directly communicate with the database at the server side. For this
interaction, API's
like: ODBC, JDBC are used.
o The user interfaces and application programs are run on the client-side.
o The server side is responsible to provide the functionalities like: query processing and
transaction
management.
o To communicate with the DBMS, client-side application establishes a connection with the
server side.
Fig: 2-tier Architecture
3-Tier Architecture
o The 3-Tier architecture contains another layer between the client and server. In this
architecture, client
can't directly communicate with the server.
o The application on the client-end interacts with an application server which further
communicates
with the database system.
o End user has no idea about the existence of the database beyond the application server.
The database
also has no idea about any other user beyond the application.
o Mapping is not good for small DBMS because it takes more time.
o In External / Conceptual mapping, it is necessary to transform the request from
external level to
conceptual schema.
o In Conceptual / Internal mapping, DBMS transform the request from the conceptual to
internal level.
o All users should be able to access the same data according to their requirements.
o DBA should be able to change the conceptual structure of the database without affecting
the user's
1. Internal Level
o The internal level has an internal schema which describes the physical storage
structure of the
database.
following activities:
2. Conceptual Level
o The conceptual schema describes the design of a database at the conceptual level.
Conceptual level
is also known as logical level.
o In the conceptual level, internal details such as an implementation of the data structure
are hidden.
3. External Level
o At the external level, a database contains several schemas that sometimes called as
subschema. The
subschema is used to describe the different view of the database.
o The view schema describes the end user interaction with database systems.
o External / Conceptual
Mapping
The Conceptual/ Internal Mapping lies between the conceptual level and the internal
level. Its role is to define the correspondence between the records and fields of the
conceptual level and files and data structures of the internal level.
External/ Conceptual Mapping
The external/Conceptual Mapping lies between the external level and the Conceptual
level. Its role is to define the correspondence between a particular external and the
conceptual view.
Data Models
Data Model is the modeling of the data description, data semantics, and consistency
constraints of the data. It provides the conceptual tools for describing the design of a
database at each level of data abstraction. Therefore, there are following four data
models used for understanding the structure of the database:
1) Relational Data Model: This type of model designs the data in the form of rows and
columns within a table. Thus, a relational model uses tables for representing
data and in-between relationships. Tables are also called relations. This model was
initially described by Edgar F. Codd, in 1969. The relational data model is the widely
used model which is primarily used by commercial data processing applications.
4) Semistructured Data Model: This type of data model is different from the other
three data models (explained above). The semistructured data model allows the data
specifications at places where the individual data items of the same type may have
different attributes sets. The Extensible Markup Language, also known as XML, is
widely used for representing the semistructured data. Although XML was initially
designed for including the markup information to the text document, it gains
importance because of its application in the exchange of data.
A schema diagram can display only some aspects of a schema like the name of
record type, data
type, and constraints. Other aspects can't be specified through the schema diagram.
For example,
the given figure neither show the data type of each data item nor the relationship
among various
files.
In the database, actual data changes quite frequently. For example, in the given figure,
the database changes whenever we add a new grade or add a student. The data at a
particular moment of time is called the instance of the database.
Data Independence
o Data independence can be explained using the three-schema architecture.
o Data independence refers characteristic of being able to modify the schema at
one level of
the database system without altering the schema at the next higher level.
Database Language
o A DBMS has appropriate languages and interfaces to express database queries and
updates.
o Database languages can be used to read, store and update the data in the database.
o Using the DDL statements, you can create the skeleton of the database.
o Data definition language is used to store the information of metadata like the number of
tables and
schemas, their names, indexes, columns in each table, constraints, etc.
These commands are used to update the database schema that's why they come
under Data definition language.
(But in Oracle database, the execution of data control language does not have
the feature of rolling back.)
and SELECT.
o Rollback: It is used to restore the database to original since the last Commit.
ACID Properties
The expansion of the term ACID defines for:
1) Atomicity: The term atomicity defines that the data remains atomic. It means if any
operation is
performed on the data, either it should be performed or executed completely or
should not be
executed at all. It further means that the operation should not break in between or
execute partially.
In thea Try Catch
case of executing operations on the transaction, the operation should be completely executed and not partially.
Example: If Remo has account A having $30 in his account from which he wishes to
send $10 to
Sheero's account, which is B. In account B, a sum of $ 100 is already present. When
$10 will be
transferred to account B, the sum will become $110. Now, there will be two operations
that will take
place. One is the amount of $10 that Remo wants to transfer will be debited from his
account A, and
the same amount will get credited to account B, i.e., into Sheero's account. Now, what
happens - the
first operation of debit executes successfully, but the credit operation, however, fails.
Thus, in Remo's
account A, the value becomes $20, and to that of Sheero's account, it remains
$100 as it was
previously present.
In the above diagram, it can be seen that after crediting $10, the amount is still $100
in account B. So, it is not an atomic transaction.
The below image shows that both debit and credit operations are done successfully.
Thus the transaction is atomic.
Thus, when the amount loses atomicity, then in the bank systems, this becomes a
huge issue, and so the atomicity is the main focus in the bank systems.
2) Consistency: The word consistency means that the value should remain preserved
always.
In DBMS, the integrity of the data should be maintained, which means if a change in the
database is
made, it should remain preserved always. In the case of transactions, the integrity of the
data is very
essential so that the database remains consistent before and after the transaction. The
data should
always be correct.
Example:
In the above figure, there are three accounts, A, B, and C, where A is making a
transaction T one by one to both B & C. There are two operations that take place, i.e.,
Debit and Credit. Account A firstly debits $50 to account B, and the amount in account A
is read $300 by B before the transaction. After the successful transaction T, the
available amount in B becomes $150. Now, A debits $20 to account C, and that time,
the value read by C is $250 (that is correct as a debit of $50 has been successfully
done to B). The debit and credit operation from account A to C has been done
successfully. We can see that the transaction is done successfully, and the value is
also read correctly. Thus, the data is consistent. In case the value read by B and C is
$300, which means that data is inconsistent because when the debit operation
executes, it will not be consistent.
4) Isolation: The term 'isolation' means separation. In DBMS, Isolation is the property of
a database where no data should affect the other one and may occur concurrently. In
short, the operation on one database should begin when the operation on the first
database gets complete. It means if two operations are being performed on two
different databases, they may not affect the value of one another. In the case of
transactions, when two or more transactions occur simultaneously, the consistency
should remain maintained. Any changes that occur in any particular transaction will not
be seen by other transactions until the change is not committed in the memory.
Example: If two operations are concurrently running on two different accounts, then
the value of both accounts should not get affected. The value should remain
persistent. As you can see in the below diagram, account A is making T1 and T2
transactions to account B and C, but both are executing independently without
affecting each other. It is known as Isolation.
Therefore, the ACID property of DBMS plays a vital role in maintaining the
consistency and availability of data in the database.
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
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.
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.
Notation of ER diagram
Database can be represented using the notations. In ER diagram, many notations are
used to express the cardinality. These notations are as follows:
Fig: Notations of ER diagram
ER Design Issues
In the previous sections of the data modeling, we learned to design an ER diagram.
We also discussed different ways of defining entity sets and relationships among them.
We also understood the various designing shapes that represent a relationship, an
entity, and its attributes. However, users often mislead the concept of the elements
and the design process of the ER diagram. Thus, it leads to a complex structure of
the ER diagram and certain issues that does not meet the characteristics of the
real-world enterprise model.
Here, we will discuss the basic design issues of an ER database schema in the following
points:
Thus, it requires the overall knowledge of each part that is involved inb desgining and
modelling an ER diagram. The basic requirement is to analyse the real-world
enterprise and the connectivity of one entity or attribute with other.
Mapping Constraints
o A mapping constraint is a data constraint that expresses the number of entities
to which
another entity can be related via a relationship set.
o It is most useful in describing the relationship sets that involve more than two
entity sets.
o For binary relationship set R on an entity set A and B, there are four possible
mapping
cardinalities. These are as follows:
1. One to one (1:1)
2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)
One-to-one
In one-to-one mapping, an entity in E1 is associated with at most one entity in E2, and
an entity in E2 is associated with at most one entity in E1.
One-to-many
In one-to-many mapping, an entity in E1 is associated with any number of entities in
E2, and an entity in E2 is associated with at most one entity in E1.
Many-to-one
In one-to-many mapping, an entity in E1 is associated with at most one entity in E2, and
an entity in E2 is associated with any number of entities in E1.
Many-to-many
In many-to-many mapping, an entity in E1 is associated with any number of entities in
E2, and an entity in E2 is associated with any number of entities in E1.
Keys
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also used to
establish and
identify relationships between tables.
For example, ID is used as a key in the Student table because it is unique for each
student. In the
PERSON table, passport_number, license_number, SSN are keys since they are
unique for each
person.
Types of keys:
1. Primary key
o It is the first key used to identify one and only one instance of an entity uniquely. An
entity can contain
multiple keys, as we saw in the PERSON table. The key which is most suitable from those
lists becomes
a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the
EMPLOYEE table, we can even select License_Number and Passport_Number as
primary keys since
they are also unique.
o For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
o Except for the primary key, the remaining attributes are considered a candidate key.
The candidate
keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of
the attributes, like SSN, Passport_Number, License_Number, etc., are considered a
candidate key.
3. Super Key
Super key is an attribute set that can uniquely identify a tuple. A super key is a superset
of a candidate
key.
For example: In the above EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME), the
name of two
employees can be the same, but their EMPLYEE_ID can't be the same. Hence, this
combination can
also be a key.
4. Foreign key
o Foreign keys are the column of the table used to point to the primary key of another
table.
For example, employee relation has two attributes, Employee_Id and PAN_No, that act
as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the
other candidate key, PAN_No, acts as the Alternate key.
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a
composite key. This key is also known as Concatenated Key.
For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID,
is large in employee relations. So it would be better to add a new virtual attribute to
identify each tuple in the relation uniquely.
Generalization
o Generalization is like a bottom-up approach in which two or more entities of
lower level
combine to form a higher level entity if they have some attributes in common.
o In generalization, an entity of a higher level can also combine with the entities
of the lower
level to form a further higher level entity.
o Generalization is more like subclass and superclass system, but the only
difference is the
approach. Generalization uses the bottom-up approach.
o In generalization, entities are combined to form a more generalized entity, i.e.,
subclasses are
combined to make a superclass.
For example, Faculty and Student entities can be generalized and create a higher level
entity Person.
Specialization
o Specialization is a top-down approach, and it is opposite to Generalization. In
specialization,
one higher level entity can be broken down into two lower level entities.
o Specialization is used to identify the subset of an entity set that shares some
distinguishing
characteristics.
o Normally, the superclass is defined first, the subclass and its related attributes
are defined
next, and relationship set are then added.
Aggregation
In aggregation, the relation between two entities is treated as a single entity. In
aggregation, relationship with its corresponding entities is aggregated into a higher
level entity.
For example: Center entity offers the Course entity act as a single entity in the
relationship which is
in a relationship with another entity visitor. In the real world, if a visitor visits a
coaching center then
he will never enquiry about the Course only or just about the Center instead he will ask
the enquiry
about both.
Reduction of ER diagram to Table
The database can be represented using the notations, and these notations can be
reduced to a collection of tables.
In the database, every entity set or relationship set can be represented in tabular form.
There are some points for converting the ER diagram to the table:
38M
667
History of Java
In the given ER diagram, LECTURE, STUDENT, SUBJECT and COURSE forms individual
tables.
In the STUDENT table, Age is the derived attribute. It can be calculated at any point
of time by calculating the difference between current date and Date of Birth.
Using these rules, you can convert the ER diagram to tables and columns and assign
the mapping between the tables. Table structure for the given ER diagram is as below:
1. One-to-one (1:1)
2. One-to-many (1:M)
3. Many-to-many (M:N)
1. One-to-one
o In a one-to-one relationship, one occurrence of an entity relates to only one occurrence
in another
entity.
o A one-to-one relationship rarely exists in practice.
o For example: if an employee is allocated a company car then that car can only be
driven by that
employee.
2. One-to-many
o In a one-to-many relationship, one occurrence in an entity relates to many
occurrences in another
entity.
o For example: An employee works in one department, but a department has many
employees.
o Therefore, department and employee have a one-to-many relationship.
3. Many-to-many
o In a many-to-many relationship, many occurrences in an entity relate to many
occurrences in another
entity.
o Same as a one-to-one relationship, the many-to-many relationship rarely exists in
practice.
o For example: At the same time, an employee can work on several projects, and a project
has a team
of many employees.
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.
o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the
attributes.
o The instance of schema STUDENT has 5 tuples.
o t3 = <Laxman, 33289, 8583287182, Gurugram, 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 Algebra
Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.
1. Notation: σ p(r)
Where:
52.8M
969
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result.
Rest of the
attributes are eliminated from the table.
o It is denoted by ∏.
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that
are either in R
or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
automatically. Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
(DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in
both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
table
Input:
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in R
but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
table
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is
also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by
rho (ρ).
STUDENT1.
oin Operations:
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
1. Operation: (EMPLOYEE ⋈
SALARY)
Result:
54.2M
877
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal on their
common
attribute names.
o It is denoted by ⋈.
SALARY table:
Input:
SALARY)
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with
missing information.
Example:
EMPLOYEE
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈
FACT_WORKERS)
Output:
EMP_NAME STREET CITY BRANCH SALAR
o It is denoted by ⟕.
FACT_WORKERS table
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
o It is denoted by ⟖.
Example: Using the above EMPLOYEE table and FACT_WORKERS Relation
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
o It is denoted by ⟗.
FACT_WORKERS table
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
3. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched
data as per the equality condition. The equi join uses the comparison operator(=).
Example:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
1. CUSTOMER ⋈ PRODUCT
Output:
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes
have to be
performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the
attribute must be available in the corresponding domain.
Example:
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation and if
the primary
key has a null value, then we can't identify those rows.
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A
primary key
can contain a unique and null value in the relational table.
Example:
Relational Calculus
There is an alternate way of formulating queries known as Relational Calculus.
Relational calculus is a non-procedural query language. In the non-procedural query
language, the user is concerned with the details of how to obtain the end results. The
relational calculus tells what to do but never explains how to do. Most commercial
relational languages are based on aspects of relational calculus including SQL-QBE
and QUEL.
Why it is called Relational Calculus?
It is based on Predicate calculus, a name derived from branch of symbolic language. A
predicate is a truth-valued function with arguments. On substituting values for the
arguments, the function result in an expression called a proposition. It can be either true
or false. It is a tailored version of a subset of the Predicate Calculus to communicate
with the relational database.
Many of the calculus expressions involves the use of Quantifiers. There are two
types of quantifiers:
o Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which
means that in a
given set of tuples exactly all tuples satisfy a given condition.
o Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all which
means that in
a given set of tuples there is at least one occurrences whose value satisfy a given
condition.
Before using the concept of quantifiers in formulas, we need to know the concept of Free
and Bound Variables.
Free and bound variables may be compared with global and local variable of
programming languages.
Where
Output: This query selects the tuples from the AUTHOR relation. It returns a tuple with
'name' from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and
Universal Quantifiers (∀).
For example:
1. { R| ∃T ∈ Authors(T.article='database' AND
R.name=T.name)}
Output: This query will yield the same result as the previous
one.
calculus uses the same operators as tuple calculus. It uses logical connectives ∧ (and),
relational calculus, filtering variable uses the domain of attributes. Domain relational
∨ (or) and ┓ (not). It uses Existential (∃) and Universal Quantifiers (∀) to bind the
variable. The QBE or Query by example is a query language related to domain
relational calculus.
Notation:
Where
For example:
1. {< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}
Output: This query will yield the article, page, and subject from the relational
javatpoint, where the subject is a database.