Introduction to Database Management Systems
Introduction to Database Management Systems
com
Anna University | Polytechnic | Schools
Unit 1
Introduction
Introduction to DBMS
DBMS stands for Database Management System.
DBMS = Database + Management System.
Database is a collection of data and Management System is a set of programs
to store and retrieve those data.
DBMS is a collection of inter-related data and set of programs to store &
access those data in an easy and effective manner.
DBMS:-
DBMS is a software that is used to manage the data. Some of the popular DBMS
softwares are: MySQL, IBM Db2, Oracle,
DBMS provides an interface to the user so that the operations on database can
be performed using the interface.
DBMS secure the data, that is the main advantage of DBMS over file system.
DBMS also secures the data from unauthorised access as well as corrupt data
insertions. It allows multiple users to access data simultaneously while
maintaining the data consistency and data integrity.
DBMS allows following operations to the authorized users of the database:
[Link]
Data Definition: Creation of table, table schema creation, removal of table
definition etc. comes under data definition. It is basically a layout of the table and
their relation with the other tables in the database. This allows to properly structure
the data in such a way so that the data that is related or dependent on other data in real
world can be represented the same way in database.
Data Modification: DBMS allows users to insert, update and delete the data
from the tables. These tables contains rows and columns, where row represents a
record of data while column represents attributes of the records. You can also bulk
update the several records in DBMS with a single click.
Data Retrieval: DBMS allows users to fetch data from the database. Searching
and retrieval of data is fast in DBMS. The size of the database doesn’t impact this
operation, on the other hand in file system, the size of the data can hugely impact the
search operation efficiency.
[Link]
1
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Need of DBMS
Database systems are basically developed for large amount of data. When
dealing with huge amount of data, there are two things that require
optimization: Storage of data and retrieval of data.
Fast Retrieval of data: Along with storing the data in an optimized and
systematic manner, it is also important that we retrieve the data quickly when needed.
[Link]
Database systems ensure that the data is retrieved as quickly as possible.
DBMS applications
[Link]
2
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Banking System: For storing customer info, tracking day to day credit and debit
transactions, generating bank statements etc. All this work has been done with
the help of Database management systems. Also, banking system needs security
of data as the data is sensitive, this is efficiently taken care by the DBMS
systems.
Sales: To store customer information, production information and invoice
details. Using DBMS, you can track, manage and generate historical data to
analyse the sales data.
Airlines: To travel though airlines, we make early reservations, this reservation
information along with flight schedule is stored in database. This is where the
real-time update of data is necessary as a flight seat reserved for one
passenger should not be allocated to another passenger, this is easily handled
by the DBMS systems as the data updates are in real time and fast.
Education sector: Database systems are frequently used in schools and colleges
to store and retrieve the data regarding student details, staff details, course
details, exam details, payroll data, attendance details, fees details etc. There is a
large amount of inter-related data that needs to be stored and retrieved in an
efficient manner.
Online shopping: You must be aware of the online shopping websites such as
Amazon, Flipkart etc. These sites store the product information, your addresses
and preferences, credit details and provide you the relevant list of products based
[Link]
on your query. All this involves a Database management system. Along with
managing the vast catalogue of items, there is a need to secure the user
private information such as bank & card details. All this is taken care of by
database management systems.
[Link]
3
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
o Because data are scattered in various files, and files may be in different
formats, writing new application programs to retrieve the appropriate
data is difficult.
Dependency on application programs:
o Changing files would lead to change in application programs.
Atomicity issues:
o Atomicity of a transaction refers to “All or nothing”, which means either
all the operations in a transaction executes or none.
o It is difficult to achieve atomicity in file processing systems.
Data Security:
o Data should be secured from unauthorised access,
o for example a student in a college should not be able to see the payroll
details of the teachers, such kind of security constraints are difficult to
apply in file processing systems.
No redundant data:
o Redundancy removed by data normalization. No data duplication saves
[Link]
storage and improves access time.
Data Consistency and Integrity:
o As we discussed earlier the root cause of data inconsistency is data
redundancy, since data normalization takes care of the data redundancy,
data inconsistency also been taken care of as part of it
Data Security:
o It is easier to apply access constraints in database systems so that only
authorized user is able to access the data.
o Each user has a different set of access thus data is secured from the
issues such as identity theft, data leaks and misuse of data.
Privacy:
o Limited access means privacy of data. DBMS can grant and revoke
access to the database on user level that ensures who is accessing which
data. It also helps user to manage the constraints on database, this
ensures which type of data can be entered into the table.
Easy access to data –
o Database systems manages data in such a way so that the data is easily
accessible with fast response times. Even if the database size is huge, the
DBMS can still provide faster access and updation of data.
Easy recovery:
o Since database systems keeps the backup of data, it is easier to do a full
recovery of data in case of a failure. This is very useful especially for
almost all the organizations, as the data maintained over time should not
be lost during a system crash or failure.
Flexible:
[Link]
4
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
o Database systems are more flexible than file processing systems. DBMS
systems are scalable,
o The database size can be increased and decreased based on the amount
of storage required.
o It also allows addition of additional tables as well as removal of existing
tables without disturbing the consistency of data.
Disadvantages of DBMS
View of Data
View of data in DBMS
Abstraction is one of the main features of database systems.
Hiding irrelevant details from user and providing abstract view of data to users,
helps in easy and efficient user-database interaction.
[Link]
The top level of that architecture is “view level”.
The view level provides the “view of data” to the users and hides the irrelevant
details such as data relationship, database schema, constraints, security etc
from the user.
To fully understand the view of data, you must have a basic knowledge of data
abstraction and instance & schema.
[Link]
5
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Physical level: This is the lowest level of data abstraction. It describes how data is
actually stored in database. You can get the complex data structure details at this level.
[Link]
Logical level: This is the middle level of 3-level data abstraction architecture. It
describes what data is stored in database.
View level: Highest level of data abstraction. This level describes the user
interaction with database system.
Schema represents the logical view of the database. It helps you understand
what data needs to go where.
Schema can be represented by a diagram as shown below.
Schema helps the database users to understand the relationship between
data. This helps in efficiently performing operations on database such as insert,
update, delete, search etc.
In the following diagram, we have a schema that shows the relationship between three
tables: Course, Student and Section. The diagram only shows the design of the
database, it doesn’t show the data present in those tables. Schema is only a structural
view(design) of a database as shown in the diagram below.
[Link]
The design of a database at physical level is called physical schema, how the data
stored in blocks of storage is described at this level.
Design of database at logical level is called logical schema, programmers and
database administrators work at this level, at this level data can be described as certain
types of data records gets stored in data structures, however the internal details such as
implementation of data structure is hidden at this level (available at physical level).
Design of database at view level is called view schema. This generally
describes end user interaction with database systems.
DBMS Instance
Definition of instance: The data stored in database at a particular moment of
time is called instance of database. Database schema defines the attributes in tables
that belong to a particular database. The value of these attributes at a moment of time
is called the instance of that database.
For example, we have seen the schema of table “employee” above. Let’s see
the table with the data now. At this moment the table contains two rows (records).
[Link]
7
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
This is the the current instance of the table “employee” because this is the data that is
stored in this table at this particular moment of time.
EMP_NAME EMP_ID EMP_ADDRESS EMP_CONTACT
-----------------------------------
Chaitanya101Noida95********
Ajeet102Delhi99********
Let’s take another example: Let’s say we have a single table student in the database,
today the table has 100 records, so today the instance of the database has 100 records.
We are going to add another 100 records in this table by tomorrow so the instance of
database tomorrow will have 200 records in table. In short, at a particular moment the
data stored in database is called the instance, this changes over time as and when we
add, delete or update data in the database.
DBMS languages
Database languages are used to read, update and store data in a database. There
are several such languages that can be used for this purpose; one of them is SQL
(Structured Query Language).
[Link]
DCL Data Control Language
DML Data Manipulation Language
TCL Transaction Control Language
All of these commands either defines or update the database schema that’s why they
come under Data Definition language.
[Link]
8
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
The architecture of DBMS depends on the computer system on which it runs.
For example, in a client-server DBMS architecture, the database systems at server
machine can run several requests made by client machine. We will understand this
communication with the help of diagrams.
[Link]
9
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
In two-tier architecture, the Database system is present at the server machine and the
DBMS application is present at the client machine, these two machines are connected
with each other through a reliable network as shown in the above diagram.
Whenever client machine makes a request to access the database present at server
using a query language like sql, the server perform the request on the database and
returns the result back to the client. The application connection interface such as
JDBC, ODBC are used for the interaction between server and client.
[Link]
10
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
In three-tier architecture, another layer is present between the client machine and
server machine. In this architecture, the client application doesn’t communicate
directly with the database systems present at the server machine, rather the client
application communicates with server application and the server application internally
communicates with the database system present at the server.
1. E-R Model
2. Object oriented Model
[Link]
11
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Record based logical Models – Like Object based model, they also describe data at
the conceptual and view levels. These models specify logical structure of database
with records, fields and attributes.
1. Relational Model
2. Hierarchical Model
3. Network Model – Network Model is same as hierarchical model except that it
has graph-like structure rather than a tree-based structure. Unlike hierarchical
model, this model allows each record to have more than one parent record.
Physical Data Models – These models describe data at the lowest level of abstraction.
[Link]
An ER diagram shows the relationship among entity sets. An entity set is a
group of similar entities and these entities can have attributes. In terms of DBMS, an
entity is a table or attribute of a table in database, so by showing relationship among
tables and their attributes, ER diagram shows the complete logical structure of a
database. Lets have a look at a simple ER diagram to understand this concept.
A simple ER Diagram:
In the following diagram we have two entities Student and College and their
relationship. The relationship between Student and College is many to one as a college
[Link]
12
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
can have many students however a student cannot study in multiple colleges at the
same time. Student entity has attributes such as Stu_Id, Stu_Name&Stu_Addr and
College entity has attributes such as Col_ID&Col_Name.
Here are the geometric shapes and their meaning in an E-R Diagram. We will
discuss these terms in detail in the next section(Components of a ER Diagram) of this
guide so don’t worry too much about these terms now, just go through them once.
Components of a ER Diagram
[Link]
1. Entity
An entity is an object or component of data. An entity is represented as rectangle in an
ER diagram.
[Link]
13
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
For example: In the following ER diagram we have two entities Student and College
and these two entities have many to one relationship as many students study in a
single college. We will read more about relationships later, for now focus on entities.
Weak Entity:
An entity that cannot be uniquely identified by its own attributes and relies on the
[Link]
relationship with other entity is called weak entity. The weak entity is represented by a
double rectangle. For example – a bank account cannot be uniquely identified without
knowing the bank to which the account belongs, so bank account is a weak entity.
2. Attribute
An attribute describes the property of an entity. An attribute is represented as Oval in
an ER diagram. There are four types of attributes:
1. Key attribute
2. Composite attribute
3. Multivalued attribute
4. Derived attribute
[Link]
14
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
1. Key attribute:
A key attribute can uniquely identify an entity from an entity set. For example, student
roll number can uniquely identify a student from a set of students. Key attribute is
represented by oval same as other attributes however the text of key attribute is
underlined.
[Link]
2. Composite attribute:
3. Multivalued attribute:
An attribute that can hold multiple values is known as multivalued attribute. It is
represented with double ovals in an ER Diagram. For example – A person can have
more than one phone numbers so the phone number attribute is multivalued.
[Link]
15
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
4. Derived attribute:
A derived attribute is one whose value is dynamic and derived from another attribute.
It is represented by dashed oval in an ER Diagram. For example – Person age is a
derived attribute as it changes over time and can be derived from another attribute
(Date of birth).
3. Relationship
A relationship is represented by diamond shape in ER diagram, it shows the
relationship among entities. There are four types of relationships:
[Link]
1. One to One
2. One to Many
3. Many to One
4. Many to Many
[Link]
16
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
When a single instance of an entity is associated with more than one instances of
another entity then it is called one to many relationship. For example – a customer can
place many orders but a order cannot be placed by many customers.
[Link]
When more than one instances of an entity is associated with a single instance of
another entity then it is called many to one relationship. For example – many students
can study in a single college but a student cannot study in many colleges at the same
time.
When more than one instances of an entity is associated with more than one instances
of another entity then it is called many to many relationship. For example, a can be
assigned to many projects and a project can be assigned to many students.
[Link]
17
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
one associated Student. Total participation is represented using a double line between
the entity set and relationship set.
[Link]
Partial participation is represented using a single line between the entity set and
relationship set.
Example: Consider an example of an IT company. There are many employees
working for the company. Let’s take the example of relationship
between employee and role software engineer. Every software engineer is an employee
but not every employee is software engineer as there are employees for other roles as
well, such as housekeeping, managers, CEO etc. so we can say that participation of
employee entity set to the software engineer relationship is partial.
new relationship if a requirement arise in future, if this is the case then it is better to
choose entity set rather than relationship set.
Let’s take an example to understand it better: A person takes a loan from a bank, here
we have two entities person and bank and their relationship is loan. This is fine until
there is a need to disburse a joint loan, in such case a new relationship needs to be
created to define the relationship between the two individuals who have taken joint
loan. In this scenario, it is better to choose loan as an entity set rather than a
relationship set.
[Link]
and father and mother relationship with their child as “child”.
[Link]
19
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
[Link]
20
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
[Link]
21
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Unit II
RELATIONAL MODEL
Relational Model
Relational Model (RM) represents the database as a collection of relations. A
relation is nothing but a table of values. Every row in the table represents a collection of
related data values. These rows in the table denote a real-world entity or relationship.
The table name and column names are helpful to interpret the meaning of values in
each row. The data are represented as a set of relations. In the relational model, data are
stored as tables. However, the physical storage of the data is independent of the way the data
are logically organized.
[Link]
1. Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of the
relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Relation key – Every row has one, two or multiple attributes, which is called relation
key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is
known as attribute domain
[Link]
22
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
a valid relation. These Relational constraints in DBMS are derived from the rules in the mini-
world that the database represents.
There are many types of Integrity Constraints in DBMS. Constraints on the Relational
database management system is mostly divided into three main categories are:
1. Domain Constraints
2. Key Constraints
3. Referential Integrity Constraints
Domain Constraints
Domain constraints can be violated if an attribute value is not appearing in the corresponding
domain or it is not of the appropriate data type.
Domain constraints specify that within each tuple, and the value of each attribute must be
unique. This is specified as data types which include standard data types integers, real
numbers, characters, Booleans, variable length strings, etc.
Example:
The example shown demonstrates creating a domain constraint such that CustomerName is
not NULL
[Link]
23
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Key Constraints
An attribute that can uniquely identify a tuple in a relation is called the key of the table. The
value of the attribute for different tuples in the relation has to be unique.
Example:
In the given table, CustomerID is a key attribute of Customer Table. It is most likely to have
a single key for one customer, CustomerID =1 is only for the CustomerName =” Google”.
1 Google Active
2 Amazon Active
3 Apple Inactive
Example:
[Link]
[Link]
24
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Simplicity: A Relational data model in DBMS is simpler than the hierarchical and
network model.
Structural Independence: The relational database is only concerned with data and
not with a structure. This can improve the performance of the model.
Easy to use: The Relational model in DBMS is easy as tables consisting of rows and
columns are quite natural and simple to understand
Query capability: It makes possible for a high-level query language like SQL to
avoid complex database navigation.
Data independence: The Structure of Relational database can be changed without
having to change any application.
Scalable: Regarding a number of records, or rows, and the number of fields, a
database should be enlarged to enhance its usability.
[Link]
Disadvantages of Relational Model
Few relational databases have limits on field lengths which can’t be exceeded.
Relational databases can sometimes become complex as the amount of data grows,
and the relations between pieces of data become more complicated.
Complex relational database systems may lead to isolated databases where the
information cannot be shared from one system to another.
Relational database systems are expected to be equipped with a query language that can assist
its users to query the database instances. There are two kinds of query languages − 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
[Link]
25
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Set different
Cartesian product
Rename
We will discuss all these operations in the following sections.
[Link]
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
[Link]
26
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
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.
[Link]
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with small
Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
Set intersection
Assignment
Natural join
Relational Calculus
[Link]
27
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is
database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra.
SQL NULL Values
In SQL there may be some records in a table that do not have values or data for
every field. This could be possible because at a time of data entry information is not
available. So SQL supports a special value known as NULL which is used to represent the
values of attributes that may be unknown or not apply to a tuple. SQL places a NULL value
in the field in the absence of a user-defined value. For example, the Apartment_number
attribute of an address applies only to address that are in apartment buildings and not to
other types of residences.
Importance of NULL value:
It is important to understand that a NULL value is different from a zero value.
A NULL value is used to represent a missing value, but that it usually has one of three
different interpretations:
[Link]
28
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Setting a NULL value is appropriate when the actual value is unknown, or when a value
would not be meaningful.
A NULL value is not equivalent to a value of ZERO if the data type is a number and is
not equivalent to spaces if the data type is character.
A NULL value can be inserted into columns of any data type.
A NULL value will evaluate NULL in any expression.
Suppose if any column has a NULL value, then UNIQUE, FOREIGN key, CHECK
constraints will ignore by SQL.
In general, each NULL value is considered to be different from every other NULL in the
database. When a NULL is involved in a comparison operation, the result is considered to
be UNKNOWN. Hence, SQL uses a three-valued logic with values True, False,
and Unknown. It is, therefore, necessary to define the results of three-valued logical
expressions when the logical connectives AND, OR, and NOT are used.
[Link]
How to test for NULL Values?
SQL allows queries that check whether an attribute value is NULL. Rather than using = or
to compare an attribute value to NULL, SQL uses IS and IS NOT. This is because SQL
considers each NULL value as being distinct from every other NULL value, so equality
comparison is not appropriate.
Now, consider the following Employee Table,
[Link]
29
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Suppose if we find the Fname, Lname of the Employee having no Super_ssn then the query
will be:
Query
SELECT Fname, Lname FROM Employee WHERE Super_ssn IS NULL;
Output:
[Link]
Now if we find the Count of the number of Employees having Super_ssn.
Query:
Whenever one of these operations are applied, integrity constraints specified on the relational
database schema must never be violated.
[Link]
30
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Insert Operation
The insert operation gives values of the attribute for a new tuple which should be inserted
into a relation.
Update Operation
You can see that in the below-given relation table CustomerName= ‘Apple’ is updated from
Inactive to Active.
[Link]
Delete Operation
To specify deletion, a condition on the attributes of the relation selects the tuple to be deleted.
The Delete operation could violate referential integrity if the tuple which is deleted is
referenced by foreign keys from other tuples in the same database.
Select Operation
[Link]
31
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
ROLL_NO NAME ADDRESS PHONE AGE
TABLE 1
These are some important terminologies that are used in terms of relation.
Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME etc.
Tuple: Each row in the relation is known as tuple. The above relation contains 4 tuples, one
of which is shown as:
Degree: The number of attributes in the relation is known as degree of the relation.
The STUDENT relation defined above has degree 5.
Cardinality: The number of tuples in a relation is known as cardinality.
The STUDENT relation defined above has cardinality 4.
[Link]
32
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Column: Column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from relation STUDENT.
ROLL_NO
[Link]
So first we will consider the Data Query Language. A generic query to retrieve from a
relational database is:
1. SELECT [DISTINCT] Attribute_List FROM R1,R2….RM
2. [WHERE condition]
3. [GROUP BY (Attributes)[HAVING condition]]
4. [ORDER BY(Attributes)[DESC]];
Part of the query represented by statement 1 is compulsory if you want to retrieve from a
relational database. The statements written inside [] are optional. We will look at the
possible query combination on relation shown in Table 1.
Case 1: If we want to retrieve attributes ROLL_NO and NAME of all students, the query
will be:
ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
[Link]
33
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
4 SURESH
ROLL_NO NAME
3 SUJIT
4 SURESH
CASE 3: If we want to retrieve all attributes of students, we can write * in place of writing
all attributes as:
SELECT * FROM STUDENT WHERE ROLL_NO>2;
[Link]
3 SUJIT ROHTAK 9156253131 20
CASE 4: If we want to represent the relation in ascending order by AGE, we can use
ORDER BY clause as:
SELECT * FROM STUDENT ORDER BY AGE;
Note: ORDER BY AGE is equivalent to ORDER BY AGE ASC. If we want to retrieve the
results in descending order of AGE, we can use ORDER BY AGE DESC.
[Link]
34
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
ADDRESS
DELHI
GURGAON
ROHTAK
If DISTINCT is not used, DELHI will be repeated twice in result set. Before understanding
GROUP BY and HAVING, we need to understand aggregations functions in SQL.
AGGRATION FUNCTIONS: Aggregation functions are used to perform mathematical
operations on data values of a relation. Some of the common aggregation functions used in
SQL are:
COUNT: Count function is used to count the number of rows in a relation. e.g;
SELECT COUNT (PHONE) FROM STUDENT;
[Link]
COUNT(PHONE)
SUM: SUM function is used to add the values of an attribute in a relation. e.g;
SELECT SUM (AGE) FROM STUDENT;
SUM(AGE)
74
In the same way, MIN, MAX and AVG can be used. As we have seen above, all
aggregation functions return only 1 row.
AVERAGE: It gives the average values of the tupples. It is also defined as sum divided by
count values.
Syntax:AVG(attributename)
OR
Syntax:SUM(attributename)/COUNT(attributename)
The above mentioned syntax also retrieves the average value of tupples.
MAXIMUM:It extracts the maximum value among the set of tupples.
Syntax:MAX(attributename)
MINIMUM:It extracts the minimum value amongst the set of all the tupples.
Syntax:MIN(attributename)
[Link]
35
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
GROUP BY: Group by is used to group the tuples of a relation based on an attribute or
group of attribute. It is always combined with aggregation function which is computed on
group. e.g.;
In this query, SUM(AGE) will be computed but not for entire table but for each address.
i.e.; sum of AGE for address DELHI(18+18=36) and similarly for other address as well.
The output is:
ADDRESS SUM(AGE)
DELHI 36
GURGAON 18
ROHTAK 20
If we try to execute the query given below, it will result in error because although we have
[Link]
computed SUM(AGE) for each address, there are more than 1 ROLL_NO for each address
we have grouped. So it can’t be displayed in result set. We need to use aggregate functions
on columns after SELECT statement to make sense of the resulting set whenever we are
using GROUP BY.
SELECT ROLL_NO, ADDRESS, SUM(AGE) FROM STUDENT
GROUP BY (ADDRESS);
Advanced SQL
Accessing SQL From a Programming Language
■ API (application-program interface) for a program to interact with a database server
■ Application makes calls to
● Connect with the database server
● Send SQL commands to the database server
● Fetch tuples of result one-by-one into program variables
■ Various tools:
● ODBC (Open Database Connectivity) works with C, C++, C#, and Visual
Basic. Other APIs such as [Link] sit on top of ODBC
● JDBC (Java Database Connectivity) works with Java
● Embedded SQL
[Link]
36
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Integrity Constraints
The Set of rules which is used to maintain the quality of information are known
as integrity constraints.
Integrity constraints make sure about data intersection, update and so on.
Integrity constraints can be understood as a guard against unintentional damage to
the database.
For any stored data if we want to preserve the consistency and correctness, a relational
DBMS typically imposes one or more data integrity constraints. These constraints restrict the
data values which can be inserted into the database or created by a database update.
There are different types of data integrity constraints that are commonly found in relational
databases, including the following −
Required data − Some columns in a database contain a valid data value in each row;
they are not allowed to contain NULL values. In the sample database, every order has
an associated customer who placed the order. The DBMS can be asked to prevent
NULL values in this column.
Validity checking − Every column in a database has a domain, a set of data values
which are legal for that column. The DBMS allowed preventing other data values in
these columns.
Entity integrity − The primary key of a table contains a unique value in each row that
[Link]
is different from the values in all other rows. Duplicate values are illegal because they
are not allowing the database to differentiate one entity from another. The DBMS can
be asked to enforce this unique values constraint.
Referential integrity − A foreign key in a relational database links each row in the
child table containing the foreign key to the row of the parent table containing the
matching primary key value. The DBMS can be asked to enforce this foreign
key/primary key constraint.
Other data relationships − The real-world situation which is modeled by a database
often has additional constraints which govern the legal data values that may appear in
the database. The DBMS is allowed to check modifications to the tables to make sure
that their values are constrained in this way.
Business rules − Updates to a database that are constrained by business rules
governing the real-world transactions which are represented by the updates.
Consistency − Many real-world transactions that cause multiple updates to a database.
The DBMS is allowed to enforce this type of consistency rule or to support
applications that implement such rules.
[Link]
37
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Domain Constraint
The Definition of an applicable set of values is known as domain constraint.
Strings, character, time, integer, currency, date etc. Are examples of the data type of domain
constraints.
Example
100
101
102
[Link] Jai
BKadam
Rajeev
1st
4th
3rd
27
34
31
11 Manish 30000
[Link]
38
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
12 Vikram 20000
13 Sudhir 10000
Rajeev 40000
Null is not allowed in Emp_ID as it is a Primary key and cannot have a NULL value.
Key Constraints
The Entity within its entity set is identified uniquely by the key which is the entity set.
There can be a number of keys in an entity set but only one will be the primary key out of all
keys. In a relational table a primary key can have a unique as well as a null value.
Example
[Link]
100
101
102
Naren
Lalit
Shivanshu
4
3
27
28
22
103 Navdeep 5 29
102 Karthik 7 25
All row ID must be unique hence 102 is not allowed.
Database authorization
Authorization is the process where the database manager gets information about the
authenticated user. Part of that information is determining which database operations the user
can perform and which data objects a user can access.
[Link]
39
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
A role is a database object that groups one or more privileges. Roles can be assigned to users
or groups or other roles by using the GRANT statement. Users that are members of roles
have the privileges that are defined for the role with which to access data.
The forms of authorization, such as administrative authority, privileges, and Row and
column access (RCAC) access, are discussed in Authorization of Big SQL objects. In
addition, ownership of objects brings with it a degree of authorization on the objects
created.
System-level authorization
SYSADM (system administrator) authority
The SYSADM (system administrator) authority provides control over all the resources
created and maintained by the database manager. The system administrator possesses all the
authorities of SYSCTRL, SYSMAINT, and SYSMON authority. The user who has
SYSADM authority is responsible both for controlling the database manager, and for
ensuring the safety and integrity of the data.
SYSCTRL authority
[Link]
The SYSCTRL authority provides control over operations that affect system resources. For
example, a user with SYSCTRL authority can create, update, start, stop, or drop a database.
This user can also start or stop an instance, but cannot access table data. Users with
SYSCTRL authority also have SYSMON authority.
SYSMAINT authority
The SYSMAINT authority provides the authority required to perform maintenance
operations on all databases that are associated with an instance. A user with SYSMAINT
authority can update the database configuration, backup a database or table space, restore
an existing database, and monitor a database. Like SYSCTRL, SYSMAINT does not
provide access to table data. Users with SYSMAINT authority also have SYSMON
authority.
SYSMON (system monitor) authority
The SYSMON (system monitor) authority provides the authority required to use the
database system monitor.
Database-level authorization
DBADM (database administrator)
The DBADM authority level provides administrative authority over a single database. This
database administrator possesses the privileges required to create objects and issue database
commands. The DBADM authority can be granted only by a user with SECADM authority.
The DBADM authority cannot be granted to PUBLIC.
SECADM (security administrator)
[Link]
40
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
The SECADM authority level provides administrative authority for security over a single
database. The security administrator authority possesses the ability to manage database
security objects (database roles, audit policies, trusted contexts, security label components,
and security labels) and grant and revoke all database privileges and authorities. A user
with SECADM authority can transfer the ownership of objects that they do not own. They
can also use the AUDIT statement to associate an audit policy with a particular database or
database object at the [Link] SECADM authority has no inherent privilege to access
data stored in tables. It can only be granted by a user with SECADM authority. The
SECADM authority cannot be granted to PUBLIC.
SQLADM (SQL administrator)
The SQLADM authority level provides administrative authority to monitor and tune SQL
statements within a single database. It can be granted by a user with ACCESSCTRL or
SECADM authority.
WLMADM (workload management administrator)
The WLMADM authority provides administrative authority to manage workload
management objects, such as service classes, work action sets, work class sets, and
workloads. It can be granted by a user with ACCESSCTRL or SECADM
[Link] (explain authority)The EXPLAIN authority level provides
administrative authority to explain query plans without gaining access to data. It can only
[Link]
be granted by a user with ACCESSCTRL or SECADM authority.
EXPLAIN (explain authority)
The EXPLAIN authority level provides administrative authority to explain query plans
without gaining access to data. It can only be granted by a user with ACCESSCTRL or
SECADM authority.
ACCESSCTRL (access control authority)
ACCESSCTRL authority can only be granted by a user with SECADM authority. The
ACCESSCTRL authority cannot be granted to PUBLIC. The ACCESSCTRL authority
level provides administrative authority to issue the following GRANT (and REVOKE)
statements:
[Link]
41
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
For more information about granting and revoking privileges, see Granting and revoking
access.
LOAD authority
SELECT, INSERT, UPDATE, DELETE privilege on tables, views, nicknames, and
materialized query tables
EXECUTE privilege on packages
EXECUTE privilege on modules
EXECUTE privilege on routines, except on the audit routines.
USAGE privilege on all sequences
[Link]
required for use of the load utility to load data into tables (a user must also have INSERT
privilege on the table).
Privileges
CONTROL privilege
If you possess the CONTROL privilege on an object, you can access that database object,
and grant and revoke privileges to or from other users on that object. The CONTROL
privilege only applies to tables, views, nicknames, indexes, and packages..
If a different user requires the CONTROL privilege to that object, a user with SECADM or
ACCESSCTRL authority can grant the CONTROL privilege to that object. The CONTROL
privilege cannot be revoked from the object owner, however, the object owner can be
changed by using the TRANSFER OWNERSHIP statement.
Individual privileges
Individual privileges can be granted to allow a user to carry out specific tasks on specific
objects. Users with the administrative authorities ACCESSCTRL or SECADM, or with the
CONTROL privilege, can grant and revoke privileges to and from users.
Revoking privileges
The REVOKE statement is used to revoke previously granted privileges. The revoking of a
privilege from an authorization name revokes the privilege granted by all authorization
names.
[Link]
42
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
Execute privileges involve actions on all types of routines such as functions, procedures,
and methods within a database. Once having EXECUTE privilege, a user can then invoke
that routine, create a function that is sourced from that routine (applies to functions only),
and reference the routine in any DDL statement such as CREATE VIEW or CREATE
TRIGGER.
Usage privilege on workloads
To enable use of a workload, a user who holds ACCESSCTRL, SECADM, or WLMADM
authority can grant USAGE privilege on that workload to a user, a group, or a role using
the GRANT USAGE ON WORKLOAD statement.
Embedded SQL applications connect to databases and execute embedded SQL statements.
The embedded SQL statements are contained in a package that must be bound to the target
database server.
You can develop embedded SQL applications for the Db2® database in the following host
programming languages: C, C++, and COBOL.
Building embedded SQL applications involves two prerequisite steps before application
compilation and linking.
[Link]
43
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
The PREP (PRECOMPILE) command is used to invoke the Db2 precompiler, which
reads your source code, parses and converts the embedded SQL statements
to Db2 run-time services API calls, and finally writes the output to a new modified
source file. The precompiler produces access plans for the SQL statements, which are
stored together as a package within the database.
Once you have precompiled and bound your embedded SQL application, it is ready to be
compiled and linked using the host language-specific development tools.
To aid in the development of embedded SQL applications, you can refer to the embedded
SQL template in Cembedded SQL template in C. Examples of working embedded SQL
sample applications can also be found in the %DB2PATH%\SQLLIB\samples directory.
Note: %DB2PATH% refers to the Db2 installation directory
Static and dynamic SQL
[Link]
SQL statements can be executed in one of two ways: statically or dynamically.
[Link]
44
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
Compatibility features for migration
The Db2 database manager provides features that facilitate the migration of embedded
SQL C applications from other database systems.
Dynamic SQL
Dynamic SQL enables you to write programs that reference SQL statements whose full text is
not known until runtime. Before discussing dynamic SQL in detail, a clear definition of static
SQL may provide a good starting point for understanding dynamic SQL. Static SQL
statements do not change from execution to execution. The full text of static SQL statements
are known at compilation, which provides the following benefits:
Successful compilation verifies that the SQL statements reference valid database
objects.
Successful compilation verifies that the necessary privileges are in place to access the
database objects.
Performance of static SQL is generally better than dynamic SQL.
Because of these advantages, you should use dynamic SQL only if you cannot use static SQL
to accomplish your goals, or if using static SQL is cumbersome compared to dynamic SQL.
However, static SQL has limitations that can be overcome with dynamic SQL. You may not
always know the full text of the SQL statements that must be executed in a PL/SQL
procedure. Your program may accept user input that defines the SQL statements to execute,
or your program may need to complete some processing work to determine the correct course
of action. In such cases, you should use dynamic SQL.
[Link]
45
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
For example, consider a reporting application that performs standard queries on tables in a
data warehouse environment where the exact table name is unknown until runtime. To
accommodate the large amount of data in the data warehouse efficiently, you create a new
table every quarter to store the invoice information for the quarter. These tables all have
exactly the same definition and are named according to the starting month and year of the
quarter, for
example INV_01_1997, INV_04_1997, INV_07_1997, INV_10_1997, INV_01_1998, etc. In
such a case, you can use dynamic SQL in your reporting application to specify the table name
at runtime.
With static SQL, all of the data definition information, such as table definitions, referenced
by the SQL statements in your program must be known at compilation. If the data definition
changes, you must change and recompile the program. Dynamic SQL programs can handle
changes in data definition information, because the SQL statements can change "on the fly"
at runtime. Therefore, dynamic SQL is much more flexible than static SQL. Dynamic SQL
enables you to write application code that is reusable because the code defines a process that
is independent of the specific SQL statements used.
In addition, dynamic SQL lets you execute SQL statements that are not supported in static
SQL programs, such as data definition language (DDL) statements. Support for these
statements allows you to accomplish more with your PL/SQL programs.
[Link]
Tuple Relational Calculus is a non-procedural query language unlike relational algebra.
Tuple Calculus provides only the description of the query but it does not provide the
methods to solve it. Thus, it explains what to do but not how to do.
In Tuple Calculus, a query is expressed as
{t| P(t)}
where t = resulting tuples,
P(t) = known as Predicate and these are the conditions that are used to fetch t
Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
It also uses quantifiers:
∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.
Example:
Table-1: Customer
Customer name Street City
Saurabh A7 Patiala
Mehak B6 Jalandhar
Sumiti D9 Ludhiana
[Link]
46
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Ria A5 Patiala
Table-2: Branch
ABC Patiala
DEF Ludhiana
GHI Jalandhar
Table-3: Account
[Link]
1112 DEF 10000
Table-4: Loan
Table-5: Borrower
[Link]
47
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Saurabh L33
Mehak L49
Ria L98
Table-6: Depositor
Saurabh 1111
Mehak 1113
Sumiti 1114
Queries-1: Find the loan number, branch, amount of loans of greater than or equal to 10000
amount.
[Link]
{t| t ∈ loan ∧ t[amount]>=10000}
Resulting relation:
Loan number
L33
L35
[Link]
48
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Loan number
L98
Queries-3: Find the names of all customers who have a loan and an account at the bank.
{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name])
∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])}
Resulting relation:
Customer name
Saurabh
Mehak
Queries-4: Find the names of all customers having a loan at the “ABC” branch.
{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-number]))}
Resulting relation:
[Link]
Customer name
Saurabh
[Link]
49
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Table-2: Loan
Loan number Branch name Amount
L10 Sub 90
L08 Main 60
Customer name
[Link]
Table-3: Borrower
Loan number
Ritu L01
Debomit L08
Soumya L03
Query-1: Find the loan number, branch, amount of loans of greater than or equal to 100
amount.
{≺l, b, a≻ | ≺l, b, a≻ ∈ loan ∧ (a ≥ 100)}
Resulting relation:
Query-2: Find the loan number for each loan of an amount greater or equal to 150.
{≺l≻ | ∃ b, a (≺l, b, a≻ ∈ loan ∧ (a ≥ 150)}
[Link]
50
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Resulting relation:
Loan number
L01
L03
Query-3: Find the names of all customers having a loan at the “Main” branch and find the
loan amount .
{≺c, a≻ | ∃ l (≺c, l≻ ∈ borrower ∧ ∃ b (≺l, b, a≻ ∈ loan ∧ (b = “Main”)))}
Resulting relation:
Ritu 200
Debomit 60
Soumya 150
[Link]
Query By Example (QBE)
Normal queries we fire on the database they should be correct and in a well-defined
structure which means they should follow a proper syntax if the syntax or query is wrong
definitely we will get an error and due to that our application or calculation definitely going
to stop. So to overcome this problem QBE was introduced. QBE stands for Query By
Example and it was developed in 1970 by Moshe Zloof at IBM.
It is a graphical query language where we get a user interface and then we fill some
required fields to get our proper result.
In SQL we will get an error if the query is not correct but in the case of QBE if the query is
wrong either we get a wrong answer or the query will not be going to execute but we will
never get any error.
Note-:
In QBE we don’t write complete queries like SQL or other database languages it comes
with some blank so we need to just fill that blanks and we will get our required result.
Example
Consider the example where a table ‘SAC’ is present in the database with Name,
Phone_Number, and Branch fields. And we want to get the name of the SAC-
Representative name who belongs to the MCA Branch. If we write this query in SQL we
have to write it like
SELECT NAME
FROM SAC
[Link]
51
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
SQL Trigger
Trigger: A trigger is a stored procedure in database which automatically invokes whenever
a special event in the database occurs. For example, a trigger can be invoked when a row is
inserted into a specified table or when certain table columns are being updated.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
[Link]
on [table_name]
[for each row]
[trigger_body]
Explanation of syntax:
1. create trigger [trigger_name]: Creates or replaces an existing trigger with the
trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. on [table_name]: This specifies the name of the table associated with the trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be executed for
each row being affected.
6. [trigger_body]: This provides the operation to be performed as trigger is fired
BEFORE triggers run the trigger action before the triggering statement is run.
AFTER triggers run the trigger action after the triggering statement is run.
Example:
Given Student Report Database, in which student marks assessment is recorded. In such
schema, create a trigger so that the total and average of specified marks is automatically
inserted whenever a record is insert.
Here, as trigger will invoke before record is inserted so, BEFORE Tag can be used.
[Link]
52
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
create trigger stud_marks
before INSERT
on
Student
for each row
set [Link] = Student.subj1 + Student.subj2 + Student.subj3, [Link] =
[Link] * 60 / 100;
Above SQL statement will create a trigger in the student database in which whenever
subjects marks are entered, before inserting this data into the database, trigger will compute
those two values and insert with the entered values. i.e.,
mysql> insert into Student values(0, "ABCDE", 20, 20, 20, 0, 0);
Query OK, 1 row affected (0.09 sec)
mysql> select * from Student;
+-----+-------+-------+-------+-------+-------+------+
| tid | name | subj1 | subj2 | subj3 | total | per |
+-----+-------+-------+-------+-------+-------+------+
| 100 | ABCDE | 20 | 20 | 20 | 60 | 36 |
+-----+-------+-------+-------+-------+-------+------+
1 row in set (0.00 sec)
[Link]
53
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Unit 3
Database Design
42 abc CO A4
[Link]
43
44
pqr
xyz
IT
CO
A3
A4
45 xyz IT A3
46 mno EC B2
47 jkl ME B2
From the above table we can conclude some valid functional dependencies:
roll_no → { name, dept_name, dept_building },→ Here, roll_no can determine
values of fields name, dept_name and dept_building, hence a valid Functional
dependency
roll_no → dept_name , Since, roll_no can determine whole set of {name,
dept_name, dept_building}, it can determine its subset dept_name also.
dept_name → dept_building , Dept_name can identify the dept_building
accurately, since departments with different dept_name will also have a different
dept_building
More valid functional dependencies: roll_no → name, {roll_no, name} ⇢
{dept_name, dept_building}, etc.
Here are some invalid functional dependencies:
[Link]
54
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
name → dept_name Students with the same name can have different dept_name,
hence this is not a valid functional dependency.
dept_building → dept_name There can be multiple departments in the same
building, For example, in the above table departments ME and EC are in the same
building B2, hence dept_building → dept_name is an invalid functional
dependency.
More invalid functional dependencies: name → roll_no, {name, dept_name} →
roll_no, dept_building → roll_no, etc.
[Link]
2. Non-Trivial functional dependency
3. Multivalued functional dependency
4. Transitive functional dependency
For example,
roll_no name age
42 abc 17
43 pqr 18
44 xyz 18
[Link]
55
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
42 abc 17
43 pqr 18
44 xyz 18
[Link]
Here, roll_no → name is a non-trivial functional dependency, since the
dependent name is not a subset of determinant roll_no
Similarly, {roll_no, name} → age is also a non-trivial functional dependency,
since age is not a subset of {roll_no, name}
42 abc 17
43 pqr 18
44 xyz 18
[Link]
56
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
45 abc 19
42 abc CO 4
43 pqr EC 2
[Link] 44
45
xyz
abc
IT
EC
1
[Link]
57
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
Example 1 – Relation STUDENT in table 1 is not in 1NF because of multi-valued
attribute STUD_PHONE. Its decomposition into 1NF has been shown in table 2.
Example 2 –
ID Name Courses
------------------
1 A c1, c2
2 E c3
3 M C2, c3
[Link]
58
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
To be in second normal form, a relation must be in first normal form and relation
must not contain any partial dependency. A relation is in 2NF if it has No Partial
Dependency, i.e., no non-prime attribute (attributes which are not part of any candidate
key) is dependent on any proper subset of any candidate key of the table.
[Link]
Partial Dependency – If the proper subset of candidate key determines non-prime
attribute, it is called partial dependency.
Example 1 – Consider table-3 as following below.
STUD_NO COURSE_NO COURSE_FEE
1 C1 1000
2 C2 1500
1 C4 2000
4 C3 1000
4 C1 1000
2 C5 2000
{Note that, there are many courses having the same course fee. }
Here,
COURSE_FEE cannot alone decide the value of COURSE_NO or STUD_NO;
COURSE_FEE together with STUD_NO cannot decide the value of COURSE_NO;
COURSE_FEE together with COURSE_NO cannot decide the value of STUD_NO;
Hence,
COURSE_FEE would be a non-prime attribute, as it does not belong to the one only
candidate key {STUD_NO, COURSE_NO} ;
But, COURSE_NO -> COURSE_FEE, i.e., COURSE_FEE is dependent on
COURSE_NO, which is a proper subset of the candidate key. Non-prime attribute
COURSE_FEE is dependent on a proper subset of the candidate key, which is a partial
dependency and so this relation is not in 2NF.
[Link]
59
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
3. Third Normal Form –
Transitive dependency – If A->B and B->C are two FDs then A->C is called transitive
dependency.
Example 1 – In relation STUDENT given in Table 4,
FD set: {STUD_NO -> STUD_NAME, STUD_NO -> STUD_STATE,
STUD_STATE -> STUD_COUNTRY, STUD_NO -> STUD_AGE}
Candidate Key: {STUD_NO}
[Link]
60
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
For this relation in table 4, STUD_NO -> STUD_STATE and STUD_STATE ->
STUD_COUNTRY are true. So STUD_COUNTRY is transitively dependent on
STUD_NO. It violates the third normal form. To convert it in third normal form,
we will decompose the relation STUDENT (STUD_NO, STUD_NAME,
STUD_PHONE, STUD_STATE, STUD_COUNTRY_STUD_AGE) as:
STUDENT (STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,
STUD_AGE)
STATE_COUNTRY (STATE, COUNTRY)
Example 2 – Consider relation R(A, B, C, D, E)
A -> BC,
CD -> E,
B -> D,
E -> A
All possible candidate keys in above relation are {A, E, CD, BC} All attributes
are on right sides of all functional dependencies are prime.
A relation R is in BCNF if R is in Third Normal Form and for every FD, LHS is
super key. A relation is in BCNF iff in every non-trivial functional dependency X –
> Y, X is a super key.
[Link]
Example 1 – Find the highest normal form of a relation R(A,B,C,D,E) with FD
set as {BC->D, AC->BE, B->E}
Step 1. As we can see, (AC)+ ={A,C,B,E,D} but none of its subset can
determine all attribute of relation, So AC will be candidate key. A or C can’t be
derived from any other attribute of the relation, so there will be only 1 candidate
key {AC}.
Step 2. Prime attributes are those attributes that are part of candidate key {A, C}
in this example and others will be non-prime {B, D, E} in this example.
Step 3. The relation R is in 1st normal form as a relational DBMS does not
allow multi-valued or composite attribute.
The relation is in 2nd normal form because BC->D is in 2nd normal form (BC
is not a proper subset of candidate key AC) and AC->BE is in 2nd normal form
(AC is candidate key) and B->E is in 2nd normal form (B is not a proper subset
of candidate key AC).
The relation is not in 3rd normal form because in BC->D (neither BC is a super
key nor D is a prime attribute) and in B->E (neither B is a super key nor E is a
prime attribute) but to satisfy 3rd normal for, either LHS of an FD should be
super key or RHS should be prime attribute.
[Link]
61
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Key Points –
BCNF is free from redundancy.
If a relation is in BCNF, then 3NF is also satisfied.
If all attributes of relation are prime attribute, then the relation is always in 3NF.
A relation in a Relational Database is always and at least in 1NF form.
Every Binary Relation ( a Relation with only 2 attributes ) is always in BCNF.
If a Relation has only singleton candidate keys( i.e. every candidate key consists of
only 1 attribute), then the Relation is always in 2NF( because no Partial functional
dependency possible).
Sometimes going for BCNF form may not preserve functional dependency. In that
case go for BCNF only if the lost FD(s) is not required, else normalize till 3NF only.
There are many more Normal forms that exist after BCNF, like 4NF and more. But
in real world database systems it’s generally not required to go beyond BCNF.
Exercise 1: Find the highest normal form in R (A, B, C, D, E) under following functional
dependencies.
ABC --> D
CD --> AE
[Link]
Important Points for solving above type of question.
1) It is always a good idea to start checking from BCNF, then 3 NF, and so on.
2) If any functional dependency satisfied a normal form then there is no need to check for
lower normal form. For example, ABC –> D is in BCNF (Note that ABC is a superkey), so
no need to check this dependency for lower normal forms.
Candidate keys in the given relation are {ABC, BCD}
BCNF: ABC -> D is in BCNF. Let us check CD -> AE, CD is not a super
key so this dependency is not in BCNF. So, R is not in BCNF.
3NF: ABC -> D we don’t need to check for this dependency as it already
satisfied BCNF. Let us consider CD -> AE. Since E is not a prime attribute, so the
relation is not in 3NF.
2NF: In 2NF, we need to check for partial dependency. CD is a proper
subset of a candidate key and it determines E, which is non-prime attribute. So,
given relation is also not in 2 NF. So, the highest normal form is 1 NF.
[Link]
62
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
Transitive rule is implied, find the cover of A+ and compute.
Now start with x={ISBN}
The FD ISBN--> TITLE, PUBLISHER has LHS which is completely contained in
current attribute set x.
Extend x by FD RHS attribute set, giving x={ISBN,TITLE,PUBLISHER}
Now FD:PUBLISHER -->PU_URL is applicable
Add RHS attribute set of FD to current attribute SET x, giving
x={ISBN,TITLE,PUBLISHER,PU_URL}
Here we can conclude that ISBN-->PU_URL
Multivalued Dependencies
The 4th Normal Form can cause the Multivalued Dependencies. If a relation is in Boyce
codee Normal form, it has to remove the multivalued Dependencies.
Explanation − The multivalued dependencies is that, if there is a dependency or relation in a
table, then one value has multiple dependencies occur.
Let us consider an example as given below. Consider the following table −
id department shift
1 coding day
2 Hr day
[Link]
63
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
id department shift
3 Network night
In the above table, id 2 has two departments Hr and Network. And shift timing day and
night.
When we select the details with the id 2, then it will result the table as follows −
id department shift
2 Hr day
2 Network night
2 Hr night
2 Network day
This means there exist multivalued dependencies. In this, the relation between department
[Link]
and shift is nothing.
This can be rectified by removing the multivalued dependency as, making this data in to two
tables as below −
Table 1
id department
1 coding
2 Hr
2 network
Table 2
id shift
1 day
2 day
[Link]
64
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
id shift
2 night
The 4th normal form is applied to remove the multivalued dependencies in the data table.
The fourth normal form thus defines the multivalued dependencies.
If two or more independent relation are kept in a single relation or we can say multivalue
dependency occurs when the presence of one or more rows in a table implies the presence
of one or more other rows in that same table. Put another way, two attributes (or columns)
in a table are independent of one another, but both depend on a third attribute.
For a dependency A -> B, if for a single value of A, multiple value of B exists, then the
table may have multi-valued dependency. The table should have at least 3 attributes and B
and C should be independent for A ->> B multivalued dependency. For example,
[Link] Person
Mahesh
Mobile
9893/9424
Food_Likes
Burger / pizza
Person->-> mobile,
Person ->-> food_likes
This is read as “person multidetermines mobile” and “person multidetermines food_likes.”
Note that a functional dependency is a special case of multivalued dependency. In a
functional dependency X -> Y, every x determines exactly one y, never more than one.
Fourth normal form (4NF) is a level of database normalization where there are no non-
trivial multivalued dependencies other than a candidate key. It builds on the first three
normal forms (1NF, 2NF and 3NF) and the Boyce-Codd Normal Form (BCNF). It states
that, in addition to a database meeting the requirements of BCNF, it must not contain more
than one multivalued dependency.
Properties – A relation R is in 4NF if and only if the following conditions are satisfied:
[Link]
65
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
S1 A
S2 B
CID CNAME
[Link] C1
C2
C
S1 A C1 C
S1 A C2 D
S2 B C1 C
S2 B C2 D
[Link]
66
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Example –
Table – R1
[Link]
Company Product
C1 pendrive
C1 mic
C2 speaker
C2 speaker
Company->->Product
Table – R2
Agent Company
Aman C1
Aman C2
[Link]
67
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Agent Company
Mohan C1
Agent->->Company
Table – R3
Agent Product
Aman pendrive
Aman mic
Aman speaker
Mohan speaker
[Link]
Agent->->Product
Table – R1⋈R2⋈R3
Company Product Agent
C1 pendrive Aman
C1 mic Aman
C2 speaker speaker
C1 speaker Aman
Agent->->Product
Fifth Normal Form / Projected Normal Form (5NF):
A relation R is in 5NF if and only if every join dependency in R is implied by the candidate
keys of R. A relation decomposed into two relations must have loss-less join Property,
which ensures that no spurious or extra tuples are generated, when relations are reunited
through a natural join.
[Link]
68
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Example – Consider the above schema, with a case as “if a company makes a product and
an agent is an agent for that company, then he always sells that product for the company”.
Under these circumstances, the ACP table is shown as:
Table – ACP
Agent Company Product
A1 PQR Nut
A1 PQR Bolt
A1 XYZ Nut
[Link]
A1 XYZ Bolt
A2 PQR Nut
The relation ACP is again decompose into 3 relations. Now, the natural Join of all the three
relations will be shown as:
Table – R1
Agent Company
A1 PQR
A1 XYZ
A2 PQR
Table – R2
Agent Product
[Link]
69
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Agent Product
A1 Nut
A1 Bolt
A2 Nut
Table – R3
Company Product
PQR Nut
PQR Bolt
[Link] XYZ
XYZ
Nut
Bolt
Result of Natural Join of R1 and R3 over ‘Company’ and then Natural Join of R13 and R2
over ‘Agent’and ‘Product’ will be table ACP.
Hence, in this example, all the redundancies are eliminated, and the decomposition of ACP
is a lossless join decomposition. Therefore, the relation is in 5NF as it does not violate the
property of lossless join.
[Link]
70
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Unit 4
TRANSACTIONS
Example: Suppose an employee of bank transfers Rs 800 from X's account to Y's account.
This small transaction contains several low-level tasks:
X's Account
Open_Account(X)
1. Old_Balance = [Link]
2. New_Balance = Old_Balance - 800
3. [Link] = New_Balance
4. Close_Account(X)
Y's Account
[Link]
1. Open_Account(Y)
2. Old_Balance = [Link]
3. New_Balance = Old_Balance + 800
4. [Link] = New_Balance
5. Close_Account(Y)
Operations of Transaction:
Read(X): Read operation is used to read the value of X from the database and stores it in a
buffer in main memory.
Write(X): Write operation is used to write the value back to the database from the buffer.
Let's take an example to debit transaction from an account which consists of following
operations:
1. R(X);
2. X = X - 500;
3. W(X);
Let's assume the value of X before starting of the transaction is 4000.
o The first operation reads X's value from database and stores it in a buffer.
o The second operation will decrease the value of X by 500. So buffer will contain
3500.
o The third operation will write the buffer's value to the database. So X's final value will
be 3500.
But it may be possible that because of the failure of hardware, software or power, etc. that
transaction may fail before finished all the operations in the set.
[Link]
71
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
For example: If in the above transaction, the debit transaction fails after executing operation
2 then X's value will remain 4000 in the database which is not acceptable by the bank.
TRANSACTION RECOVERY
UNDO and REDO: lists of transactions UNDO = all transactions running at the last
checkpoint REDO = empty For each entry in the log, starting at the last checkpoint If a
BEGIN TRANSACTION entry is found for T Add T to UNDO If a COMMIT entry is found
for T Move T from UNDO to REDO
[Link]
Undo or transaction backout recovery is performed by the DBMS Server. For
example, when a transaction is aborted, transaction log file information is used to roll
back all related updates. The DBMS Server writes the Compensation Log Records
(CLRs) to record a history of the actions taken during undo operations.
Redo Operation
A Redo recovery operation is database-oriented. Redo recovery is performed after a
server or an installation fails. Its main purpose is to recover the contents of the DMF
cached data pages that are lost when a fast-commit server fails. Redo recovery is
performed by the recovery process. Redo recovery precedes undo recovery.
Redo Operation in a Cluster Environment
In an Ingres cluster environment where all nodes are active, the local recovery server
performs transaction redo/undo for a failed DBMS server on its node, just like in the
non-cluster case. The difference in a cluster installation is that if the recovery process
(RCP) dies on one node, either because of an Ingres failure, or a general failure of the
hardware, an RCP on another node will take responsibility for cleaning up
transactions for the failed nodes.
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
[Link]
72
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
SYSTEM RECOVERY
Any transaction that was running at the time of failure needs to be undone and
restarted
Any transactions that committed since the last checkpoint need to be redone
Transactions of type T1 need no recovery • Transactions of type T3 or T5 need to be
undone and restarted
Transactions of type T2 or T4 need to be redone.
[Link]
Media Failures
System failures are not too severe • Only information since the last checkpoint is
affected • This can be recovered from the transaction log • Media failures (disk crashes etc)
are more serious • The data stored to disk is damaged • The transaction log itself may be
damaged
Recovery from Media Failure
• Restore the database from the last backup
• Use the transaction log to redo any changes made since the last backup
• If the transaction log is damaged you can’t do step 2
• Store the log on a separate physical device to the database
• The risk of losing both is then reduced.
MEDIA RECOVERY
If you restore the archived redo log files and data files, then you must perform media
recovery before you can open the database. Any database transactions in the archived redo
log files not reflected in the data files are applied to the data files, bringing them to a
transaction-consistent state before the database is opened.
Media recovery requires a control file, data files (typically restored from backup), and
online and archived redo log files containing changes since the time the data files were
backed up. Media recovery is most often used to recover from media failure, such as the loss
of a file or disk, or a user error, such as the deletion of the contents of a table.
In a complete recovery, you restore backup data files and apply all changes from the
archived and online redo log files to the data files. The database is returned to its state at the
time of failure and can be opened with no loss of data.
RMAN enables you to perform both a complete and a point-in-time recovery of your
database. However, this documentation focuses on complete recovery.
[Link]
Two Phase commit protocol is a type of distributed commit protocol. There are two
different types of databases. In a local database system, every transaction needs to be
committed. Therefore, the transaction manager has the role to commit the decision by
conveying it to the reporting manager.
when it comes to a distributed system, the transaction manager should convey it from
all the servers from various sites included in the distributed system to commit the decision.
When each server completes the processing at each site. The transaction reaches a partially
committed state. But it has to wait until all the transaction reaches that state. Once all the
transactions from different servers reach the partially committed state, the transaction
manager can commit the transaction. However, it is necessary that all the sites must commit
the transaction.
SAVE POINTS
[Link]
74
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
transaction that can be "rolled back to" without affecting any work done in the transaction
before the savepoint was created.
SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to
that point whenever required.
SAVEPOINT savepoint_name;
Copy
In short, using this command we can name the different states of our data in any table and
then rollback to that state using the ROLLBACK command whenever required.
savepoint
Savepoint names must be distinct within a given transaction. If you create a second savepoint
[Link]
with the same identifier as an earlier savepoint, then the earlier savepoint is erased. After a
savepoint has been created, you can either continue processing, commit your work, roll back
the entire transaction, or roll back to the savepoint.
Example
Creating Savepoints: Example To update the salary for Banda and Greene in the sample
table [Link], check that the total department salary does not exceed 314,000, then
reenter the salary for Greene:
UPDATE employees
SAVEPOINT banda_sal;
UPDATE employees
SAVEPOINT greene_sal;
UPDATE employees
COMMIT;
Recovery Facilities
Checkpoint facility allows updates to the database for getting the latest patches to be made
permanent and keep secure from vulnerability. Recovery manager allows the database system
for restoring the database to a reliable and steady-state after any failure occurs.
In addition to the advanced facilities noted above, SQL is rich in the type of ease of
use capabilities that are necessary to support relational databases from the simple to the
complex. Table Facility First and foremost, SQL provides a table facility that enables a
prompted, intuitive interface for the following functions: 9 Defining databases 9 Populating
databases with rows 9 Manipulating databases.
Table Editor SQL also provides a table editor that makes it easy for you to perform
[Link]
the following functions against rows in table data that is structured in row and column
format:. 9 Access 9 Insert 9 Update 9 Delete Query Facility: With the Query facility, SQL
permits you to interactively define queries and have results displayed in a variety of report
formats including the following: 9 Tabular 9 Matrix 9 Free format For those readers who
have a System i5 background, you will notice that SQL brings with it its own naming scheme
that is significantly different from corresponding native objects. See table 4-1 for specifics
CONCURRENCY
Database concurrency is the ability of a database to allow multiple users to affect
multiple transactions. This is one of the main properties that separates a database from other
forms of data storage, like spreadsheets.
The ability to offer concurrency is unique to databases. Spreadsheets or other flat file
means of storage are often compared to databases, but they differ in this one important
regard.
Spreadsheets cannot offer several users the ability to view and work on the different
data in the same file, because once the first user opens the file it is locked to other users.
Other users can read the file, but may not edit data.
[Link]
76
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
The ability to offer concurrency is unique to databases. Spreadsheets or other flat file
means of storage are often compared to databases, but they differ in this one important
regard.
Spreadsheets cannot offer several users the ability to view and work on the different
data in the same file, because once the first user opens the file it is locked to other users.
Other users can read the file, but may not edit data.
LOCKING PROTOCOLS
[Link]
concurrency problem can be solved by securing or locking a transaction to a specific user.
The lock is a variable that specifies which activities are allowed on a certain data item.
In DBMS Lock based Protocols, there are two modes for locking and unlocking data
items Shared Lock (lock-S) and Exclusive Lock (lock-X). Let's go through the two types of
locks in detail:
Shared Lock
Shared Locks, which are often denoted as lock-S(), are defined as locks that provide
Read-Only access to the information associated with them. Whenever a shared lock is
used on a database, it can be read by several users, but these users who are reading the
information or the data items will not have the permission to edit it or make any
changes to the data items.
To put it another way, we can say that shared locks don't provide the access to write.
Because numerous users can read the data items simultaneously, multiple shared locks
can be installed on them at the same time, but the data item must not have any other
locks connected with it.
A shared lock, also known as a read lock, is solely used to read data objects. Read
integrity is supported via shared locks.
Shared locks can also be used to prevent records from being updated.
S-lock is requested via the Lock-S instruction.
Exclusive Lock
Exclusive Lock allows the data item to be read as well as written. This is a one-time
use mode that can't be utilized on the exact data item twice. To obtain X-lock, the user
[Link]
77
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
needs to make use of the lock-x instruction. After finishing the 'write' step,
transactions can unlock the data item.
By imposing an X lock on a transaction that needs to update a person's account
balance, for example, you can allow it to proceed. As a result of the exclusive lock,
the second transaction is unable to read or write.
The other name for an exclusive lock is write lock.
At any given time, the exclusive locks can only be owned by one transaction.
Example of exclusive locks: Consider the instance where the value of a data item X is equal
to 50 and a transaction requires a deduction of 20 from the data item X. By putting a Y lock
on this particular transaction, we can make it possible. As a result, the exclusive lock prevents
any other transaction from reading or writing.
There are basically four lock based protocols in dbms namely Simplistic Lock
Protocol, Pre-claiming Lock Protocol, Two-phase Locking Protocol, and Strict Two-Phase
Locking Protocol. Let's go through each of these lock-based protocols in detail.
[Link]
be unlocked.
1. Growing Phase: In this phase, we can acquire new locks on data items but none of
these locks can be released.
2. Shrinking Phase: In this phase, the existing locks can be released but no new locks
can be obtained.
Two-phase locking helps to reduce the amount of concurrency in a schedule but just like
the two sides of a coin two-phase locking has a few cons too. The protocol raises transaction
processing costs and may have unintended consequences. The likelihood of establishing
deadlocks is one bad result.
retention of all exclusive locks until the transaction commits or aborts. The two-phase is with
deadlock.
It is responsible for assuring that if 1 transaction modifies data, there can be no other
transaction that will be able to read it until the first transaction commits. The majority of
database systems use a strict two-phase locking protocol.
Deadlock
When a transaction must wait an unlimited period for a lock, it is referred to as starvation.
The following are the causes of starvation :
Let's know how starvation can be prevented. Random process selection for resource or
processor allocation should be avoided since it encourages hunger. The resource allocation
priority scheme should contain ideas like aging, in which a process' priority rises as it waits
longer. This prevents starvation.
Deadlock- In a circular chain, a deadlock situation occurs when two or more processes
are expecting each other to release a resource, or when more than 2 processes are waiting for
the resource.
[Link]
Two-Phase Locking –
This is just a skeleton transaction that shows how unlocking and locking work with 2-PL.
Note for:
[Link]
79
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Transaction T1:
The growing Phase is from steps 1-3.
The shrinking Phase is from steps 5-7.
Lock Point at 3
Transaction T2:
The growing Phase is from steps 2-6.
The shrinking Phase is from steps 8-9.
Lock Point at 6
DEADLOCK
[Link]
lock, and similarly, transaction T2 will wait for transaction T1 to give up the lock. As a
consequence, All activity comes to a halt and remains at a standstill forever unless the
DBMS detects the deadlock and aborts one of the transactions.
Deadlock Avoidance –
When a database is stuck in a deadlock, It is always better to avoid the deadlock rather than
restarting or aborting the database. The deadlock avoidance method is suitable for smaller
databases whereas the deadlock prevention method is suitable for larger databases.
One method of avoiding deadlock is using application-consistent logic. In the above-given
example, Transactions that access Students and Grades should always access the tables in
the same order. In this way, in the scenario described above, Transaction T1 simply waits
for transaction T2 to release the lock on Grades before it begins. When transaction T2
releases the lock, Transaction T1 can proceed freely.
Another method for avoiding deadlock is to apply both row-level locking mechanism and
READ COMMITTED isolation level. However, It does not guarantee to remove deadlocks
completely.
[Link]
80
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Deadlock Detection –
When a transaction waits indefinitely to obtain a lock, The database management system
should detect whether the transaction is involved in a deadlock or not.
Wait-for-graph is one of the methods for detecting the deadlock situation. This method is
suitable for smaller databases. In this method, a graph is drawn based on the transaction and
their lock on the resource. If the graph created has a closed-loop or a cycle, then there is a
deadlock.
For the above-mentioned scenario, the Wait-For graph is drawn below
Deadlock prevention –
For a large database, the deadlock prevention method is suitable. A deadlock can be
[Link]
prevented if the resources are allocated in such a way that deadlock never occurs. The
DBMS analyzes the operations whether they can create a deadlock situation or not, If they
do, that transaction is never allowed to be executed.
Deadlock prevention mechanism proposes two schemes :
Wait-Die Scheme –
In this scheme, If a transaction requests a resource that is locked by another transaction,
then the DBMS simply checks the timestamp of both transactions and allows the older
transaction to wait until the resource is available for execution.
Suppose, there are two transactions T1 and T2, and Let the timestamp of any transaction
T be TS (T). Now, If there is a lock on T2 by some other transaction and T1 is
requesting for resources held by T2, then DBMS performs the following actions:
Checks if TS (T1) < TS (T2) – if T1 is the older transaction and T2 has held some
resource, then it allows T1 to wait until resource is available for execution. That means
if a younger transaction has locked some resource and an older transaction is waiting
for it, then an older transaction is allowed to wait for it till it is available. If T1 is an
older transaction and has held some resource with it and if T2 is waiting for it, then T2
is killed and restarted later with random delay but with the same timestamp. i.e. if the
older transaction has held some resource and the younger transaction waits for the
resource, then the younger transaction is killed and restarted with a very minute delay
with the same timestamp.
This scheme allows the older transaction to wait but kills the younger one.
by an older one, then the younger transaction is asked to wait till the older one releases
it.
SERIALIZABILITY
A schedule is serialized if it is equivalent to a serial schedule. A concurrent schedule must
ensure it is the same as if executed serially means one after another. It refers to the sequence
of actions such as read, write, abort, commit are performed in a serial manner.
Example
T1 T2
READ1(A)
WRITE1(A)
READ1(B)
C1
[Link] READ2(B)
WRITE2(B)
READ2(B)
C2
Non serial schedule − When a transaction is overlapped between the transaction T1 and T2.
Example
T1 T2
READ1(A)
WRITE1(A)
[Link]
82
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
T1 T2
READ2(B)
WRITE2(B)
READ1(B)
WRITE1(B)
READ1(B)
Types of serializability
[Link]
A schedule is view-serializability if it is viewed equivalent to a serial schedule.
The rules it follows are as follows −
T1 is reading the initial value of A, then T2 also reads the initial value of A.
T1 is the reading value written by T2, then T2 also reads the value written by T1.
T1 is writing the final value, and then T2 also has the write operation as the final
value.
Conflict serializability
It orders any conflicting operations in the same way as some serial execution. A pair of
operations is said to conflict if they operate on the same data item and one of them is a write
operation.
That means
Readi(x) readj(x) - non conflict read-read operation
Readi(x) writej(x) - conflict read-write operation.
Writei(x) readj(x) - conflict write-read operation.
Writei(x) writej(x) - conflict write-write operation.
RECOVERY ISOLATION LEVELS
In case of transaction the term ACID has been used significantly to state some of
important properties that a transaction must follow. We all know ACID stands for Atomicity,
Consistency, Isolation and Durability and these properties collectively called as ACID
Properties.
[Link]
83
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Properties of transaction
Isolation
It determines the visibility of transactions of other systems. A lower level allows every user
to access the same data. Therefore, it involves high risk of data privacy and security of the
system. However, a higher isolation level reduces the type of concurrency over the data but
requires more resources and is slower than lower isolation levels.
The isolation protocols help safeguards the data from unwanted transactions. They maintain
[Link]
the integrity of every data by defining how and when the changes made by one operation are
visible to others.
Levels of isolation
[Link]
84
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
[Link]
Solution
Step 1 − In the above program, the first session starts and ends without doing any
transaction.
Step 2 − The second session begins at session-level with isolation level "Repeatable
Read". Transaction A& B gets executed with these settings.
Step 3 − Once again a new transaction begins with isolation level "Read
uncommitted". This setting is used only for "Transaction C" since "Set transaction"
alone is mentioned. If the "SET transaction" is used without global or session
keywords, then these particular settings will work only for a single transaction.
Step 4 − Once again "Set Transaction" with isolation level Read committed works
only for Transaction D. (Refer step 3 for reason)
Step 5 − "Transaction E" gets continued at the "Repeatable Read" since the
transaction started at step 2 has not ended still. Transaction isolation level set at Step 3
and Step 4 vanishes once a single transaction is executed. So, automatically
"Transaction E" will refer to the prior transaction settings.
If you run a query without mentioning the BEGIN TRAN keyword then it would be
considered an implicit transition.
If you run a query that starts with BEGIN TRAN and ends with COMMIT or
ROLLBACK, then it would be considered an explicit transaction.
Transaction Properties
A database management system (DBMS) is considered a relational database management
system (RDBMS) if it follows the transactional properties, ACID.
A: Atomicity
C: Consistency
I: Isolation
D: Durability
The SQL Server takes care of the Atomicity, Consistency, and Durability of the system, and
the user has to care about the Isolation property of the transaction. The meaning of each of
these properties is described below, as it applies to a transaction.
Atomicity
Transaction work should be atomic, which means all the work is one unit. If the user
performs a transition, either the transaction should complete and perform all the asked
operations, or it should fail and don’t do anything. Atomicity deals with the transaction
[Link]
process and an RDBMS transaction does not leave the work incomplete.
Consistency
After the transaction is completed, the database should not be left in an inconsistent state,
which means the data on which transaction is applied must be logically correct, according to
the rules of the system.
Isolation
If two transactions are applied on a similar database, then both the transaction should be
isolated from each other, and the user must see the result. It can also be defined as a
transaction that should see the data only after or before the concurrent transaction process is
completed, which means if a one transaction process is in between, the other transaction
process should wait until the first transaction is completed.
For instance, if A performs a transaction process on data d1, and before the transaction
process gets completed, B also performs another transaction process on the same
data d1. Here, the isolation property will isolate the transaction process of A and B, and the
transaction process of B will only start after the transaction process of A gets completed.
Durability
Even if the system fails, the transaction should be persistent, which means, if the system fails
during a transaction process, the transaction should be dropped, too, without affecting the
data.
[Link]
86
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Dirty Reads
Lost Updates
Non-repeatable Reads
Phantom Reads
Dirty Read
This problem occurs when another process reads the changed, but uncommitted data. For
instance, if one process has changed data but not committed it yet, another process is able to
read the same data. This leads to the inconsistent state for the reader.
Lost Updates
[Link]
This problem occurs when two processes try to manipulate the same data simultaneously.
This problem can lead to data loss, or the second process might overwrite the first processs
change.
Non-repeatable Reads
This problem occurs when one process is reading the data, and another process is writing the
data. In non-repeatable reads, the first process reading the value might get two different
values, as the changed data is read a second time because the second process changes the
data.
Phantom Reads
If two same queries executed by two users show different output, then it would be a Phantom
Read problem. For instance, If user A select a query to read some data, at the same time the
user B insert some new data but the user A only get able to read the old data at the first
attempt, but when user A re-query the same statement then he/she gets a different set of data.
1. Pessimistic model - In the pessimistic model of managing concurrent data access, the
readers can block writers, and the writers can block readers.
2. Optimistic model - In the optimistic model of managing concurrent data access, the
readers cannot block writers, and the writers cannot block readers, but the writer can
block another writer.
[Link]
87
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
Note that readers are users are performing the SELECT operations. Writers are users are
performing INSERT, ALTER, UPDATE, S.E.T. operations.
Isolation Level
When we connect to a SQL server database, the application can submit queries to the
database with one of five different isolation levels. These levels are:
Read Uncommitted
Read Committed
Repeatable Read
Serializable
Snapshot
Out of these five isolation levels, Read Uncommitted, Read Committed, Repeatable Read,
and Serializable come under the pessimistic concurrency model. Snapshot comes under the
optimistic concurrency model. These levels are ordered in terms of the separation of work by
two different processes, from minimal separation to maximal.
Let's look at each of these isolation levels and how they affect concurrency of operations.
Read Uncommitted
This is the first level of isolation, and it comes under the pessimistic model of concurrency. In
[Link]
Read Uncommitted, one transaction is allowed to read the data that is about to be changed by
the commit of another process. Read Uncommitted allows the dirty read problem.
Read Committed
This is the second level of isolation and also falls under the pessimistic model of
concurrency. In the Read Committed isolation level, we are only allowed to read data that is
committed, which means this level eliminates the dirty read problem. In this level, if you are
reading data then the concurrent transactions that can delete or write data, some work is
blocked until other work is complete.
Repeatable Read
The Repeatable Read isolation level is similar to the Read Committed level and eliminates
the Non-Repeatable Read problem. In this level, the transaction has to wait till another
transaction's update or read query is complete. But if there is an insert transaction, it does not
wait for anyone. This can lead to the Phantom Read problem.
Serializable
This is the highest level of isolation in the pessimistic model. By implementing this level of
isolation, we can prevent the Phantom Read problem. In this level of isolation, we can ask
any transaction to wait until the current transaction completes.
Snapshot
Snapshot follows the optimistic model of concurrency, and this level of isolation takes a
snapshot of the current data and uses it as a copy for the different transactions. Here each
[Link]
88
Anna University, Polytechnic & Schools
[Link]
Anna University | Polytechnic | Schools
transaction has its copy of data, so if a user tries to perform a transaction like an update or
insert, it asks him to re-verify all the operation before the process gets started executing.
[Link]
[Link]
89
Anna University, Polytechnic & Schools
Transaction isolation levels, including Read Uncommitted, Read Committed, Repeatable Read, and Serializable, vary in the extent to which they prevent data manipulation anomalies like dirty reads, non-repeatable reads, and phantom reads . Read Uncommitted allows maximum concurrency but minimal consistency, allowing dirty reads. Read Committed prevents dirty reads by only accessing committed data . Repeatable Read locks data items for transaction duration, preventing non-repeatable reads. Serializable, the strictest level, provides complete isolation but at the cost of reduced concurrency and increased resource use . Each level represents a trade-off between consistency and system performance, where higher isolation ensures more data accuracy but decreases system throughput.
The ACID model in RDBMS transactions ensures reliability and integrity through four properties: Atomicity, Consistency, Isolation, and Durability. Atomicity ensures all operations within a transaction are completed; otherwise, none are . Consistency maintains the database's validity, transitioning from one valid state to another after a transaction . Isolation ensures that transactions are executed independently, preventing them from affecting each other while being processed simultaneously . Durability guarantees that once a transaction is committed, it remains so even in the event of a system failure, ensuring data permanence . These properties collectively ensure the system's robustness against faults and errors during transaction processing.
Isolation in ACID transactions refers to the requirement that transaction operations appear to be executed in isolation from other transactions, ensuring consistency and correctness even when multiple transactions are processed concurrently . Concurrency controls, on the other hand, are methods or mechanisms employed by databases to manage concurrent transaction execution, preventing issues like dirty reads or lost updates while still allowing some level of overlapping operation to improve performance . While isolation is a transaction property that defines the expected behavior, concurrency controls are practical implementations or algorithms (like locking) that ensure this behavior by managing transaction interactions.
Integrity constraints are essential for maintaining a valid relational database by enforcing rules that preserve data accuracy and reliability. Domain constraints ensure attribute values fall within a specific range or set of allowable types . Key constraints, such as primary keys, guarantee that each tuple can be uniquely identified . Referential integrity constraints ensure that relationships between tables remain consistent, especially when foreign keys are involved . Together, these constraints help uphold the logical consistency and correctness of the database, preventing invalid data modifications and maintaining the database's intended structure and relationships.
In a relational database schema, 'degree' refers to the number of attributes in a relation, essentially the number of columns in a table . It defines the scope or breadth of the data contained in each tuple of the table, impacting the structure and complexity of the relational model. 'Cardinality' refers to the number of tuples, or rows, present in a table . It indicates the volume of data points available in the relation, affecting storage and performance complexity. Both metrics are crucial for understanding and managing the size and relational characteristics of tables within a database.
Data independence in relational databases allows the structure of the database to be changed without affecting the application layer. This is primarily achieved because the database's logical schema can be altered (e.g., adding or removing tables, attributes) without altering the data access logic of the application that interacts with it . This separation ensures that development and maintenance processes are more efficient and less error-prone, enabling flexible updates and scalability of database infrastructure to accommodate growth or new requirements without extensive redevelopments.
The advantages of a relational database model include simplicity due to its tabular format, structural independence that enhances performance, ease of use with natural data structures, robust query capabilities with languages like SQL, data independence allowing structural changes without affecting applications, and scalability for handling large datasets . Disadvantages include potential complexity and limited scalability due to fixed field lengths, the likelihood of isolated databases preventing data sharing, and the increased complexity and maintenance burden as data volumes and relational structures grow . These aspects highlight the model's strengths in user-friendliness and flexibility against its challenges in scalability and integration.
Referential integrity constraints may cause issues during updates or deletions when a tuple referenced in a foreign key is modified or deleted in such a way that leaves other related records orphaned. For example, if a primary key in a parent table is deleted, any foreign key references to this key in a child table would become invalid. This violation requires careful handling, such as cascading updates or deletions, using null values, or preventing the operation altogether unless corresponding changes are made to related records . These constraints are critical for maintaining logical relationships between data across different tables.
The statement that relational databases are more scalable than hierarchical or network models needs consideration of various factors. Relational models offer flexibility and ease of use for expanding data structures without affecting existing applications, facilitating logical expansions and adaptations . However, in practice, their scalability in terms of handling large data volumes and complex relationships could be challenged due to increased query complexity and necessary joins, potentially impacting performance as relationships grow . Hierarchical and network models, although more rigid, can often directly model complex relationships at a lower level, potentially handling specific scaled scenarios with high efficiency. Overall, relational databases offer better logical scalability but may face performance constraints under heavy data relationship complexities.
Domain constraints refer to the rules that the data in each column must follow, typically pertaining to data types and value ranges. For example, a domain constraint might ensure that a 'CustomerName' cannot be NULL . In contrast, key constraints are used to enforce uniqueness in identifying tuples within a relation. A key constraint might dictate that 'CustomerID' in a table must be unique, preventing duplicate values for this identifier . Importantly, domain constraints focus on ensuring data validity based on predefined types or rules, while key constraints ensure that each tuple is uniquely identifiable.