Databases Notes
Databases Notes
net/publication/344296324
CITATIONS READS
0 298
1 author:
Ghulam Gilanie
The Islamia University of Bahawalpur
113 PUBLICATIONS 408 CITATIONS
SEE PROFILE
All content following this page was uploaded by Ghulam Gilanie on 18 September 2020.
2. Distributed Database
Just opposite of the centralized database concept, the distributed database has contributions from
the common database as well as the information captured by local computers also. The data is not
at one place and is distributed at various sites of an organization. These sites are connected to each
other with the help of communication links, which helps them to access the distributed data easily.
You can imagine a distributed database as a one in which various portions of a database are stored
in multiple different locations (physical) along with the application procedures, which are
0300-2038128 Page | 1
Compiled by Dr. Ghulam Gilanie Janjua
replicated and distributed among various points in a network. There are two kinds of distributed
database, viz. homogenous and heterogeneous. The databases, which have same underlying
hardware and run over same operating systems and application procedures, are known as
homogeneous DDB, for e.g. all physical locations in a DDB. Whereas, the operating systems,
underlying hardware as well as application procedures can be different at various sites of a DDB
which is known as heterogeneous DDB.
3. Personal Database
Data is collected and stored on personal computers, which is small and easily manageable. The
data is generally used by the same department of an organization and is accessed by a small group
of people.
4. End User Database
The end user is usually not concerned about the transaction or operations done at various levels
and is only aware of the product, which may be a software or an application. Therefore, this is a
shared database, which is specifically designed for the end user, just like different levels’
managers. Summary of whole information is collected in this database.
5. Commercial Database
These are the paid versions of the huge databases designed uniquely for the users who want to
access the information for help. These databases are subject specific, and one cannot afford to
maintain such a huge information. Access to such databases is provided through commercial links.
6. NoSQL Database
These are used for large sets of distributed data. There are some big data performance issues, which
are effectively handled by relational databases, such kind of issues, are easily managed by NoSQL
databases. There are very efficient in analyzing large size unstructured data that may be stored at
multiple virtual servers of the cloud.
7. Operational Database
Information related to operations of an enterprise is stored inside this database. Functional lines
like marketing, employee relations, customer service etc. require such kind of databases.
0300-2038128 Page | 2
Compiled by Dr. Ghulam Gilanie Janjua
8. Relational Databases
These databases are categorized by a set of tables where data gets fit into a pre-defined category.
The table consists of rows and columns where the column has an entry for data for a specific
category and rows contains instance for that data defined according to the category. The Structured
Query Language (SQL) is the standard user and application program interface for a relational
database. There are various simple operations that can be applied over the table, which makes these
databases easier to extend, join two databases with a common relation and modify all existing
applications.
9. Cloud Databases
Now a day, data has been specifically getting stored over either clouds also known as a virtual
environment, in a hybrid cloud, public or private cloud. A cloud database is a database that has
been optimized or built for such a virtualized environment. There are various benefits of a cloud
database, some of which are the ability to pay for storage capacity and bandwidth on a per-user
basis, and they provide scalability on demand, along with high availability. A cloud database also
gives enterprises the opportunity to support business applications in a software-as-a-service
deployment.
0300-2038128 Page | 3
Compiled by Dr. Ghulam Gilanie Janjua
0300-2038128 Page | 4
Compiled by Dr. Ghulam Gilanie Janjua
Network Model
This is an extension of the Hierarchical model. In this model, data is organized more like a graph,
and are allowed to have more than one parent node. In this database, model data is more related as
more relationships are established in this database model. Also, as the data is more related, hence
accessing the data is also easier and fast. This database model was used to map many-to-many data
0300-2038128 Page | 5
Compiled by Dr. Ghulam Gilanie Janjua
relationships. This was the most widely used database model, before Relational Model was
introduced.
Entity-relationship Model
Let us now learn how the ER Model is represented by means of an ER diagram. Any object, for
example, entities, attributes of an entity, relationship sets, and attributes of relationship sets, can
be represented with the help of an ER diagram.
Entity
Entities are represented by means of rectangles. Rectangles are named with the entity set they
represent.
Attributes
Attributes are the properties of entities. Attributes are represented by means of ellipses. Every
ellipse represents one attribute and is directly connected to its entity (rectangle).
If the attributes are composite, they are further divided in a tree like structure. Every node is then
connected to its attribute. That is, composite attributes are represented by ellipses that are
connected with an ellipse.
0300-2038128 Page | 6
Compiled by Dr. Ghulam Gilanie Janjua
0300-2038128 Page | 7
Compiled by Dr. Ghulam Gilanie Janjua
Relationship
Relationships are represented by diamond-shaped box. Name of the relationship is written inside
the diamond-box. All the entities (rectangles) participating in a relationship, are connected to it
by a line.
Binary Relationship and Cardinality
A relationship where two entities are participating is called a binary relationship. Cardinality is
the number of instance of an entity from a relation that can be associated with the relation.
One-to-one − When only one instance of an entity is associated with the relationship, it is
marked as '1:1'. The following image reflects that only one instance of each entity should
be associated with the relationship. It depicts one-to-one relationship.
0300-2038128 Page | 8
Compiled by Dr. Ghulam Gilanie Janjua
Many-to-one − When more than one instance of entity is associated with the relationship,
it is marked as 'N:1'. The following image reflects that more than one instance of an entity
on the left and only one instance of an entity on the right can be associated with the
relationship. It depicts many-to-one relationship.
Many-to-many − The following image reflects that more than one instance of an entity
on the left and more than one instance of an entity on the right can be associated with the
relationship. It depicts many-to-many relationship.
Participation Constraints
Total Participation − Each entity is involved in the relationship. Total participation is
represented by double lines.
Partial participation − Not all entities are involved in the relationship. Partial
participation is represented by single lines.
0300-2038128 Page | 9
Compiled by Dr. Ghulam Gilanie Janjua
Relational Model
In this model, data is organized in two-dimensional tables and the relationship is maintained by
storing a common field. This model was introduced by E.F Codd in 1970, and since then it has
been the most widely used database model, we can say the only database model used around the
world. The basic structure of data in the relational model is tables. All the information related to a
particular type is stored in rows of that table. Hence, tables are also known as relations in
relational model.
Meta data
In a typical database, the user saved data is present and Meta data is stored. Metadata is data
about the data. This is information stored by the DBMS to better understand the data stored in it.
For example, when I store my Name in a database, the DBMS will store when the name was
stored in the database, what is the size of the name, is it stored as related data to some other data,
or is it independent, all this information is metadata.
Database Access Language
Database Access Language is a simple language designed to write commands to access, insert,
update and delete data stored in any database. A user can write commands in the Database Access
Language and submit it to the DBMS for execution, which is then translated and executed by the
DBMS. User can create new databases, tables, insert data, fetch stored data, update data and delete
the data using the access language.
DBMS Architecture
A Database Management system is not always directly available for users and applications to
access and store data in it. A Database Management system can be centralized (all the data stored
at one location), decentralized (multiple copies of database at different locations) or hierarchical,
depending upon its architecture.
0300-2038128 Page | 10
Compiled by Dr. Ghulam Gilanie Janjua
The design of a DBMS depends on its architecture. It can be centralized or decentralized or
hierarchical. The architecture of a DBMS can be seen as either single tier or multi-tier. An n-tier
architecture divides the whole system into related but independent n modules, which can be
independently modified, altered, changed, or replaced.
In 1-tier architecture, the DBMS is the only entity where the user directly sits on the DBMS and
uses it. Any changes done here will directly be done on the DBMS itself. It does not provide
handy tools for end-users. Database designers and programmers normally prefer to use single-tier
architecture.
If the architecture of DBMS is 2-tier, then it must have an application through which the DBMS
can be accessed. Programmers use 2-tier architecture where they access the DBMS by means of
an application. Here the application tier is entirely independent of the database in terms of
operation, design, and programming.
3-tier Architecture
A 3-tier architecture separates its tiers from each other based on the complexity of the users and
how they use the data present in the database. It is the most widely used architecture to design a
DBMS.
Database (Data) Tier − At this tier, the database resides along with its query processing
languages. We also have the relations that define the data and their constraints at this level.
Application (Middle) Tier − At this tier reside the application server and the programs
that access the database. For a user, this application tier presents an abstracted view of the
database. End-users are unaware of any existence of the database beyond the application.
At the other end, the database tier is not aware of any other user beyond the application
tier. Hence, the application layer sits in the middle and acts as a mediator between the
end-user and the database.
User (Presentation) Tier − End-users operate on this tier and they know nothing about
any existence of the database beyond this layer. At this layer, multiple views of the
database can be provided by the application. All views are generated by applications that
reside in the application tier.
Multiple-tier database architecture is highly modifiable, as almost all its components are
independent and can be changed independently.
Entity and Entity Set
0300-2038128 Page | 11
Compiled by Dr. Ghulam Gilanie Janjua
An Entity is generally a real-world object, which has characteristics and holds relationships in a
DBMS. If a Student is an Entity, then the complete dataset of all the students will be the Entity
Set.
Attributes
Student’s roll no., student's name, student's age, student's gender etc. will be its attributes. An
attribute can be of many types, here are different types of attributes defined in ER database model:
1. Simple attribute: The attributes with values that are atomic and cannot be broken down
further are simple attributes. For example, student's age.
2. Composite attribute: A composite attribute is made up of more than one simple attribute.
For example, student's address will contain, house no., street name, pincode etc.
3. Derived attribute: These are the attributes, which are not present in the whole database
management system, but are derived using other attributes. For example, average age of
students in a class.
4. Single-valued attribute: As the name suggests, they have a single value.
5. Multi-valued attribute: And, they can have multiple values.
Keys
If the attribute roll no. can uniquely identify a student entity, amongst all the students, then the
attribute roll no. will be said to be a key.
Following are the types of Keys:
1. Super Key
2. Candidate Key
3. Primary Key
Database Keys
Keys are very important part of Relational database model. They are used to establish and identify
relationships between tables and to uniquely identify any record or row of data inside a table. A
Key can be a single attribute or a group of attributes, where the combination may act as a key.
Why we need a Key?
In real world applications, number of tables required for storing the data is huge, and the different
tables are related to each other as well. In addition, tables store many data in them. Tables generally
extends to thousands of records stored in them, unsorted and unorganized. Now to fetch any
particular record from such dataset, you will have to apply some conditions, but what if there is
duplicate data present and every time you try to fetch some data by applying certain condition, you
get the wrong data. How many trials before you get the right data? To avoid all this, Keys are
defined to easily identify any row of data in a table.
Example.
student_id name phone age
1 Akon 9876723452 17
2 Akon 9991165674 19
3 Bkon 7898756543 18
4 Ckon 8987867898 19
5 Dkon 9990080080 17
0300-2038128 Page | 12
Compiled by Dr. Ghulam Gilanie Janjua
Let's take a simple Student table, with fields student_id, name, phone and age.
Super Key
Super Key is defined as a set of attributes within a table that can uniquely identify each record
within a table. Super Key is a superset of Candidate key. In the table defined above super key
would include student_id, (student_id, name), phone etc. The first one is simple as student_id is
unique for every row of data, hence it can be used to identity each row uniquely. Next
comes, (student_id, name), now name of two students can be same, but their student_id can't be
same hence this combination can also be a key. Similarly, phone number for every student will be
unique, hence again; phone can also be a key. Therefore, they all are super keys.
Candidate Key
Candidate keys are defined as the minimal set of fields, which can uniquely identify each record
in a table. It is an attribute or a set of attributes that can act as a Primary Key for a table to uniquely
identify each record in that table. There can be more than one candidate key.
In our example, student_id and phone both are candidate keys for table Student.
A candidate key can never be NULL or empty. In addition, its value should be unique.
There can be more than one candidate keys for a table.
A candidate key can be a combination of more than one columns (attributes).
Primary Key
Primary key is a candidate key that is most appropriate to become the main key for any table. It is
a key that can uniquely identify each record in a table.
For the table Student we can make the student_id column as the primary key.
Composite Key
Key that consists of two or more attributes that uniquely identify any record in a table is
called Composite key. However, the attributes, which together form the Composite key, are not a
key independently or individually.
0300-2038128 Page | 13
Compiled by Dr. Ghulam Gilanie Janjua
In the above picture, we have a Score table, which stores the marks scored by a student in a
particular subject. In this table, student_id and subject_id together will form the primary key;
hence, it is a composite key.
Secondary or Alternative key
The candidate key, which are not selected as primary key, are known as secondary keys or
alternative keys.
Non-key Attributes
Non-key attributes are the attributes or fields of a table, other than candidate key attributes/fields
in a table.
Non-prime Attributes
Non-prime Attributes are attributes other than Primary Key attribute(s).
Relationships
When an Entity is related to another Entity, they are said to have a relationship. For example,
A Class Entity is related to Student entity, because students study in classes, hence this is a
relationship. Depending upon the number of entities involved, a degree is assigned to
relationships. For example, if 2 entities are involved, it is said to be Binary relationship, if 3
entities are involved, it is said to be Ternary relationship, and so on.
Codd's Rule for Relational DBMS
E.F Codd was a Computer Scientist who invented the Relational model for Database
management. Based on relational model, the Relational database was created. Codd proposed 13
rules popularly known as Codd's 12 rules to test DBMS's concept against his relational model.
Codd's rule actually define what quality a DBMS requires in order to become a Relational Database
Management System (RDBMS). Until now, hardly any commercial product follows all the 13
Codd's rules. Even Oracle follows only eight and half (8.5) out of 13. The Codd's 12 rules are as
follows.
Rule zero
This rule states that for a system to qualify as an RDBMS, it must be able to manage database
entirely through the relational capabilities.
0300-2038128 Page | 14
Compiled by Dr. Ghulam Gilanie Janjua
Rule 1: Information rule
All information (including metadata) is to be represented as stored data in cells of tables. The rows
and columns have to be strictly unordered.
Rule 2: Guaranteed Access
Each unique piece of data (atomic value) should be accessible by Table Name + Primary Key
(Row) + Attribute (column).
NOTE: Ability to directly access via POINTER is a violation of this rule.
Rule 3: Systematic treatment of NULL
Null has several meanings; it can mean missing data, not applicable or no value. It should be
handled consistently. In addition, Primary key must not be null, ever. Expression on NULL must
give null.
Rule 4: Active Online Catalog
Database dictionary (catalog) is the structure description of the complete Database and it must be
stored online. The Catalog must be governed by same rules as rest of the database. The same query
language should be used on catalog as used to query database.
Rule 5: Powerful and Well-Structured Language
One well-structured language must be there to provide all manners of access to the data stored in
the database. Example: SQL, etc. If the database allows access to the data without the use of this
language, then that is a violation.
Rule 6: View Updation Rule
All the view that are theoretically updatable should be updatable by the system as well.
Rule 7: Relational Level Operation
There must be Insert, Delete, and Update operations at each level of relations. Set operation like
Union, Intersection and minus should also be supported.
Rule 8: Physical Data Independence
The physical storage of data should not matter to the system. If say, some file supporting table is
renamed or moved from one disk to another, it should not effect the application.
Rule 9: Logical Data Independence
If there is, change in the logical structure (table structures) of the database the user view of data
should not change. Say, if a table is split into two tables, a new view should give result as the join
of the two tables. This rule is most difficult to satisfy.
Rule 10: Integrity Independence
The database should be able to enforce its own integrity rather than using other programs. Key and
Check constraints, trigger etc., should be stored in Data Dictionary. This also
make RDBMS independent of front-end.
Rule 11: Distribution Independence
A database should work properly regardless of its distribution across a network. Even if a database
is geographically distributed, with data stored in pieces, the end user should get an impression that
it is stored at the same place. This lays the foundation of distributed database.
Rule 12: No subversion Rule
If low-level access is allowed to a system, it should not be able to subvert or bypass integrity rules
to change the data. Some sort of looking or encryption can achieve this.
What is Relational Algebra?
Every database management system must define a query language to allow users to access the data
stored in the database. Relational Algebra is a procedural query language used to query the
database tables to access data in different ways. In relational algebra, input is a relation (table from
0300-2038128 Page | 15
Compiled by Dr. Ghulam Gilanie Janjua
which data has to be accessed) and output is a relation (a temporary table holding the data asked
for by the user).
Relational Algebra works overall table at once, so we do not have to use loops etc. to iterate over
all the rows (tuples) of data one by one. All we have to do is specify the table name from which
we need the data, and in a single line of command, relational algebra will traverse the entire given
table to fetch data for you.
The primary operations that we can perform using relational algebra are:
1. Select
2. Project
3. Union
4. Set Different
5. Cartesian product
6. Rename
Select Operation (σ)
This is used to fetch rows (tuples) from table (relation) which satisfies a given condition.
Syntax: σp(r)
Where, σ represents the Select Predicate, r is the name of relation (table name in which you want
to look for data), and p is the prepositional logic, where we specify the conditions that must be
satisfied by the data. In prepositional logic, one can use unary and binary operators
like =, <, > etc, to specify the conditions.
Let's take an example of the Student table we specified above in the Introduction of relational
algebra, and fetch data for students with age more than 17.
σage > 17 (Student)
This will fetch the tuples(rows) from table Student, for which age will be greater than 17.
You can also use, and, or etc operators, to specify two conditions, for example,
σage > 17 and gender = 'Male' (Student)
This will return tuples (rows) from table Student with information of male students, of age more
than 17. (Consider the Student table has an attribute Gender too.)
Project Operation (∏)
0300-2038128 Page | 16
Compiled by Dr. Ghulam Gilanie Janjua
Project operation is used to project only a certain set of attributes of a relation. In simple words, If
you want to see only the names all of the students in the Student table, then you can use Project
Operation.
It will only project or show the columns or attributes asked for, and will also remove duplicate
data from the columns.
Syntax: ∏A1, A2...(r)
where A1, A2 etc are attribute names(column names).
For example,
∏Name, Age(Student)
Above statement will show us only the Name and Age columns for all the rows of data
in Student table.
Union Operation (∪)
This operation is used to fetch data from two relations (tables) or temporary relation (result of
another operation).
For this operation to work, the relations (tables) specified should have same number of attributes
(columns) and same attribute domain. In addition, the duplicate tuples are automatically eliminated
from the result.
Syntax: A ∪ B
where A and B are relations.
For example, if we have two tables RegularClass and ExtraClass, both have a column student to
save name of student, then,
∏Student(RegularClass) ∪ ∏Student(ExtraClass)
Above operation will give us name of Students who are attending both regular classes and extra
classes, eliminating repetition.
Set Difference (-)
This operation is used to find data present in one relation and not present in the second relation.
This operation is also applicable on two relations, just like Union operation.
Syntax: A - B
where A and B are relations.
For example, if we want to find name of students who attend the regular class but not the extra
class, then, we can use the below operation:
∏Student(RegularClass) - ∏Student(ExtraClass)
Cartesian product (X)
This is used to combine data from two different relations (tables) into one and fetch data from the
combined relation.
Syntax: A X B
For example, if we want to find the information for Regular Class and Extra Class, which are
conducted during morning, then, we can use the following operation:
σtime = 'morning' (RegularClass X ExtraClass)
For the above query to work, both RegularClass and ExtraClass should have the attribute time.
Rename Operation (ρ)
This operation is used to rename the output relation for any query operation, which returns result
like Select, Project etc. Or to simply rename a relation (table)
Syntax: ρ(RelationNew, RelationOld)
Apart from these common operations Relational Algebra is also used for Join operations like,
Natural Join
0300-2038128 Page | 17
Compiled by Dr. Ghulam Gilanie Janjua
Outer Join
Theta join etc.
Introduction to SQL
Structure Query Language (SQL) is a database query language used for storing and managing data
in Relational DBMS. SQL was the first commercial language introduced for E.F
Codd's Relational model of database. Today almost all RDBMS (MySql, Oracle, Infomix,
Sybase, MS Access) use SQL as the standard database query language. SQL is used to perform all
types of data operations in RDBMS.
SQL Command
SQL defines following ways to manipulate data stored in an RDBMS.
DDL: Data Definition Language
This includes changes to the structure of the table like creation of table, altering table, deleting a
table etc.
All DDL commands are auto-committed. That means it saves all the changes permanently in the
database.
Command Description
0300-2038128 Page | 18
Compiled by Dr. Ghulam Gilanie Janjua
commit to permanently save
0300-2038128 Page | 19
Compiled by Dr. Ghulam Gilanie Janjua
Universal Set: set of all possible values
(in the area of interest)
P(A) Power Set: all subsets of A P({1, 2}) = { {}, {1}, {2}, {1, 2} }
A=B Equality: both sets have the same members {3, 4, 5} = {5, 3, 4}
A×B Cartesian Product {1, 2} × {3, 4}
(set of ordered pairs from A and B) = {(1, 3), (1, 4), (2, 3), (2, 4)}
|A| Cardinality: the number of elements of set A |{3, 4}| = 2
| Such that { n | n > 0 } = {1, 2, 3,...}
: Such that { n : n > 0 } = {1, 2, 3,...}
∀ For All ∀x>1, x2>x
∃ There Exists ∃ x | x2>x
∴ Therefore a=b ∴ b=a
Relational Algebra
There are two kinds of query languages, i.e., relational algebra and relational calculus.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input and
yields instances of relations as output. It uses operators to perform queries. An operator can be
either unary or binary. They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate results are also
considered relations.
The fundamental operations of relational algebra are as follows −
Select
Project
Union
Set different
Cartesian product
Rename
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic formula
which may use connectors like and, or, and not. These terms may use relational operators like −
=, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those books
published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏ A 1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
0300-2038128 Page | 20
Compiled by Dr. Ghulam Gilanie Janjua
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not in the
second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'Janjua'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by Janjua.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename operation
allows us to rename the output relation. 'rename' operation is denoted with small Greek letter ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that is,
it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on 'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
0300-2038128 Page | 21
Compiled by Dr. Ghulam Gilanie Janjua
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as done
in TRC, mentioned above).
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ MyBooks ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation MyBooks, where subject is
database.
Data Independence
A database system normally contains many data in addition to users’ data. For example, it stores
data about data, known as metadata, to locate and retrieve data easily. It is rather difficult to
modify or update a set of metadata once it is stored in the database. However, as a DBMS expands,
it needs to change over time to satisfy the requirements of the users. If the entire data were
dependent, it would become a tedious and highly complex job.
Metadata itself follows a layered architecture, so that when we change data at one layer, it does
not affect the data at another level. This data is independent but mapped to each other.
Logical Data Independence
Logical data is data about database, that is, it stores information about how data is managed inside.
For example, a table (relation) stored in the database and all its constraints, applied on that
relation.
Logical data independence is a kind of mechanism, which liberalizes itself from actual data stored
on the disk. If we do some changes on table format, it should not change the data residing on the
disk.
Physical Data Independence
All the schemas are logical, and the actual data is stored in bit format on the disk. Physical data
independence is the power to change the physical data without impacting the schema or logical
data.
For example, in case we want to change or upgrade the storage system itself − suppose we want
to replace hard-disks with SSD − it should not have any impact on the logical data or schemas.
Generalization
0300-2038128 Page | 22
Compiled by Dr. Ghulam Gilanie Janjua
As mentioned above, the process of generalizing entities, where the generalized entities contain
the properties of all the generalized entities, is called generalization. In generalization, a number
of entities are brought together into one generalized entity based on their similar characteristics.
For example, pigeon, house sparrow, crow and dove can all be generalized as Birds.
Specialization
Specialization is the opposite of generalization. In specialization, a group of entities is divided
into sub-groups based on their characteristics. Take a group ‘Person’ for example. A person has
name, date of birth, gender, etc. These properties are common in all persons, human beings. But
in a company, persons can be identified as employee, employer, customer, or vendor, based on
what role they play in the company.
Similarly, in a school database, persons can be specialized as teacher, student, or a staff, based on
what role they play in school as entities.
Inheritance
We use all the above features of ER-Model in order to create classes of objects in object-oriented
programming. The details of entities are generally hidden from the user; this process known
as abstraction.
Inheritance is an important feature of Generalization and Specialization. It allows lower-level
entities to inherit the attributes of higher-level entities.
0300-2038128 Page | 23
Compiled by Dr. Ghulam Gilanie Janjua
For example, the attributes of a Person class such as name, age, and gender can be inherited by
lower-level entities such as Student or Teacher.
Relation Data Model
Relational data model is the primary data model, which is used widely around the world for data
storage and processing. This model is simple and it has all the properties and capabilities required
to process data with storage efficiency.
Concepts
Tables − In relational data model, relations are saved in the format of Tables. This format stores
the relation among entities. A table has rows and columns, where rows represents records and
columns represent the attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system represents relation
instance. Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and
their names.
Relation key − Each row has one or more attributes, known as relation key, which can identify
the row in the relation (table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope, known as attribute
domain.
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These conditions
are called Relational Integrity Constraints. There are three main integrity constraints −
Key constraints
Domain constraints
Referential integrity constraints
0300-2038128 Page | 24
Compiled by Dr. Ghulam Gilanie Janjua
Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there are more than
one such minimal subsets, these are called candidate keys.
Key constraints force that −
in a relation with a key attribute, no two tuples can have identical values for key attributes.
a key attribute can not have NULL values.
Key constraints are also referred to as Entity Constraints.
Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a positive
integer. The same constraints have been tried to employ on the attributes of a relation. Every
attribute is bound to have a specific range of values. For example, age cannot be less than zero
and telephone numbers cannot contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key
attribute of a relation that can be referred in other relation.
Referential integrity constraint states that if a relation refers to a key attribute of a different or
same relation, then that key element must exist.
ER Model to Relational Model
ER Model, when conceptualized into diagrams, gives a good overview of entity-relationship,
which is easier to understand. ER diagrams can be mapped to relational schema, that is, it is
possible to create relational schema using ER diagram. We cannot import all the ER constraints
into relational model, but an approximate schema can be generated.
There are several processes and algorithms available to convert ER Diagrams into Relational
Schema. Some of them are automated and some of them are manual. We may focus here on the
mapping diagram contents to relational basics.
ER diagrams mainly comprise of −
Entity and its attributes
Relationship, which is association among entities.
Mapping Entity
An entity is a real-world object with some attributes.
0300-2038128 Page | 25
Compiled by Dr. Ghulam Gilanie Janjua
Mapping Process
Create table for a relationship.
Add the primary keys of all participating Entities as fields of table with their respective
data types.
If relationship has any attribute, add each attribute as field of table.
Declare a primary key composing all the primary keys of participating entities.
Declare all foreign key constraints.
Mapping Weak Entity Sets
A weak entity set is one, which does not have any primary key associated with it.
Mapping Process
Create table for weak entity set.
Add all its attributes to table as field.
Add the primary key of identifying entity set.
Declare all foreign key constraints.
Mapping Hierarchical Entities
ER specialization or generalization comes in the form of hierarchical entity sets.
0300-2038128 Page | 26
Compiled by Dr. Ghulam Gilanie Janjua
Mapping Process
Create tables for all higher-level entities.
Create tables for lower-level entities.
Add primary keys of higher-level entities in the table of lower-level entities.
In lower-level tables, add all other attributes of lower-level entities.
Declare primary key of higher-level table and the primary key for lower-level table.
Declare foreign key constraints.
SQL
SQL is a programming language for Relational Databases. It is designed over relational algebra
and tuple relational calculus. SQL comes as a package with all major distributions of RDBMS.
SQL comprises both data definition and data manipulation languages. Using the data definition
properties of SQL, one can design and modify database schema, whereas data manipulation
properties allows SQL to store and retrieve data from database.
Data Definition Language
SQL uses the following set of commands to define database schema −
CREATE
Creates new databases, tables and views from RDBMS.
For example −
Create database mydb;
Create table article;
Create view for_students;
DROP
Drops commands, views, tables, and databases from RDBMS.
For example−
Drop object_type object_name;
0300-2038128 Page | 27
Compiled by Dr. Ghulam Gilanie Janjua
Drop database mydb;
Drop table article;
Drop view for_students;
ALTER
Modifies database schema.
Alter object_type object_name parameters;
For example−
Alter table article add subject varchar;
This command adds an attribute in the relation article with the name subject of string type.
Data Manipulation Language
SQL is equipped with data manipulation language (DML). DML modifies the database instance
by inserting, updating and deleting its data. DML is responsible for all forms data modification
in a database. SQL contains the following set of commands in its DML section −
SELECT/FROM/WHERE
INSERT INTO/VALUES
UPDATE/SET/WHERE
DELETE FROM/WHERE
These basic constructs allow database programmers and users to enter data and information into
the database and retrieve efficiently using a number of filter options.
SELECT/FROM/WHERE
SELECT − This is one of the fundamental query command of SQL. It is similar to the
projection operation of relational algebra. It selects the attributes based on the condition
described by WHERE clause.
FROM − This clause takes a relation name as an argument from which attributes are to
be selected/projected. In case more than one relation names are given, this clause
corresponds to Cartesian product.
WHERE − This clause defines predicate or conditions, which must match in order to
qualify the attributes to be projected.
For example −
Select author_name
From book_author
Where age > 50;
This command will yield the names of authors from the relation book_author whose age is
greater than 50.
INSERT INTO/VALUES
This command is used for inserting values into the rows of a table (relation).
Syntax−
INSERT INTO table (column1 [, column2, column3 ... ]) VALUES (value1 [, value2, value3 ... ])
Or
INSERT INTO table VALUES (value1, [value2, ... ])
For example −
INSERT INTO mytbl (Author, Subject) VALUES ("anonymous", "computers");
UPDATE/SET/WHERE
This command is used for updating or modifying the values of columns in a table (relation).
Syntax −
0300-2038128 Page | 28
Compiled by Dr. Ghulam Gilanie Janjua
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE
condition]
For example −
UPDATE mytbl SET Author="webmaster" WHERE Author="anonymous";
DELETE/FROM/WHERE
This command is used for removing one or more rows from a table (relation).
Syntax −
DELETE FROM table_name [WHERE condition];
For example −
DELETE FROM mytbl WHERE Author="unknown";
Normalization
Functional Dependency
Functional dependency (FD) is a set of constraints between two attributes in a relation. Functional
dependency says that if two tuples have same values for attributes A1, A2,..., An, then those two
tuples must have to have same values for attributes B1, B2, ..., Bn.
Functional dependency is represented by an arrow sign (→) that is, X→Y, where X functionally
determines Y. The left-hand side attributes determine the values of attributes on the right-hand
side.
Armstrong's Axioms
If F is a set of functional dependencies then the closure of F, denoted as F+, is the set of all
functional dependencies logically implied by F. Armstrong's Axioms are a set of rules, that when
applied repeatedly, generates a closure of functional dependencies.
Reflexive rule − If alpha is a set of attributes and beta is_subset_of alpha, then alpha holds
beta.
Augmentation rule − If a → b holds and y is attribute set, then ay → by also holds. That
is adding attributes in dependencies, does not change the basic dependencies.
Transitivity rule − Same as transitive rule in algebra, if a → b holds and b → c holds,
then a → c also holds. a → b is called as a functionally that determines b.
Trivial Functional Dependency
Trivial − If a functional dependency (FD) X → Y holds, where Y is a subset of X, then it
is called a trivial FD. Trivial FDs always hold.
Non-trivial − If an FD X → Y holds, where Y is not a subset of X, then it is called a non-
trivial FD.
Completely non-trivial − If an FD X → Y holds, where x intersect Y = Φ, it is said to be
a completely non-trivial FD.
Normalization
If a database design is not perfect, it may contain anomalies, which are like a bad dream for any
database administrator. Managing a database with anomalies is next to impossible.
Update anomalies − If data items are scattered and are not linked to each other properly,
then it could lead to strange situations. For example, when we try to update one data item
having its copies scattered over several places, a few instances get updated properly while
a few others are left with old values. Such instances leave the database in an inconsistent
state.
Deletion anomalies − We tried to delete a record, but parts of it was left undeleted because
of unawareness, the data is also saved somewhere else.
Insert anomalies − We tried to insert data in a record that does not exist at all.
0300-2038128 Page | 29
Compiled by Dr. Ghulam Gilanie Janjua
Normalization is a method to remove all these anomalies and bring the database to a consistent
state.
First Normal Form
First Normal Form is defined in the definition of relations (tables) itself. This rule defines that all
the attributes in a relation must have atomic domains. The values in an atomic domain are
indivisible units.
Each attribute must contain only a single value from its pre-defined domain.
Second Normal Form
Before we learn about the second normal form, we need to understand the following −
Prime attribute − An attribute, which is a part of the candidate-key, is known as a prime
attribute.
Non-prime attribute − An attribute, which is not a part of the prime-key, is said to be a
non-prime attribute.
If we follow second normal form, then every non-prime attribute should be fully functionally
dependent on prime key attribute. That is, if X → A holds, then there should not be any proper
subset Y of X, for which Y → A also holds true.
We see here in Student_Project relation that the prime key attributes are Stu_ID and Proj_ID.
According to the rule, non-key attributes, i.e. Stu_Name and Proj_Name must be dependent upon
both and not on any of the prime key attribute individually. But we find that Stu_Name can be
0300-2038128 Page | 30
Compiled by Dr. Ghulam Gilanie Janjua
identified by Stu_ID and Proj_Name can be identified by Proj_ID independently. This is
called partial dependency, which is not allowed in Second Normal Form.
We broke the relation in two as depicted in the above picture. Therefore, there exists no partial
dependency.
Third Normal Form
For a relation to be in Third Normal Form, it must be in Second Normal form and the following
must satisfy −
No non-prime attribute is transitively dependent on prime key attribute.
For any non-trivial functional dependency, X → A, then either −
o X is a superkey or,
o A is prime attribute.
We find that in the above Student_detail relation, Stu_ID is the key and only prime key attribute.
We find that City can be identified by Stu_ID as well as Zip itself. Neither Zip is a superkey nor
is City a prime attribute. Additionally, Stu_ID → Zip → City, so there exists transitive
dependency.
To bring this relation into third normal form, we break the relation into two relations as follows
−
0300-2038128 Page | 31
Compiled by Dr. Ghulam Gilanie Janjua
Boyce-Codd Normal Form (BCNF) is an extension of Third Normal Form on strict terms. BCNF
states that −
For any non-trivial functional dependency, X → A, X must be a super-key.
In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is the super-
key in the relation ZipCodes. So,
Stu_ID → Stu_Name, Zip
and
Zip → City
Which confirms that both the relations are in BCNF.
Joins
We understand the benefits of taking a Cartesian product of two relations, which gives us all the
possible tuples that are paired together. But it might not be feasible for us in certain cases to take
a Cartesian product where we encounter huge relations with thousands of tuples having a
considerable large number of attributes.
Join is a combination of a Cartesian product followed by a selection process. A Join operation
pairs two tuples from different relations, if and only if a given join condition is satisfied.
We will briefly describe various join types in the following sections.
Theta (θ) Join
Theta join combines tuples from different relations provided they satisfy the theta condition. The
join condition is denoted by the symbol θ.
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
Student
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −
STUDENT ⋈Student.Std = Subject.Class SUBJECT
0300-2038128 Page | 32
Compiled by Dr. Ghulam Gilanie Janjua
Student_detail
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
HoD
Dept Head
CS Alex
ME Maya
EE Mira
Courses ⋈ HoD
0300-2038128 Page | 33
Compiled by Dr. Ghulam Gilanie Janjua
ME ME01 Mechanics Maya
A B
100 Database
101 Mechanics
102 Electronics
Right
A B
100 Alex
102 Maya
104 Mira
Courses HoD
A B C D
0300-2038128 Page | 34
Compiled by Dr. Ghulam Gilanie Janjua
Courses HoD
A B C D
A B C D
Storage System
Databases are stored in file formats, which contain records. At physical level, the actual data is
stored in electromagnetic format on some device. These storage devices can be broadly
categorized into three types −
Primary Storage − The memory storage that is directly accessible to the CPU comes
under this category. CPU's internal memory (registers), fast memory (cache), and main
memory (RAM) are directly accessible to the CPU, as they are all placed on the
motherboard or CPU chipset. This storage is typically very small, ultra-fast, and volatile.
0300-2038128 Page | 35
Compiled by Dr. Ghulam Gilanie Janjua
Primary storage requires continuous power supply in order to maintain its state. In case of
a power failure, all its data is lost.
Secondary Storage − Secondary storage devices are used to store data for future use or
as backup. Secondary storage includes memory devices that are not a part of the CPU
chipset or motherboard, for example, magnetic disks, optical disks (DVD, CD, etc.), hard
disks, flash drives, and magnetic tapes.
Tertiary Storage − Tertiary storage is used to store huge volumes of data. Since such
storage devices are external to the computer system, they are the slowest in speed. These
storage devices are mostly used to take the back up of an entire system. Optical disks and
magnetic tapes are widely used as tertiary storage.
Memory Hierarchy
A computer system has a well-defined hierarchy of memory. A CPU has direct access to it main
memory as well as its inbuilt registers. The access time of the main memory is obviously less than
the CPU speed. To minimize this speed mismatch, cache memory is introduced. Cache memory
provides the fastest access time and it contains data that is most frequently accessed by the CPU.
The memory with the fastest access is the costliest one. Larger storage devices offer slow speed
and they are less expensive, however they can store huge volumes of data as compared to CPU
registers or cache memory.
Magnetic Disks
Hard disk drives are the most common secondary storage devices in present computer systems.
These are called magnetic disks because they use the concept of magnetization to store
information. Hard disks consist of metal disks coated with magnetizable material. These disks are
placed vertically on a spindle. A read/write head moves in between the disks and is used to
magnetize or de-magnetize the spot under it. A magnetized spot can be recognized as 0 (zero) or
1 (one).
Hard disks are formatted in a well-defined order to store data efficiently. A hard disk plate has
many concentric circles on it, called tracks. Every track is further divided into sectors. A sector
on a hard disk typically stores 512 bytes of data.
Redundant Array of Independent Disks
RAID or Redundant Array of Independent Disks, is a technology to connect multiple secondary
storage devices and use them as a single storage media.
RAID consists of an array of disks in which multiple disks are connected together to achieve
different goals. RAID levels define the use of disk arrays.
RAID 0
In this level, a striped array of disks is implemented. The data is broken down into blocks and the
blocks are distributed among disks. Each disk receives a block of data to write/read in parallel. It
enhances the speed and performance of the storage device. There is no parity and backup in Level
0.
0300-2038128 Page | 36
Compiled by Dr. Ghulam Gilanie Janjua
RAID 1
RAID 1 uses mirroring techniques. When data is sent to a RAID controller, it sends a copy of
data to all the disks in the array. RAID level 1 is also called mirroring and provides 100%
redundancy in case of a failure.
RAID 2
RAID 2 records Error Correction Code using Hamming distance for its data, striped on different
disks. Like level 0, each data bit in a word is recorded on a separate disk and ECC codes of the
data words are stored on a different set disks. Due to its complex structure and high cost, RAID
2 is not commercially available.
RAID 3
RAID 3 stripes the data onto multiple disks. The parity bit generated for data word is stored on a
different disk. This technique makes it to overcome single disk failures.
RAID 4
In this level, an entire block of data is written onto data disks and then the parity is generated and
stored on a different disk. Note that level 3 uses byte-level striping, whereas level 4 uses block-
level striping. Both level 3 and level 4 require at least three disks to implement RAID.
0300-2038128 Page | 37
Compiled by Dr. Ghulam Gilanie Janjua
RAID 5
RAID 5 writes whole data blocks onto different disks, but the parity bits generated for data block
stripe are distributed among all the data disks rather than storing them on a different dedicated
disk.
RAID 6
RAID 6 is an extension of level 5. In this level, two independent parities are generated and stored
in distributed fashion among multiple disks. Two parities provide additional fault tolerance. This
level requires at least four disk drives to implement RAID.
File Structure
Relative data and information is stored collectively in file formats. A file is a sequence of records
stored in binary format. A disk drive is formatted into several blocks that can store records. File
records are mapped onto those disk blocks.
File Organization
File Organization defines how file records are mapped onto disk blocks. We have four types of
File Organization to organize file records −
0300-2038128 Page | 38
Compiled by Dr. Ghulam Gilanie Janjua
0300-2038128 Page | 39
Compiled by Dr. Ghulam Gilanie Janjua
read only. Files opened in read mode can be shared among several entities. Write mode
allows data modification. Files opened in write mode can be read but cannot be shared.
Locate − Every file has a file pointer, which tells the current position where the data is to
be read or written. This pointer can be adjusted accordingly. Using find (seek) operation,
it can be moved forward or backward.
Read − By default, when files are opened in read mode, the file pointer points to the
beginning of the file. There are options where the user can tell the operating system where
to locate the file pointer at the time of opening a file. The very next data to the file pointer
is read.
Write − User can select to open a file in write mode, which enables them to edit its
contents. It can be deletion, insertion, or modification. The file pointer can be located at
the time of opening or can be dynamically changed if the operating system allows to do
so.
Close − This is the most important operation from the operating system’s point of view.
When a request to close a file is generated, the operating system
o removes all the locks (if in shared mode),
o saves the data (if altered) to the secondary storage media, and
o releases all the buffers and file handlers associated with the file.
The organization of data inside a file plays a major role here. The process to locate the file pointer
to a desired record inside a file various based on whether the records are arranged sequentially or
clustered.
Indexing
We know that data is stored in the form of records. Every record has a key field, which helps it
to be recognized uniquely.
Indexing is a data structure technique to efficiently retrieve records from the database files based
on some attributes on which the indexing has been done. Indexing in database systems is similar
to what we see in books.
Indexing is defined based on its indexing attributes. Indexing can be of the following types −
Primary Index − Primary index is defined on an ordered data file. The data file is ordered
on a key field. The key field is generally the primary key of the relation.
Secondary Index − Secondary index may be generated from a field which is a candidate
key and has a unique value in every record, or a non-key with duplicate values.
Clustering Index − Clustering index is defined on an ordered data file. The data file is
ordered on a non-key field.
Ordered Indexing is of two types −
Dense Index
Sparse Index
Dense Index
In dense index, there is an index record for every search key value in the database. This makes
searching faster but requires more space to store index records itself. Index records contain search
key value and a pointer to the actual record on the disk.
0300-2038128 Page | 40
Compiled by Dr. Ghulam Gilanie Janjua
Sparse Index
In sparse index, index records are not created for every search key. An index record here contains
a search key and an actual pointer to the data on the disk. To search a record, we first proceed by
index record and reach at the actual location of the data. If the data we are looking for is not where
we directly reach by following the index, then the system starts sequential search until the desired
data is found.
Multilevel Index
Index records comprise search-key values and data pointers. Multilevel index is stored on the disk
along with the actual database files. As the size of the database grows, so does the size of the
indices. There is an immense need to keep the index records in the main memory so as to speed
up the search operations. If single-level index is used, then a large size index cannot be kept in
memory which leads to multiple disk accesses.
0300-2038128 Page | 41
Compiled by Dr. Ghulam Gilanie Janjua
Multi-level Index helps in breaking down the index into several smaller indices in order to make
the outermost level so small that it can be saved in a single disk block, which can easily be
accommodated anywhere in the main memory.
B+ Tree
A B+ tree is a balanced binary search tree that follows a multi-level index format. The leaf nodes
of a B+ tree denote actual data pointers. B+ tree ensures that all leaf nodes remain at the same
height, thus balanced. Additionally, the leaf nodes are linked using a link list; therefore, a B+ tree
can support random access as well as sequential access.
Structure of B+ Tree
Every leaf node is at equal distance from the root node. A B+ tree is of the order n where n is
fixed for every B+ tree.
0300-2038128 Page | 42
Compiled by Dr. Ghulam Gilanie Janjua
Internal nodes −
Internal (non-leaf) nodes contain at least ⌈n/2⌉ pointers, except the root node.
At most, an internal node can contain n pointers.
Leaf nodes −
Leaf nodes contain at least ⌈n/2⌉ record pointers and ⌈n/2⌉ key values.
At most, a leaf node can contain n record pointers and n key values.
Every leaf node contains one block pointer P to point to next leaf node and forms a linked
list.
B+ Tree Insertion
B+ trees are filled from bottom and each entry is done at the leaf node.
If a leaf node overflows −
o Split node into two parts.
o Partition at i = ⌊(m+1)/2⌋.
o First i entries are stored in one node.
o Rest of the entries (i+1 onwards) are moved to a new node.
o ith key is duplicated at the parent of the leaf.
If a non-leaf node overflows −
o Split node into two parts.
o Partition the node at i = ⌈(m+1)/2⌉.
o Entries up to i are kept in one node.
o Rest of the entries are moved to a new node.
B+ Tree Deletion
B+ tree entries are deleted at the leaf nodes.
The target entry is searched and deleted.
o If it is an internal node, delete and replace with the entry from the left position.
After deletion, underflow is tested,
o If underflow occurs, distribute the entries from the nodes left to it.
If distribution is not possible from left, then
o Distribute from the nodes right to it.
If distribution is not possible from left or from right, then
o Merge the node with left and right to it.
Hashing
For a huge database structure, it can be almost next to impossible to search all the index values
through all its level and then reach the destination data block to retrieve the desired data. Hashing
is an effective technique to calculate the direct location of a data record on the disk without using
index structure.
Hashing uses hash functions with search keys as parameters to generate the address of a data
record.
Hash Organization
Bucket − A hash file stores data in bucket format. Bucket is considered a unit of storage.
A bucket typically stores one complete disk block, which in turn can store one or more
records.
Hash Function − A hash function, h, is a mapping function that maps all the set of search-
keys K to the address where actual records are placed. It is a function from search keys to
bucket addresses.
Static Hashing
0300-2038128 Page | 43
Compiled by Dr. Ghulam Gilanie Janjua
In static hashing, when a search-key value is provided, the hash function always computes the
same address. For example, if mod-4 hash function is used, then it shall generate only 5 values.
The output address shall always be same for that function. The number of buckets provided
remains unchanged at all times.
Operation
Insertion − When a record is required to be entered using static hash, the hash
function h computes the bucket address for search key K, where the record will be stored.
Bucket address = h(K)
Search − When a record needs to be retrieved, the same hash function can be used to
retrieve the address of the bucket where the data is stored.
Delete − This is simply a search followed by a deletion operation.
Bucket Overflow
The condition of bucket-overflow is known as collision. This is a fatal state for any static hash
function. In this case, overflow chaining can be used.
Overflow Chaining − When buckets are full, a new bucket is allocated for the same hash
result and is linked after the previous one. This mechanism is called Closed Hashing.
0300-2038128 Page | 44
Compiled by Dr. Ghulam Gilanie Janjua
Linear Probing − When a hash function generates an address at which data is already
stored, the next free bucket is allocated to it. This mechanism is called Open Hashing.
Dynamic Hashing
The problem with static hashing is that it does not expand or shrink dynamically as the size of the
database grows or shrinks. Dynamic hashing provides a mechanism in which data buckets are
added and removed dynamically and on-demand. Dynamic hashing is also known as extended
hashing.
Hash function, in dynamic hashing, is made to produce a large number of values and only a few
are used initially.
0300-2038128 Page | 45
Compiled by Dr. Ghulam Gilanie Janjua
Organization
The prefix of an entire hash value is taken as a hash index. Only a portion of the hash value is
used for computing bucket addresses. Every hash index has a depth value to signify how many
bits are used for computing a hash function. These bits can address 2n buckets. When all these
bits are consumed − that is, when all the buckets are full − then the depth value is increased
linearly and twice the buckets are allocated.
Operation
Querying − Look at the depth value of the hash index and use those bits to compute the
bucket address.
Update − Perform a query as above and update the data.
Deletion − Perform a query to locate the desired data and delete the same.
Insertion − Compute the address of the bucket
o If the bucket is already full.
Add more buckets.
Add additional bits to the hash value.
Re-compute the hash function.
o Else
Add data to the bucket,
o If all the buckets are full, perform the remedies of static hashing.
0300-2038128 Page | 46
Compiled by Dr. Ghulam Gilanie Janjua
Hashing is not favorable when the data is organized in some ordering and the queries require a
range of data. When data is discrete and random, hash performs the best.
Hashing algorithms have high complexity than indexing. All hash operations are done in constant
time.
Transaction
A transaction can be defined as a group of tasks. A single task is the minimum processing unit
which cannot be divided further.
Let’s take an example of a simple transaction. Suppose a bank employee transfers Rs 500 from
A's account to B's account. This very simple and small transaction involves several low-level
tasks.
A’s Account
Open_Account(A)
Old_Balance = A.balance
New_Balance = Old_Balance - 500
A.balance = New_Balance
Close_Account(A)
B’s Account
Open_Account(B)
Old_Balance = B.balance
New_Balance = Old_Balance + 500
B.balance = New_Balance
Close_Account(B)
ACID Properties
A transaction is a very small unit of a program and it may contain several lowlevel tasks. A
transaction in a database system must maintain Atomicity, Consistency, Isolation, and Durability
− commonly known as ACID properties − in order to ensure accuracy, completeness, and data
integrity.
Atomicity − This property states that a transaction must be treated as an atomic unit, that
is, either all of its operations are executed or none. There must be no state in a database
where a transaction is left partially completed. States should be defined either before the
execution of the transaction or after the execution/abortion/failure of the transaction.
Consistency − The database must remain in a consistent state after any transaction. No
transaction should have any adverse effect on the data residing in the database. If the
database was in a consistent state before the execution of a transaction, it must remain
consistent after the execution of the transaction as well.
Durability − The database should be durable enough to hold all its latest updates even if
the system fails or restarts. If a transaction updates a chunk of data in a database and
commits, then the database will hold the modified data. If a transaction commits but the
system fails before the data could be written on to the disk, then that data will be updated
once the system springs back into action.
Isolation − In a database system where more than one transaction are being executed
simultaneously and in parallel, the property of isolation states that all the transactions will
be carried out and executed as if it is the only transaction in the system. No transaction
will affect the existence of any other transaction.
Serializability
0300-2038128 Page | 47
Compiled by Dr. Ghulam Gilanie Janjua
When multiple transactions are being executed by the operating system in a multiprogramming
environment, there are possibilities that instructions of one transactions are interleaved with some
other transaction.
Schedule − A chronological execution sequence of a transaction is called a schedule. A
schedule can have many transactions in it, each comprising of a number of
instructions/tasks.
Serial Schedule − It is a schedule in which transactions are aligned in such a way that one
transaction is executed first. When the first transaction completes its cycle, then the next
transaction is executed. Transactions are ordered one after the other. This type of schedule
is called a serial schedule, as transactions are executed in a serial manner.
In a multi-transaction environment, serial schedules are considered as a benchmark. The
execution sequence of an instruction in a transaction cannot be changed, but two transactions can
have their instructions executed in a random fashion. This execution does no harm if two
transactions are mutually independent and working on different segments of data; but in case
these two transactions are working on the same data, then the results may vary. This ever-varying
result may bring the database to an inconsistent state.
To resolve this problem, we allow parallel execution of a transaction schedule, if its transactions
are either serializable or have some equivalence relation among them.
Equivalence Schedules
An equivalence schedule can be of the following types −
Result Equivalence
If two schedules produce the same result after execution, they are said to be result equivalent.
They may yield the same result for some value and different results for another set of values.
That's why this equivalence is not generally considered significant.
View Equivalence
Two schedules would be view equivalence if the transactions in both the schedules perform
similar actions in a similar manner.
For example −
If T reads the initial data in S1, then it also reads the initial data in S2.
If T reads the value written by J in S1, then it also reads the value written by J in S2.
If T performs the final write on the data value in S1, then it also performs the final write
on the data value in S2.
Conflict Equivalence
Two schedules would be conflicting if they have the following properties −
Both belong to separate transactions.
Both accesses the same data item.
At least one of them is "write" operation.
Two schedules having multiple transactions with conflicting operations are said to be conflict
equivalent if and only if −
Both the schedules contain the same set of Transactions.
The order of conflicting pairs of operation is maintained in both the schedules.
Note − View equivalent schedules are view serializable and conflict equivalent schedules are
conflict serializable. All conflict serializable schedules are view serializable too.
States of Transactions
A transaction in a database can be in one of the following states −
0300-2038128 Page | 48
Compiled by Dr. Ghulam Gilanie Janjua
Active − In this state, the transaction is being executed. This is the initial state of every
transaction.
Partially Committed − When a transaction executes its final operation, it is said to be in
a partially committed state.
Failed − A transaction is said to be in a failed state if any of the checks made by the
database recovery system fails. A failed transaction can no longer proceed further.
Aborted − If any of the checks fails and the transaction has reached a failed state, then the
recovery manager rolls back all its write operations on the database to bring the database
back to its original state where it was prior to the execution of the transaction. Transactions
in this state are called aborted. The database recovery module can select one of the two
operations after a transaction aborts −
o Re-start the transaction
o Kill the transaction
Committed − If a transaction executes all its operations successfully, it is said to be
committed. All its effects are now permanently established on the database system.
Data Backup
0300-2038128 Page | 49
Compiled by Dr. Ghulam Gilanie Janjua
Recovery
When the system recovers from a failure, it can restore the latest dump.
It can maintain a redo-list and an undo-list as checkpoints.
It can recover the system by consulting undo-redo lists to restore the state of all
transactions up to the last checkpoint.
Database Backup & Recovery from Catastrophic Failure
A catastrophic failure is one where a stable, secondary storage device gets corrupt. With the
storage device, all the valuable data that is stored inside is lost. We have two different strategies
to recover data from such a catastrophic failure −
Remote backup &minu; Here a backup copy of the database is stored at a remote location
from where it can be restored in case of a catastrophe.
Alternatively, database backups can be taken on magnetic tapes and stored at a safer place.
This backup can later be transferred onto a freshly installed database to bring it to the
point of backup.
Grown-up databases are too bulky to be frequently backed up. In such cases, we have techniques
where we can restore a database just by looking at its logs. So, all that we need to do here is to
take a backup of all the logs at frequent intervals of time. The database can be backed up once a
week, and the logs being very small can be backed up every day or as frequently as possible.
Remote Backup
Remote backup provides a sense of security in case the primary location where the database is
located gets destroyed. Remote backup can be offline or real-time or online. In case it is offline,
it is maintained manually.
Online backup systems are more real-time and lifesavers for database administrators and
investors. An online backup system is a mechanism where every bit of the real-time data is backed
up simultaneously at two distant places. One of them is directly connected to the system and the
other one is kept at a remote place as backup.
As soon as the primary database storage fails, the backup system senses the failure and switches
the user system to the remote storage. Sometimes this is so instant that the users can’t even realize
a failure.
0300-2038128 Page | 50
Compiled by Dr. Ghulam Gilanie Janjua
Data Recovery
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.
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 terminates an active transaction because
the DBMS is not able to execute it, or it has to stop because of some system condition.
For example, in case of deadlock or resource unavailability, the system aborts an active
transaction.
System Crash
There are problems − external to the system − that may cause the system to stop abruptly and
cause the system to crash. For example, interruptions in power supply may cause the failure of
underlying hardware or software failure.
Examples may include operating system errors.
Disk Failure
In early days of technology evolution, it was a common problem where hard-disk drives or storage
drives used to fail frequently.
Disk failures include formation of bad sectors, unreachability to the disk, disk head crash or any
other failure, which destroys all or a part of disk storage.
Storage Structure
We have already described the storage system. In brief, the storage structure can be divided into
two categories −
Volatile storage − As the name suggests, a volatile storage cannot survive system crashes.
Volatile storage devices are placed very close to the CPU; normally they are embedded
onto the chipset itself. For example, main memory and cache memory are examples of
volatile storage. They are fast but can store only a small amount of information.
Non-volatile storage − These memories are made to survive system crashes. They are
huge in data storage capacity, but slower in accessibility. Examples may include hard-
disks, magnetic tapes, flash memory, and non-volatile (battery backed up) RAM.
Recovery and Atomicity
When a system crashes, it may have several transactions being executed and various files opened
for them to modify the data items. Transactions are made of various operations, which are atomic
in nature. But according to ACID properties of DBMS, atomicity of transactions as a whole must
be maintained, that is, either all the operations are executed or none.
When a DBMS recovers from a crash, it should maintain the following −
0300-2038128 Page | 51
Compiled by Dr. Ghulam Gilanie Janjua
It should check the states of all the transactions, which were being executed.
A transaction may be in the middle of some operation; the DBMS must ensure the
atomicity of the transaction in this case.
It should check whether the transaction can be completed now or it needs to be rolled back.
No transactions would be allowed to leave the DBMS in 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.
What is a data flow diagram (DFD)?
A picture is worth a thousand words. A Data Flow Diagram (DFD) is a traditional way to
visualize the information flows within a system. A neat and clear DFD can depict a good
amount of the system requirements graphically. It can be manual, automated, or a combination
of both.
It shows how information enters and leaves the system, what changes the information and
where information is stored. The purpose of a DFD is to show the scope and boundaries of a
system as a whole. It may be used as a communications tool between a systems analyst and any
person who plays a part in the system that acts as the starting point for redesigning a system.
It is usually beginning with a context diagram as level 0 of the DFD diagram, a simple
representation of the whole system. To elaborate further from that, we drill down to a level 1
diagram with lower-level functions decomposed from the major functions of the system. This
could continue to evolve to become a level 2 diagram when further analysis is required.
Progression to levels 3, 4 and so on is possible but anything beyond level 3 is not very common.
Please bear in mind that the level of detail for decomposing a particular function depending on
the complexity that function.
DFD Diagram Notations
Now we'd like to briefly introduce to you a few diagram notations which you'll see in the
tutorial below.
External Entity
An external entity can represent a human, system or subsystem. It is where certain data comes
from or goes to. It is external to the system we study, in terms of the business process. For this
reason, people used to draw external entities on the edge of a diagram.
Process
A process is a business activity or function where the manipulation and transformation of data
take place. A process can be decomposed to a finer level of details, for representing how data
is being processed within the process.
0300-2038128 Page | 52
Compiled by Dr. Ghulam Gilanie Janjua
Data Store
A data store represents the storage of persistent data required and/or produced by the process.
Here are some examples of data stores: membership forms, database tables, etc.
Data Flow
A data flow represents the flow of information, with its direction represented by an arrowhead
that shows at the end(s) of flow connector.
0300-2038128 Page | 53
View publication stats