MDU BCA- Database System
MDU BCA- Database System
CONTACT NO-7827646303
2
SECTION-IV
Relational Algebra
Relational Calculus
Relational Database Design: Functional
Dependencies
Modification Anomalies
1st to 3rd NFs
BCNF
4th and 5th NFs
Computing Clousers of set FDs
SQL:
Data Types
Basic Queries in SQL
Insert,Delete and Update Statements
Views
Query Processing:
General Strategies of Query Processing
Query Optimization
Query Processor
Concept of Security
Concurrency and Recovery
5
SECTION-I
BASIC CONCEPT
Data:
Data, in the context of databases, refers to all the single items
that are stored in a database, either individually or as a set. Data
in a database is primarily stored in database tables, which are
organized into columns that dictate the data types stored
therein. So, if the “Customers” table has a column titled
“Telephone Number,” whose data type is defined as “Number,”
then only numerals can be stored in that column.
Explanation about Data:
Data, even in a database, is rarely useful in its raw form. For
example, in a banking application, data is the whole collection
of bank account numbers; bank customers’ names, addresses,
and ages; bank transactions and so on. Being presented with
this mass of numbers will simply overwhelm the average
human -- an individual simply cannot process it all. However,
when data is arranged relationally, it then becomes information,
which is much more useful to users. For example, if the mass
of numbers stored in the banking database above is used to
extract the names and addresses of the top 100 clients by size
of deposit, then the data has been used to provide useful
information.
Files:
File Organization
o The File is a collection of records. Using the primary key,
that format.
In earlier days, database applications were built on top of
file systems.
database.
File processing design approach was well suited to
situations.
Design is simple.
Integrity problems
Database Approach:
In order to remove all limitations of the File Based Approach,
a new approach was required that must be more effective
known as Database approach.
The Database is a shared collection of logically related data,
designed to meet the information needs of an organization. A
database is a computer based record keeping system whose
over all purpose is to record and maintains information. The
database is a single, large repository of data, which can be used
simultaneously by many departments and users. Instead of
disconnected files with redundant data, all data items are
integrated with a minimum amount of duplication.
The database is no longer owned by one department but is a
shared corporate resource. The database holds not only the
organization’s operational data but also a description of this
data. For this reason, a database is also defined as a self-
describing collection of integrated records. The description of
the data is known as the Data Dictionary or Meta Data (the
‘data about data’). It is the self-describing nature of a database
that provides program-data independence.
19
Characteristics of database
Approach-2 :
Isolation between Programs and Data, and Data
Abstraction :
In a traditional file processing system, the structure of
Approach-4 :
Sharing of knowledge and Multi-user Transaction
Processing :
A multi-user DBMS, as its name implies, must allow
Advantages Of Database
Further developed information sharing: A benefit of
the database administration approach is, the DBMS assists
with establishing a climate where end clients have better
admittance to more and better-oversaw information. Such
access makes it workable for end clients to react rapidly to
changes in their current circumstances.
Further developed information security: The more
clients access the information, the more noteworthy the
dangers of information security breaks. Partnerships
contribute significant measures of time, exertion, and cash
to guarantee that corporate information is utilized
appropriately. A DBMS gives a system to better
authorization of information protection and security
arrangements.
Better information reconciliation: More extensive
admittance to very much overseen information advances a
coordinated perspective on the association’s tasks and a
more clear perspective on the 10,000 foot view. It turns
out to be a lot more straightforward to perceive what
activities in a single fragment of the organization mean for
different sections.
Limited information irregularity: Information
irregularity exists when various renditions of similar
information show up in better places. For instance,
27
Disadvantages Of Database
Albeit the database framework yields impressive benefits over
past information the executives draws near, database
frameworks in all actuality do convey critical hindrances.
Advantages Disadvantages
Data consistency is
It requires more memory
increased
Additional information
Multiuser DBMS can be
can be derived from same
more expensive
data
Damage to database
It is cost-efficient affects virtually all
applications programs
4. Procedures
Characteristics of DBMS
o It uses a digital repository established on a server to store
and manage the information.
o It can provide a clear and logical view of the process that
manipulates data.
o DBMS contains automatic backup and recovery
procedures.
o It contains ACID properties which maintain data in a
healthy state in case of failure.
o It can reduce the complex relationship between data.
o It is used to support manipulation and processing of data.
o It is used to provide security of data.
o It can view the database from different viewpoints
according to the requirements of the user.
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.
34
Sophisticated Users
Specialized Users
Application Users
Function of DBMS:
Users
DBMS users:
Types of Users
The users of the database can be classified into the following
groups −
Native users − The native users need not be aware of the
presence of the database system. They are end users of the
database who works through a menu driven application
programs, where the type and range of response is always
indicated to the user
Online users − Online users may communicate with databases
directly through an online terminal or indirectly through user
interface and application programs.
Sophisticated Users − They are those users who interact with
the system without writing the program instead they form their
request in database query language. They are the SQL
programmers, who are going to deal directly with the database.
43
Database Administrator.
Advantages of DBMS
The advantages of the DBMS are explained below −
Redundancy problem can be solved.
In the File System, duplicate data is created in many places
because all the programs have their own files which create data
redundancy resulting in wastage of memory. In DBMS, all the
files are integrated in a single database. So there is no chance
of duplicate data.
For example: A student record in a library or examination can
contain duplicate values, but when they are converted into a
single database, all the duplicate values are removed.
Has a very high security level.
Data security level is high by protecting your precious data
from unauthorized access. Only authorized users should have
the grant to access the database with the help of credentials.
45
Data loss is a big problem for all the organizations. In the file
system users have to back up the files in regular intervals which
lead to waste of time and resources.
DBMS solves this problem of taking backup automatically and
recovery of the database.
Tunability
Tuning means adjusting something to get a better performance.
Same in the case of DBMS, as it provides tunability to improve
performance. DBA adjusts databases to get effective results.
Disadvantages of DBMS
The disadvantages of DBMS are as follows:
Complexity
The provision of the functionality that is expected of a good
DBMS makes the DBMS an extremely complex piece of
software. Database designers, developers, database
administrators and end-users must understand this
functionality to take full advantage of it.
Failure to understand the system can lead to bad design
decisions, which leads to a serious consequence for an
organization.
Size
The functionality of DBMS makes use of a large piece of
software which occupies megabytes of disk space.
Performance
Performance may not run as fast as desired.
Higher impact of a failure
The centralization of resources increases the vulnerability of
the system because all users and applications rely on the
availability of DBMS, the failure of any component can bring
operation to halt.
47
Cost of DBMS
The cost of DBMS varies significantly depending on the
environment and functionality provided. There is also the
recurrent annual maintenance cost.
DBMS languages:
parameters.
(But in Oracle database, the execution of data control
language does not have the feature of rolling back.)
Here are some tasks that come under DCL:
o Grant: It is used to give user access privileges to a
database.
o Revoke: It is used to take back permissions from the user.
There are the following operations which have the
authorization of Revoke:
CONNECT, INSERT, USAGE, EXECUTE, DELETE,
UPDATE and SELECT.
4. Transaction Control Language
TCL is used to run the changes made by the DML statement.
TCL can be grouped into a logical transaction.
Here are some tasks that come under TCL:
o Commit: It is used to save the transaction on the database.
o Rollback: It is used to restore the database to original
since the last Commit.
50
CHAPTER-2
Importance of a DBA
If your organization uses a database management system
(DBMS) for mission-critical workloads, it's important to have
on board one or more database administrators to ensure that
applications have ongoing, uninterrupted access to data. Most
modern organizations of every size use at least one DBMS, and
therefore the need for database administrators is greater today
than ever before.
non-tech person
02. Database Administrator (DBA) :
Database administrator is a person who creates updates and
maintains the database. It is more of a wide role as a data
administrator might be someone who is hired to create,
maintain, and backup the database, optimize the database for
high performance, or someone who helps in integrating
databases into applications. The major skills required to be an
excellent database administrator are troubleshooting, logical
thought process, and a strong will to learn as it involves a vast
area. This role is also known as Database Coordinator or
Database Programmer.
Responsibilities :
Create and design a database
DATA DATABASE
S.NO. ADMINISTRATOR ADMINISTRATOR
Database Designers:
Life Cycle
Comparing this physical data model with the logical with the
previous logical model, we might conclude the differences that
in a physical database entity names are considered table names
and attributes are considered column names. Also, the data type
of each column is defined in the physical model depending on
the actual database used.
Glossary
Entity - An entity in the database can be defined as abstract
data that we save in our database. For example, a customer,
products.
Attributes - An attribute is a detailed form of data consisting
of entities like length, name, price, etc.
Relationship - A relationship can be defined as the connection
between two entities or figures. For example, a person can
relate to multiple persons in a family.
61
Database Users
Database users are the ones who really use and take the benefits
of the database. There will be different types of users depending
on their needs and way of accessing the database.
SECTION-II
DBMS Architecture
o The DBMS design depends upon its architecture. 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
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
application.
69
1. Instances :
Instances are the collection of information stored at a
particular moment. The instances can be changed by certain
CRUD operations as like addition, deletion of data. It may be
noted that any search query will not make any kind of
changes in the instances.
Example –
Let’s say a table teacher in our database whose name is
School, suppose the table has 50 records so the instance of
the database has 50 records for now and tomorrow we are
going to add another fifty records so tomorrow the instance
have total 100 records. This is called an instance.
77
2. Schema :
Example –
Let’s say a table teacher in our database name school, the
teacher table require the name, dob, doj in their table so we
design a structure as :
Teacher table
name: String
doj: date
dob: date
Above given is the schema of the table teacher.
Difference between Schema and Instance :
Schema Instance
It is the collection of
information stored in a
It is the overall description of database at a particular
the database. moment.
Data Independence:
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.
Advantages of Centralized Database
o It has decreased the risk of data management, i.e.,
central repository.
o It provides better data quality, which enables
organizations to establish data standards.
85
3) Relational Database
This database is based on the relational data model, which
stores data in the form of rows(tuple) and columns(attributes),
and together forms a table(relation). A relational database uses
SQL for storing, manipulating, as well as maintaining the data.
E.F. Codd invented the database in 1970. Each table in the
database carries a key that makes the data unique from
others. Examples of Relational databases are MySQL,
Microsoft SQL Server, Oracle, etc.
Properties of Relational Database
There are following four commonly known properties of a
relational model known as ACID properties, where:
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.
87
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.
91
database.
o It allows executing parallel queries on the system.
Clients:
• Offer appropriate interfaces through a client software module
to access as well as utilize the various server resources.
• Clients perhaps diskless machines or PCs or Workstations
with disks with only the client software installed.
• Connected to the servers by means of some form of a network.
• (LAN- local area network, wireless network and so on.)
DBMS Server:
• Provides database query as well as transaction services to the
clients
• Relational DBMS servers are habitually called query servers,
SQL servers, or transaction servers
94
Classification of DBMS's:
• Based on the data model used
• Traditional- Network, Relational, Hierarchical.
• Emerging- Object-oriented and Object-relational.
• Other classifications
• Single-user (typically utilized with personal computers) v/s
multi-user (most DBMSs).
• Centralized (utilizes a single computer with one database)
v/s distributed (uses multiple computers and multiple
databases)
Variations of Distributed DBMSs (DDBMSs):
• Homogeneous DDBMS
• Heterogeneous DDBMS
• Federated or Multi-database Systems
• Distributed Database Systems have at the present come to be
known as client-server based database systems because
• They don’t support a totally distributed environment
however rather a set of database servers supporting a set of
clients.
96
DBMS Architecture:
o The DBMS design depends upon its architecture. The
basic client/server architecture is used to deal with a large
number of PCs, web servers, database servers and other
components that are connected with networks.
o The client/server architecture consists of many PCs and a
workstation which are connected via the network.
o DBMS architecture depends upon how users are
connected to the database to get their request done.
Types of DBMS Architecture
97
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
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
application.
99
Data Models:
Advantages –
Simplicity, Data Integrity, Data security, Efficiency, Easy
availability of expertise.
Disadvantages –
Complexity, Inflexibility, Lack of Data Independence,
Lack of querying facility, Data Manipulation Language,
Lack Of standards.
2. Network Data Model :
In network type, the model data are represented by
collection of records. In this, relationships among the data
103
Advantages –
Simplicity, Data Integrity, Data Independence, Database
standards.
Disadvantages –
System Complexity, Lack of structural Independence.
3. Relational Data Model :
Relational Data Model uses tables to represent the data and
the relationship among these data. Each table has multiple
columns and each column is identified by a unique name.
It is a low-level model.
104
Advantages –
Structural Independence, Simplicity, Ease of designing,
Implementation, Ad-Hoc query capability.
Disadvantages –
Hardware Overheads, Ease of design can result in bad
design.
Objects –
An object is an abstraction of a real world entity or we can
106
Attribute –
An attribute describes the properties of object. For
example: Object is STUDENT and its attribute are Roll no,
Branch, Setmarks() in the Student class.
Methods –
Method represents the behavior of an object. Basically, it
represents the real-world action. For example: Finding a
STUDENT marks in above figure as Setmarks().
Class –
A class is a collection of similar objects with shared
structure i.e. attributes and behavior i.e. methods. An
object is an instance of class. For example: Person,
Student, Doctor, Engineer in above figure.
class student
{
char Name[20];
int roll_no;
--
--
public:
void search();
107
void update();
}
In this example, students refers to class and S1, S2 are the
objects of class which can be created in main function.
Inheritance –
By using inheritance, new class can inherit the attributes
and methods of the old class i.e. base class. For example:
as classes Student, Doctor and Engineer are inherited from
the base class Person.
Easily understandable.
easily.
Conceptual Modeling:
SECTION-III
Entity-Relationship Model:
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.
113
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.
114
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.
115
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.
116
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.
117
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.
118
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.
Entity Types:
<Professor_Dependents>
Entity Sets:
Single-valued attribute :
The attribute which takes up only a single value for each entity
instance is a single-valued attribute.
Example: The age of a student.
Multi-valued attribute :
The attribute which takes up more than a single value for each
entity instance is a multi-valued attribute.
Example: Phone number of a student: Landline and mobile.
Derived attribute :
An attribute that can be derived from other attributes is derived
attributes.
Example: Total and average marks of a student.
Complex attribute :
Those attributes, which can be formed by the nesting of
composite and multi-valued attributes, are called
“Complex Attributes“. These attributes are rarely
used in DBMS(DataBase Management System). That’s why
they are not so popular.
Representation:
Complex attributes are the nesting of two or more composite
and multi-valued attributes. Therefore, these multi-valued and
composite attributes are called ‘Components’ of complex
attributes.
These components are grouped between parentheses ‘(
)’ and multi-valued attributes between curly braces ‘{ }’,
Components are separated by commas ‘, ‘.
For example: let us consider a person having multiple phone
numbers, emails, and an address.
Here, phone number and email are examples of multi-valued
attributes and address is an example of the composite attribute,
127
Complex attributes
Components:
Email, Phone number, Address(All are separated by commas
and multi-valued components are represented between curly
braces).
Complex Attribute: Address_EmPhone(You can choose any
name).
What is an ER Diagram?
What is an ER Model?
History of ER models
Components of ER Diagram
Entities
Weak Entity
Attributes
Key Attribute
Composite Attribute
Multivalued Attribute
Derived Attribute
Relationships
One-to-One Relationships
One-to-Many Relationships
Many-to-One Relationships
Many-to-Many Relationships
Entities
Weak Entity
An entity that makes reliance over another entity is called a
weak entity
Attribute
Key Attribute
Key attribute uniquely identifies an entity from an entity set.
Composite Attribute
An attribute that is composed of several other attributes is
known as a composite attribute.
Multivalued Attribute
Some attributes can possess over one value, those attributes
are called multivalued attributes.
Derived Attribute
An attribute that can be derived from other attributes of the
entity is known as a derived attribute.
Relationship
In the example below, both the student and the course are
entities, and study is the relationship between them.
136
One-to-One Relationship
When a single element of an entity is associated with a single
element of another entity, it is called a one-to-one
relationship.
One-to-Many Relationship
When a single element of an entity is associated with more
than one element of another entity, it is called a one-to-many
relationship
Many-to-One Relationship
When more than one element of an entity is related to a single
element of another entity, then it is called a many-to-one
relationship.
Many-to-Many Relationship
When more than one element of an entity is associated with
more than one element of another entity, this is called a many-
to-many relationship.
Data Abstraction:
component.
In this coupling, data is combined from different sources
Hierarchical Model :
Hierarchical model
John CSE CA
Jake CSE SE
Gami CA 2.0
Mary SE 3.0
Mayen SE 4.0
Example 2: Consider the below cricket database system
hierarchical model scheme.
145
Hierarchical model
Here, in this example, for each player, there are some set of
positions (P_POSITION) he plays, a set of places (P_PLACE),
and also a set of birthdates (P_BDATE) of the players. In the
above figure, each node represents a logical record type and is
displayed by a list of its fields. The child node represents a set
of records that are connected to each record of the parent type,
which is due to a many-to-many relationship is from child to
parent. In the above, figure, the root node PLAYER states that
for every player there will be a set of positions, a set of places
(only one), and a set of birthdates (which is only one).
Advantages of the hierarchical model :
As the database is based on this architecture the
relationships between various layers are logically simple so,
it has a very simple hierarchical database structure.
It has data sharing as all data are held in a common database
Network Model :
This model was formalized by the Database Task group in
the 1960s. This model is the generalization of the hierarchical
model. This model can consist of multiple parent segments
and these segments are grouped as levels but there exists a
logical association between the segments belonging to any
level. Mostly, there exists a many-to-many logical
association between any of the two segments. We
called graphs the logical associations between the segments.
Therefore, this model replaces the hierarchical tree with a
graph-like structure, and with that, there can more general
connections among different nodes. It can have M: N
relations i.e, many-to-many which allows a record to have
more than one parent segment.
147
hierarchical model.
A member record that is the same as of child in the
hierarchical model.
Structure of a Network Model :
In the above figure, member TWO has only one owner ‘ONE’
whereas member FIVE has two owners i.e, TWO and THREE.
Here, each link between the two record types represents 1 : M
relationship between them. This model consists of both lateral
and top-down connections between the nodes. Therefore, it
allows 1: 1, 1 : M, M : N relationships among the given entities
which helps in avoiding data redundancy problems as it
supports multiple paths to the same record. There are various
examples such as TOTAL by Cincom Systems Inc., EDMS by
Xerox Corp., etc.
Example : Network model for a Finance Department.
Below we have designed the network model for a Finance
Department :
148
database standard,
Disadvantages of Network Model :
The schema or the structure of this database is very complex
optimization.
This model fails in achieving structural independence even
945512345
1 RAM DELHI 1 18
915625313
3 SUJIT ROHTAK 1 20
4 SURESH DELHI 18
IMPORTANT TERMINOLOGIES
Attribute: Attributes are the properties that define a
4
152
945512
1 RAM DELHI 3451 18 CS
153
ROHTA 915625
3 SUJIT K 3131 20 ECE
SURE
4 SH DELHI 18 IT
BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
ELECTRONICS AND
COMMUNICATION
ECE ENGINEERING
CV CIVIL ENGINEERING
ANOMALIES
An anomaly is an irregularity, or something which deviates
from the expected or normal state. When designing databases,
we identify three types of
anomalies: Insert, Update and Delete.
Insertion Anomaly in Referencing Relation:
We can’t insert a row in REFERENCING RELATION if
referencing attribute’s value is not present in referenced
attribute value. e.g.; Insertion of a student with
BRANCH_CODE ‘ME’ in STUDENT relation will result in
error because ‘ME’ is not present in BRANCH_CODE of
BRANCH.
Deletion/ Updation Anomaly in Referenced Relation:
We can’t delete or update a row from REFERENCED
RELATION if value of REFERENCED ATTRIBUTE is used
in value of REFERENCING ATTRIBUTE. e.g; if we try to
delete tuple from BRANCH having BRANCH_CODE ‘CS’, it
will result in error because ‘CS’ is referenced by
BRANCH_CODE of STUDENT, but if we try to delete the
row from BRANCH with BRANCH_CODE CV, it will be
deleted as the value is not been used by referencing relation. It
can be handled by following method:
SUPER KEYS:
Any set of attributes that allows us to identify unique rows
(tuples) in a given relation are known as super keys. Out of
these super keys we can always choose a proper subset
among these which can be used as a primary key. Such keys
are known as Candidate keys. If there is a combination of
two or more attributes which is being used as the primary key
then we call it as a Composite key.
Basic Operators in Relational Algebra
Article Contributed by Sonal Tuteja. Please write comments if
you find anything incorrect, or you want to share more
information about the topic discussed above
The relational model for databases starts with Codd. Not the
kind you eat, but a gentleman named E.F. Codd. Codd
developed the model in a 1970 research paper. His concepts
were put into practice in the late 70's in an IBM product
called System R. Subsequently, the Interactive Graphics
Retrieval System (INGRES) was put in place at Berkeley
University.
156
Database Relations:
Properties of Relations:
(a). In any given column of the table, all items are of the same
kind whereas items in different columns may not be of the
same kind.
(b). For a row, each coloums must have an atomic value and
also for a row, a columns cannot more than one value.
(c). All of a relations are distinct. That is, a relation does not
contain two rows which are identical in every column. That is,
each row of the relation can be uniquely identified by its
contents.
(d). The ordering of two within a relation is immaterial. That
is, we cannot retrieved any thing by saying that from row
number 5, column name is to be accessed. Thee is no order
maintained for rows inside a relation.
(e). The columns of a relation are assigned distinct names and
the ordering of these columns is immaterial.
Keys:
Types of keys:
1. Primary key
o It is the first key used to identify one and only one instance
2. Candidate key
o A candidate key is an attribute or set of attributes that can
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.
4. Foreign key
o Foreign keys are the column of the table used to point to
5. Alternate key
There may be one or more attributes or a combination of
attributes that uniquely identify each tuple in a relation. These
attributes or combinations of the attributes are called the
candidate keys. One key is chosen as the primary key from
these candidate keys, and the remaining candidate key, if it
exists, is termed the alternate key. In other words, the total
169
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.
170
7. Artificial key
The key created using arbitrarily assigned data are known as
artificial keys. These keys are created when a primary key is
large and complex and has no relationship with many other
relations. The data values of the artificial keys are usually
numbered in a serial order.
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.
171
Domain:
Example :
CREATE DOMAIN S_ID INT(3) NOT NULL
CHECK(VALUE > 0);
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain
1. Domain constraints
o Domain constraints can be defined as the definition of a
field.
Example:
175
tables.
o In the Referential integrity constraints, if a foreign key in
4. Key constraints
o Keys are the entity set that is used to identify an entity
SECTION-IV
Relational algebra:
3 4
Note: By Default projection removes duplicate data.
Selection (σ)
Selection is used to select required tuples of the relations.
for the above relation
σ (c>3)R
will select the tuples which have c more than 3.
Note: selection operator only selects the required tuples but
does not display them. For displaying, data projection
operator is used.
For the above selected tuples, to display we need to use
projection also.
π (σ (c>3)R ) will show following tuples.
A B C
-------
1 2 4
4 3 4
Union (U)
Union operation in relational algebra is same as union
operation in set theory, only constraint is for union of two
relation both relation must have same set of Attributes.
Rename (ρ)
Rename is a unary operation used for renaming attributes of a
relation.
ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’.
AXB
Name Age Sex Id Course
---------------------------------
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
180
Kim 20 M 2 DBMS
Note: if A has ‘n’ tuples and B has ‘m’ tuples then A X B
will have ‘n*m’ tuples.
Emp Dep
(Name Id Dept_name ) (Dept_name Manager)
------------------------ ---------------------
A 120 IT Sale Y
B 125 HR Prod Z
C 110 Sale IT A
D 111 IT
Emp ⋈ Dep
Conditional Join
Conditional join works similar to natural join. In natural join,
by default condition is equal between common attribute
while in conditional join we can specify the any condition
such as greater than, less than, not equal
Let us see below example
R S
(ID Sex Marks) (ID Sex Marks)
------------------ --------------------
1 F 45 10 M 20
2 F 55 11 M 22
3 F 60 12 M 59
Relational calculus:
For example:
1. { T.name | Author(T) AND T.article = 'database' }
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.
2. Domain Relational Calculus (DRC)
The second form of relation is known as Domain relational
calculus. In domain relational calculus, filtering variable uses
the domain of attributes. Domain relational calculus uses the
same operators as tuple calculus. It uses logical connectives ∧
(and), ∨ (or) and ┓ (not). It uses Existential (∃) and Universal
Quantifiers (∀) to bind the variable. The QBE or Query by
185
Functional dependencies:
of A.
o The following dependencies are also trivial like: A → A,
B→B
Example:
187
a subset of A.
o When A intersection B is NULL, then A → B is called as
complete non-trivial.
Example:
1. ID → Name,
2. Name → DOB
Modification anomalies:
Anomalies
There are different types of anomalies which can occur in
referencing and referenced relation which can be discussed as:
188
14 John 7272826385, UP
9064738238
14 John 7272826385 UP
14 John 9064738238 UP
25 Chemistry 30
25 Biology 30
191
47 English 35
83 Math 38
83 Computer 38
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
192
47 English
83 Math
83 Computer
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
3NF.
o A table is in BCNF if every functional dependency X →
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
EMP_DEPT table:
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
197
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
For the first table: EMP_ID
For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
Now, this is in BCNF because left side part of both the
functional dependencies is a key.
21 Computer Dancing
21 Math Singing
198
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
199
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
Example
SUBJECT LECTURER SEMESTER
In the above table, John takes both Computer and Math class
for Semester 1 but he doesn't take Math class for Semester 2.
In this case, combination of all these fields required to identify
a valid data.
Suppose we add a new Semester as Semester 3 but do not know
about the subject and who will be taking that subject so we
leave Lecturer and Subject as NULL. But all three columns
together acts as a primary key, so we can't leave other two
columns blank.
So to make the above table into 5NF, we can decompose it into
three relations P1, P2 & P3:
P1
SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math
P2
201
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMSTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
202
2. repeat
3. oldX+ := X+ ;
4. for each functional dependency Y → Z in F do
5. if X+ ⊇ Y then X+ := X+ ∪ Z;
6. until (X+ = oldX+ );
QUESTIONS ON CLOSURE SET OF ATTRIBUTE:
1) Given relational schema R( P Q R S T U V) having
following attribute P Q R S T U and V, also there is a set of
functional dependency denoted by FD = { P->Q, QR->ST,
PTV->V }.
Determine Closure of (QR)+ and (PR)+
a) QR+ = QR (as the closure of an attribute or set of attributes
contain same).
Now as per algorithm look into a set of FD that complete the
left side of any FD contains either Q, R, or QR since in FD
QR→ST has complete QR.
Hence QR+ = QRST
Again, trace the remaining two FD that any left part of FD
contains any Q, R, S, T.
Since no complete left side of the remaining two FD{P->Q,
PTV->V} contain Q, R, S, T.
Therefore QR+ = QRST (Answer)
Note: In FD PTV→V, T is in QRST but that cannot be
entertained, as complete PTV should be a subset of QRST
Now as per algorithm look into a set of FD, and check that
complete left side of any FD contains either P, R, or PR. Since
in FD P→Q, P is a subset of PR, Hence PR+ = PRQ
Again, trace the remaining two FD that any left part of FD
contains any P, R, Q, Since, in FD QR → ST has its complete
left part QR in PQR
Hence PR+ = PRQST
Again trace the remaining one FD { PTV->V } that its complete
left belongs to PRQST. Since complete PTV is not in PRQST,
hence we ignore it.
Therefore PR+ = PRQST ( Answer)
2. Given relational schema R( P Q R S T) having following
attributes P Q R S and T, also there is a set of functional
dependency denoted by FD = { P->QR, RS->T, Q->S, T-> P }.
Determine Closure of ( T )+
T + = T (as the closure of an attribute or set of attributes contain
same) Now as per algorithm look into a set of FD that complete
the left side of any FD contains T since, in FD T → P, T is in
T, Hence T+ = TP Again trace the remaining three FD that any
left part of FD contain any TP, Since in FD P → QR has its
complete left part P in TP, Hence T+ = TPQR Again trace the
remaining two FD { RS->T, Q->S } that any of its Complete
left belongs to TPQR, Since in FD Q → S has its complete left
part Q in TPQR, Hence T+ = TPQRS Again trace the remaining
one FD { RS->T } that its complete left belongs to TPQRS,
Since in FD RS → T has its complete left part RS in TPQRS
Hence T+ = TPQRS ( no changes, as T, is already in
TPQRS) Therefore T+ = TPQRS ( Answer).
205
SQL:
then the system figure out the best way to carry out the
request and the SQL engine determines that how to
interpret the task.
206
Characteristics of SQL
o SQL is easy to learn.
management systems.
o SQL can execute queries against the database.
Interactive language
SQL is a domain language used to communicate with the
database. It is also used to receive answers to the complex
questions in seconds.
Multiple data view
Using the SQL language, the users can make different views of
the database structure.
Data types:
Data types are used to represent the nature of the data that can
be stored in the database table. For example, in a particular
column of a table, if we want to store a string type of data then
we will have to declare a string data type of this column.
Data types mainly classified into three categories for every
database.
o String Data types
o Numeric Data types
o Date and time Data types
Data Types in MySQL, SQL Server and Oracle Databases
MySQL Data Types
A list of data types used in MySQL database. This is based on
MySQL 8.0.
209
Or
1. INSERT INTO TABLE_NAME
2. VALUES (value1, value2, value3, .... valueN);
For example:
1. INSERT INTO javatpoint (Author, Subject) VALUES ("Sono
o", "DBMS");
b. UPDATE: This command is used to update or modify the
value of a column in the table.
Syntax:
1. UPDATE table_name SET [column_name1= value1,...column
_nameN = valueN] [WHERE CONDITION]
For example:
1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'
c. DELETE: It is used to remove one or more row from a table.
Syntax:
1. DELETE FROM table_name [WHERE condition];
For example:
1. DELETE FROM javatpoint
2. WHERE Author="Sonoo";
3. Data Control Language
DCL commands are used to grant and take back authority from
any database user.
225
1. COMMIT;
Example:
1. DELETE FROM CUSTOMERS
2. WHERE AGE = 25;
3. COMMIT;
b. Rollback: Rollback command is used to undo transactions
that have not already been saved to the database.
Syntax:
1. ROLLBACK;
Example:
1. DELETE FROM CUSTOMERS
2. WHERE AGE = 25;
3. ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a
certain point without rolling back the entire transaction.
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
o SELECT
a. SELECT: This is the same as the projection operation of
relational algebra. It is used to select the attribute based on the
condition described by WHERE clause.
227
Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:
1. SELECT emp_name
2. FROM employee
3. WHERE age > 20;
1 ABHIRAM 22 ALLAHABAD
229
2 ALKA 20 GHAZIABAD
3 DISHA 21 VARANASI
4 ESHA 21 DELHI
5 MANMEET 23 JALANDHAR
1 ABHIRAM 22 ALLAHABAD
2 ALKA 20 GHAZIABAD
3 DISHA 21 VARANASI
4 ESHA 21 DELHI
5 MANMEET 23 JALANDHAR
6 PRATIK 24 KANPUR
230
The results show that all ten records are inserted successfully
using a single query.
Example 2:
To create a table in the database, first, we need to select the
database in which we want to create a table.
1. mysql> USE dbs;
Then we will write a query to create a table named items_tbl in
the selected database 'dbs'.
1. mysql> CREATE TABLE items_tbl(ID INT, Item_Name V
ARCHAR(20), Item_Quantity INT, Item_Price INT, Purchas
e_Date DATE);
2 Toothpaste 2 80 2021-07-10
3 Pen 10 50 2021-07-12
5 Brush 3 90 2021-07-15
8 Chips 5 50 2021-07-30
Packet
9 Marker 2 30 2021-08-13
10 Scissors 1 60 2021-08-13
The results show that all ten records are inserted successfully
using a single query.
45.5M
773
History of Java
1. UPDATE table_name
2. SET column_name = expression
3. WHERE conditions
Let's take an example: here we are going to update an entry in
the source table.
SQL statement:
1. UPDATE students
2. SET User_Name = 'beinghuman'
3. WHERE Student_Id = '3'
Source Table:
1. UPDATE tableDestination
2. SET tableDestination.col = value
3. WHERE EXISTS (
4. SELECT col2.value
5. FROM tblSource
6. WHERE tblSource.join_col = tblDestination. Join_col
7. AND tblSource.Constraint = value)
You can also try this one -
1. UPDATE
2. Table
3. SET
4. Table.column1 = othertable.column 1,
5. Table.column2 = othertable.column 2
6. FROM
7. Table
8. INNER JOIN
9. Other_table
10. ON
11. Table.id = other_table.id
My SQL SYNTAX:
If you want to UPDATE with SELECT in My SQL, you can
use this syntax:
Let's take an example having two tables. Here,
First table contains -
Cat_id, cat_name,
And the second table contains -
Rel_cat_id, rel_cat_name
243
Views in SQL:
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 1
2 Kathrin 86 2
3 David 74 1
4 Alina 90 2
5 John 96 1
1. Creating view
245
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
246
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
247
Query processing:
Query Optimization:
2. Selection is commutative.
Equivalence 2
Theta Join
Equivalence 2
Equivalence 2
Query Processor:
Concept of Security:
Attacks on Backups
Companies that do not protect backup data using the same
rigorous controls employed to protect databases themselves are
at risk of cyberattacks on backups.
The following factors amplify the threats:
o Data volumes are growing: Data capture, storage, and
processing continue to increase exponentially in almost all
organizations. Any tools or methods must be highly
flexible to meet current as well as far-off needs.
o The infrastructure is sprawling: Network environments
are becoming more complicated, especially as companies
shift their workloads into multiple clouds and hybrid cloud
architectures and make the selection of deployment,
management, and administration of security solutions
more difficult.
o More stringent requirements for regulatory
compliance: The worldwide regulatory compliance
landscape continues to increase by complexity. This
makes the compliance of every mandate more
challenging.
Best use of Database Security
As databases are almost always accessible via the network, any
security risk to any component or part of the infrastructure can
threaten the database. Likewise, any security attack that
impacts a device or workstation could endanger the database.
Therefore, security for databases must go beyond the limits of
the database.
264
For example:
Consider the below diagram where two transactions TX and
TY, are performed on the same account A where the balance
of account A is $300.
Crash Recovery
DBMS is a highly complex system with hundreds of
transactions being executed every second. The durability and
robustness of a DBMS depends on its complex architecture and
its underlying hardware and system software. If it fails or
crashes amid transactions, it is expected that the system would
follow some sort of algorithm or techniques to recover lost data.
277
Failure Classification
To see where the problem has occurred, we generalize a failure
into various categories, as follows −
Transaction failure
A transaction has to abort when it fails to execute or when it
reaches a point from where it can’t go any further. This is called
transaction failure where only a few transactions or processes
are hurt.
Reasons for a transaction failure could be −
Logical errors − Where a transaction cannot complete
because it has some code error or any internal error
condition.
System errors − Where the database system itself
an inconsistent state.
There are two types of techniques, which can help a DBMS in
recovering as well as maintaining the atomicity of a transaction
−
Maintaining the logs of each transaction, and writing them
onto some stable storage before actually modifying the
database.
Maintaining shadow paging, where the changes are done
on a volatile memory, and later, the actual database is
updated.
Log-based Recovery
Log is a sequence of records, which maintains the records of
actions performed by a transaction. It is important that the logs
are written prior to the actual modification and stored on a
stable storage media, which is failsafe.
Log-based recovery works as follows −
The log file is kept on a stable storage media.
When a transaction enters the system and starts execution,
follows −
<Tn, X, V1, V2>
It reads Tn has changed the value of X, from V 1 to V2.
280
If the recovery system sees a log with <Tn, Start> and <Tn,