Introduction To Dbms
Introduction To Dbms
INTRODUCTION TO DBMS(R20A0551)
B.TECH II YEAR - II SEM
(2021-22)
PREPARED
BY
S.RAJANI(Asst. Professor)
G.VAIDEHI(Asst. Professor)
B.Tech (Electronics & Communication Engineering) R-20
UNIT I:
INTRODUCTION
Database: Purpose of Database Systems,File Processing System Vs DBMS, History, Characteristic-
Three schema Architecture of a database, Functional components of a DBMS.DBMS Languages-
Database users and DBA.
UNIT II:
DATABASE DESIGN
ER Model: Objects, Attributes and its Type. Entity set and Relationship set-Design Issues of ER
model-Constraints. Keys-primary key, Super key, candidate keys. Introduction to relational
model-Tabular, Representation of Various ER Schemas. ER Diagram Notations- Goals of ER
Diagram- Weak Entity Set- Views.
UNIT III:
STRUCTURED QUERY LANGUAGE
SQL: Overview, The Form of Basic SQL Query -UNION, INTERSECT, and EXCEPT– join operations:
equi join and non equi join-Nested queries - correlated and uncorrelated- Aggregate Functions-
Null values.Views, Triggers.
UNIT IV :
DEPENDENCIES AND NORMAL FORMS
Importance of a good schema design,:- Problems encountered with bad schema designs,
Motivation for normal forms- functional dependencies, -Armstrong's axioms for FD's- Closure of
a set of FD's,- Minimal covers-Definitions of 1NF,2NF, 3NF and BCNF- Decompositions and
desirable properties.
UNIT V:
Transactions: Transaction concept, transaction state, System log, Commit point, Desirable
Properties of a Transaction, concurrent executions, serializability, recoverability, implementation
of isolation, transaction definition in SQL, Testing for serializability, Serializability by Locks-
Locking Systems with Several Lock Modes- Concurrency Control by Timestamps, validation.
TEXT BOOKS:
1) Abraham Silberschatz, Henry F. Korth, S. Sudarshan,‖ Database System
REFERENCE BOOKS:
1) Raghu Ramakrishnan, Johannes Gehrke, ―Database Management System‖, McGraw Hill.,
3rd Edition 2007.
2) Elmasri&Navathe,‖Fundamentals of Database System,‖ Addison-Wesley Publishing, 5th
Edition, 2008.
3) Date.C.J, ―An Introduction to Database‖, Addison-Wesley Pub Co, 8th Edition, 2006.
4) Peterrob, Carlos Coronel, ―Database Systems – Design, Implementation, and
Management‖, 9th Edition, Thomson Learning, 2009.
COURSE OUTCOMES:
1) Understand the basic concepts and the applications of database systems
2) Master the basics of SQL and construct queries using SQL
3) Understand the relational database design principles
4) Familiarize with the basic issues of transaction processing and concurrency control
5) Familiarize with database storage structures and access techniques
UNIT I
INTRODUCTION
DATABASE
Data:
It is a collection of information.
The facts that can be recorded and which have implicit meaning known as 'data'.
Example:
Customer -----
1.cname.
2.cno.
3.ccity.
Database:
• It is a collection of interrelated data.
• These can be stored in the form of tables.
• A database can be of any size and varying complexity.
• A database may be generated and manipulated manually or it may be computerized.
Example:
Customer database consists the fields as cname, cno, and ccity
Database System:
It is computerized system, whose overall purpose is to maintain the information and to make that the
information is available on demand.
Advantages:
1.Redundency can be reduced
2.Inconsistency can be avoided.
3.Data can be shared
4.Standards can be enforced.
5.Security restrictions can be applied.
6.Integrity can be maintained.
7.Data gathering can be possible.
8.Requirements can be balanced.
1
DATABASE MANGAEMENT SYSTEM
For example, within a company there are different departments, as well as customers, who
each need to see different kinds of data. Each employee in the company will have different
levels of access to the database with their own customized front-end application.
In a database, data is organized strictly in row and column format. The rows are called Tuple
or Record. The data items within one row may belong to different data types. On the other
hand, the columns are often called Domain or Attribute. All the data items within a single
attribute are of the same data type.
2
DATABASE MANGAEMENT SYSTEM
Databases touch all aspects of our lives. Some of the major areas of application are as
follows:
1. Banking
2. Airlines
3. Universities
4. Manufacturing and selling
5. Human resources
Enterprise Information
◦ Sales: For customer, product, and purchase information.
◦ Accounting: For payments, receipts, account balances, assets and other accounting
information.
◦ Human resources: For information about employees, salaries, payroll taxes, and benefits,
and for generation of pay checks.
◦ Manufacturing: For management of the supply chain and for tracking production of items in
factories, inventories of items in warehouses and stores, and orders for items.
Online retailers: For sales data noted above plus online order tracking, generation of
recommendation lists, and maintenance of online product evaluations.
◦ Banking: For customer information, accounts, loans, and banking transactions.
◦ Credit card transactions: For purchases on credit cards and generation of monthly
statements.
◦ Finance: For storing information about holdings, sales, and purchases of financial
instruments such as stocks and bonds; also for storing real-time market data to enable online
trading by customers and automated trading by the firm.
Universities: For student information, course registrations, and grades (in addition to
standard enterprise information such as human resources and accounting).
Airlines: For reservations and schedule information. Airlines were among the first to use
databases in a geographically distributed manner.
3
DATABASE MANGAEMENT SYSTEM
Data redundancy and inconsistency: Since different programmers create the files and
application programs over a long period, the various files are likely to have different
structures and the programs may be written in several programming languages. Moreover, the
same information may be duplicated in several places (files). For example, if a student has a
double major (say, music and mathematics) the address and telephone number of that student
may appear in a file that consists of student records of students in the Music department and
in a file that consists of student records of students in the Mathematics department. This
redundancy leads to higher storage and access cost. In addition, it may lead to data
inconsistency; that is, the various copies of the same data may no longer agree.
For example, a changed student address may be reflected in the Music department records but
not elsewhere in the system.
4
DATABASE MANGAEMENT SYSTEM
Difficulty in accessing data: Suppose that one of the university clerks needs to find out the
names of all students who live within a particular postal-code area. The clerk asks the data-
processing department to generate such a list. Because the designers of the original system
did not anticipate this request, there is no application program on hand to meet it. There is,
however, an application program to generate the list of all students.
Data isolation: 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.
Integrity problems: The data values stored in the database must satisfy certain types of
consistency
constraints: Suppose the university maintains an account for each department, and records
the balance amount in each account. Suppose also that the university requires that the account
balance of a department may never fall below zero. Developers enforce these constraints in
the system by adding appropriate code in the various application programs. However, when
new constraints are added, it is difficult to change the programs to enforce them. The problem
is compounded when constraints involve several data items from different files.
Atomicity problems: A computer system, like any other device, is subject to failure. In many
applications, it is crucial that, if a failure occurs, the data be restored to the consistent state
that existed prior to the failure.
Concurrent-access anomalies. For the sake of overall performance of the system and faster
response, many systems allow multiple users to update the data simultaneously. Indeed,
today, the largest Internet retailers may have millions of accesses per day to their data by
shoppers. In such an environment, interaction of concurrent updates is possible and may
result in inconsistent data. Consider department A, with an account balance of $10,000. If two
department clerks debit the account balance (by say $500 and $100, respectively) of
department A at almost exactly the same time, the result of the concurrent executions may
leave the budget in an incorrect (or inconsistent) state.
Suppose that the programs executing on behalf of each withdrawal read the old balance,
reduce that value by the amount being withdrawn, and write the result back. If the two
programs run concurrently, they may both read the value $10,000, and write back $9500 and
$9900, respectively. Depending on which one writes the value last, the account balance of
department A may contain either $9500 or $9900, rather than the correct value of $9400. To
guard against this possibility, the system must maintain some form of supervision. But
supervision is difficult to provide because data may be accessed by many different
application programs that have not been coordinated previously.
5
DATABASE MANGAEMENT SYSTEM
Security problems. Not every user of the database system should be able to access all the
data. For example, in a university, payroll personnel need to see only that part of the database
that has financial information. They do not need access to information about academic
records. But, since application programs are added to the file-processing system in an ad hoc
manner, enforcing such security constraints is difficult. These difficulties, among others,
prompted the development of database systems. In what follows, we shall see the concepts
and algorithms that enable database systems to solve the problems with file processing
systems.
Advantages of DBMS:
Controlling of Redundancy: Data redundancy refers to the duplication of data (i.e storing
same data multiple times). In a database system, by having a centralized database and
centralized control of data by the DBA the unnecessary duplication of data is avoided. It also
eliminates the extra time for processing the large volume of data. It results in saving the
storage space.
Improved Data Sharing : DBMS allows a user to share the data in any number of
application programs.
Data Integrity : Integrity means that the data in the database is accurate. Centralized control
of the data helps in permitting the administrator to define integrity constraints to the data in
the database. For example: in customer database we can can enforce an integrity that it must
accept the customer only from Noida and Meerut city.
Security : Having complete authority over the operational data, enables the DBA in ensuring
that the only mean of access to the database is through proper channels. The DBA can define
authorization checks to be carried out whenever access to sensitive data is attempted.
Data Consistency : By eliminating data redundancy, we greatly reduce the opportunities for
inconsistency. For example: is a customer address is stored only once, we cannot have
disagreement on the stored values. Also updating data values is greatly simplified when each
value is stored in one place only. Finally, we avoid the wasted storage that results from
redundant data storage.
Efficient Data Access : In a database system, the data is managed by the DBMS and all
access to the data is through the DBMS providing a key to effective data processing
Enforcements of Standards : With the centralized of data, DBA can establish and enforce
the data standards which may include the naming conventions, data quality standards etc.
6
DATABASE MANGAEMENT SYSTEM
Data Independence : Ina database system, the database management system provides the
interface
between the application programs and the data. When changes are made to the data
representation, the meta data obtained by the DBMS is changed but the DBMS is continues to
provide the data to application program in the previously used way. The DBMs handles the
task of transformation of data wherever necessary.
Reduced Application Development and Maintenance Time : DBMS supports many
important functions that are common to many applications, accessing data stored in the
DBMS, which facilitates the quick development of application
Disadvantages of DBMS
• It is bit complex. Since it supports multiple functionality to give the user the best, the
underlying software has become complex. The designers and developers should have
thorough knowledge about the software to get the most out of it.
• Because of its complexity and functionality, it uses large amount of memory. It also
needs large memory to run efficiently.
• DBMS system works on the centralized system, i.e.; all the users from all over the
world access
• this database. Hence any failure of the DBMS, will impact all the users.
• DBMS is generalized software, i.e.; it is written work on the entire systems rather
specific one. Hence some of the application will run slow.
7
DATABASE MANGAEMENT SYSTEM
8
DATABASE MANGAEMENT SYSTEM
These people maintain personal databases by using ready-made program packages that
provide easy to use menu based interfaces.
4.System Analyst:
These people determine the requirements of end users and develop specifications for transactions.
5.Application Programmers (Software Engineers):
These people can test, debug, document and maintain the specified transactions.
9
DATABASE MANGAEMENT SYSTEM
10
DATABASE MANGAEMENT SYSTEM
This code defines a new record type called instructor with four fields. Each field has a name
and a type associated with it. A university organization may have several such record types,
including
1
• department, with fields dept_name, building, and budget
• course, with fields course_id, title, dept_name, and credits
• student, with fields ID, name, dept_name, and tot_cred
At the physical level, an instructor, department, or student record can be described as a block
of consecutive storage locations. At the logical level, each such record is described by a type
definition, as in the previous code segment, and the interrelationship of these record types is
defined as well. Finally, at the view level, computer users see a set of application programs
that hide details of the data types. At the view level, several views of the database are
defined, and a database user sees some or all of these views.
11
DATABASE MANGAEMENT SYSTEM
UNIT II
DATABASE DESIGN
ER Model
Data Models
Underlying the structure of a database is the data model: a collection of conceptual tools for
describing data, data relationships, data semantics, and consistency constraints.
The data models can be classified into four different categories:
12
• Relational Model. The relational model uses a collection of tables to represent both
data and the relationships among those data. Each table has multiple columns, and
each column has a unique name. Tables are also known as relations. The relational
model is an example of a record-based model.
• Entity-Relationship Model. The entity-relationship (E-R) data model uses a
collection of basic objects, called entities, and relationships among these objects.
Suppose that each department has offices in several locations and we want to record
the locations at which each employee works. The ER diagram for this variant of
Works In, which we call Works In2
Example - ternary
12
DATABASE MANGAEMENT SYSTEM
13
DATABASE MANGAEMENT SYSTEM
As a result they are used little now, except in old database code that is still in service in some
places.
Database Languages
A database system provides a data-definition language to specify the database schema and a
data-manipulation language to express database queries and updates. In practice, the data
definition and data-manipulation languages are not two separate languages; instead they
simply form parts of a single database language, such as the widely used SQL language.
Data-Manipulation Language
A data-manipulation language (DML) is a language that enables users to access or
manipulate data as organized by the appropriate data model. The types of access are:
• Retrieval of information stored in the database
• Insertion of new information into the database
• Deletion of information from the database
• Modification of information stored in the database
There are basically two types:
• Procedural DMLs require a user to specify what data are needed and how to get those data.
• Referential Integrity. There are cases where we wish to ensure that a value that appears in
one relation for a given set of attributes also appears in a certain set of attributes in another
relation (referential integrity). For example, the department listed for each course must be one
that actually exists. More precisely, the dept name value in a course record must appear in the
dept name attribute of some record of the department relation.
• Assertions. An assertion is any condition that the database must always satisfy. Domain
constraints and referential-integrity constraints are special forms of assertions. However,
there are many constraints that we cannot express by using only these special forms. For
example, “Every department must have at least five courses offered every semester” must be
expressed as an assertion..
• Authorization. We may want to differentiate among the users as far as the type of access
they are permitted on various data values in the database. These differentiations are expressed
in terms of authorization, the most common being: read authorization, which allows
reading, but not modification, of data; insert authorization, which allows insertion of new
data, but not modification of existing data; update authorization, which allows
modification, but not deletion, of data; and delete authorization, which allows deletion of
data. We may assign the user all, none, or a combination of these types of authorization. The
DDL, just like any other programming language, gets as input some instructions (statements)
and generates some output. The output of the DDL is placed in the data dictionary,which
contains metadata—that is, data about data.
Data Dictionary
We can define a data dictionary as a DBMS component that stores the definition of data
characteristics and relationships. You may recall that such “data about data” were labelled
metadata. The DBMS data dictionary provides the DBMS with its self describing
characteristic. In effect, the data dictionary resembles and X-ray of the company’s entire data
set, and is a crucial element in the data administration function.
For example, the data dictionary typically stores descriptions of all:
• Data elements that are define in all tables of all databases. Specifically the data dictionary
stores the name, data types, display formats, internal storage formats, and validation rules.
The data dictionary tells where an element is used, by whom it is used and so on.
• Tables define in all databases. For example, the data dictionary is likely to store the name of
the table creator, the date of creation access authorizations, the number of columns, and so
on.
15
DATABASE MANGAEMENT SYSTEM
• Indexes define for each database tables. For each index the DBMS stores at least the index
name the attributes used, the location, specific index characteristics and the creation date.
• Define databases: who created each database, the date of creation where the database is
located, who the DBA is and so on.
• End users and The Administrators of the data base
• Programs that access the database including screen formats, report formats application
formats, SQL queries and so on.
• Access authorization for all users of all databases.
• Relationships among data elements which elements are involved: whether the relationship
are mandatory or optional, the connectivity and cardinality and so on.
Database Architecture:
The architecture of a database system is greatly influenced by the underlying computer
system on which the database system runs. Database systems can be centralized, or client-
server, where one server machine executes work on behalf of multiple client machines.
Database systems can also be designed to exploit parallel computer architectures. Distributed
databases span multiple geographically separated machines.
17
DATABASE MANGAEMENT SYSTEM
Query Processor:
The query processor components include
· DDL interpreter, which interprets DDL statements and records the definitions in the data
dictionary.
· DML compiler, which translates DML statements in a query language into an evaluation
plan consisting of low-level instructions that the query evaluation engine understands. A
query can usually be translated into any of a number of alternative evaluation plans that all
give the same result. The DML compiler also performs query optimization, that is, it picks
the lowest cost evaluation plan from among the alternatives. Query evaluation engine,
which executes low-level instructions generated by the DML compiler.
Storage Manager:
A storage manager is a program module that provides the interface between the low level
data stored in the database and the application programs and queries submitted to the system.
The storage manager is responsible for the interaction with the file manager. The storage
manager components include:
• Authorization and integrity manager, which tests for the satisfaction of integrity
constraints and checks the authority of users to access data.
18
DATABASE MANGAEMENT SYSTEM
What is ER Modeling?
A graphical technique for understanding and organizing the data independent of the actual
database implementation
We need to be familiar with the following terms to go further.
Entity
Any thing that has an independent existence and about which we collect data. It is also
known as entity type. In ER modeling, notation for entity is given below.
Entity instance
Entity instance is a particular member of the entity type.
Example for entity instance : A particular employee
19
DATABASE MANGAEMENT SYSTEM
Regular Entity
An entity which has its own key attribute is a regular entity.
Example for regular entity : Employee.
Weak entity
An entity which depends on other entity for its existence and doesn't have any key attribute of
its own is a weak entity.
Example for a weak entity : In a parent/child relationship, a parent is considered as a strong
entity and the child is a weak entity.
In ER modeling, notation for weak entity is given below.
Attributes
Properties/characteristics which describe entities are called attributes.
In ER modeling, notation for attribute is given below
• Domain of Attributes
The set of possible values that an attribute can take is called the domain of the
attribute.
For example, the attribute day may take any value from the set {Monday, Tuesday ...
Friday}. Hence this set canbe termed as the domain of the attribute day.
• Key attribute
The attribute (or combination of attributes) which is unique for every entity instance
is called key attribute.
E.g the employee_id of an employee, pan_card_number of a person etc.If the key
attributeconsists of two or more attributes in combination, it is called a composite key.
In ER modeling, notation for key attribute is given below.
20
DATABASE MANGAEMENT SYSTEM
• Simple attribute
If an attribute cannot be divided into simpler components, it is a simple attribute.
Example for simple attribute : employee_id of an employee.
• Composite attribute
If an attribute can be split into components, it is called a composite attribute.
Example for composite attribute : Name of the employee which can be split into
First_name, Middle_name, and Last_name.
• Single valued Attributes
If an attribute can take only a single value for each entity instance, it is a single valued
attribute.
example for single valued attribute : age of a student. It can take only one value for a
particular student.
• Multi-valued Attributes
If an attribute can take more than one value for each entity instance, it is a multi-
valued attribute.
example for multi valued attribute : telephone number of an employee, a particular
employee may have multiple telephone numbers.
In ER modeling, notation for multi-valued attribute is given below.
• Stored Attribute
An attribute which need to be stored permanently is a stored attribute
Example for stored attribute : name of a student
• Derived Attribute
An attribute which can be calculated or derived based on other attributes is a derived
attribute.
21
DATABASE MANGAEMENT SYSTEM
Example for derived attribute : age of employee which can be calculated from date of
birth and current date.
In ER modelling, notation for derived attribute is given below.
Relationships
Associations between entities are called relationships
Example : An employee works for an organization. Here "works for" is a relation between the
entities employee and organization.
In ER modeling, notation for relationship is given below.
However in ER Modeling, To connect a weak Entity with others, you should use a weak
relationship notation as given below
Degree of a Relationship
Degree of a relationship is the number of entity types involved. The n-ary relationship is the
general form for degree n. Special cases are unary, binary, and ternary ,where the degree is 1,
2, and 3, respectively.
Example for unary relationship : An employee ia a manager of another employee
Example for binary relationship : An employee works-for department.
Example for ternary relationship : customer purchase item from a shop keeper Cardinality of
a Relationship Relationship cardinalities specify how many of each entity type is allowed.
22
DATABASE MANGAEMENT SYSTEM
One employee is assigned with only one parking space and one parking space is assigned to
only one employee. Hence it is a 1:1 relationship and cardinality is One-To-One (1:1)
23
DATABASE MANGAEMENT SYSTEM
One organization can have many employees , but one employee works in only one
organization.
Hence it is a 1:N relationship and cardinality is One-To-Many (1:N)
One employee works in only one organization But one organization can have many
employees.
24
DATABASE MANGAEMENT SYSTEM
One student can enroll for many courses and one course can be enrolled by many students.
Hence it is a M:N relationship and cardinality is Many-to-Many (M:N)
Relationship Participation
1. Total
In total participation, every entity instance will be connected through the relationship to
another instance of the other participating entity types
25
DATABASE MANGAEMENT SYSTEM
2. Partial
Example for relationship participation Consider the relationship - Employee is head of the
department. Here all employees will not be the head of the department. Only one employee
will be the head of the department. In other words, only few instances of employee entity
participate in the above relationship. So employee entity's participation is partial in the said
relationship.
Disadvantages
1. Physical design derived from E-R Model may have some amount of ambiguities or
inconsistency.
2. Sometime diagrams may lead to misinterpretations
Relational Model
The relational model is today the primary data model for commercial data processing
applications. It attained its primary position because of its simplicity, which eases the job of
the programmer,compared to earlier data models such as the network model or the
hierarchical model.
Database Schema
When we talk about a database, we must differentiate between the database schema, which
is the logical design of the database, and the database instance, which is a snapshot of the
data in the database at a given instant in time. The concept of a relation corresponds to the
programming language notion of a variable, while the concept of a relation schema
corresponds to the programming-language notion of type definition.
Keys
A key constraint is a statement that a certain minimal subset of the fields of a relation is a unique
identifier for a tuple.
Example:
The ‘students’ relation and the constraint that no 2 students have tha same student id (sid).
These can be classified into 3 types as below.
Primary Key:
This is also a candidate key, whose values are used to identify tuples in the relation. It is common to
designate one of the candidate keys as a primary key of the relation. The attributes that form the
primary key of a relation schema are underlined. It is used to denote a candidate key that is chosen by
the database designer as the principal means of identifying entities with an entity set.
A superkey:
is a set of one or more attributes that, taken collectively, allow us to identify uniquely a tuple
in the relation. For example, the ID attribute of the relation instructor is sufficient to
distinguish one instructor tuple from another. Thus, ID is a superkey. The name attribute of
instructor, on the other hand, is not a superkey, because several instructors might have the
same name. A superkey may contain extraneous attributes. For example, the combination of
ID and name is a superkey for the relation instructor. If K is a superkey, then so is any
superset of K. We are often interested in superkeys for which no proper subset is a superkey.
Such minimal superkeys are called
candidate keys:
It is customary to list the primary key attributes of a relation schema before the other
attributes;
27
DATABASE MANGAEMENT SYSTEM
for example, the dept name attribute of department is listed first, since it is the primary key.
Primary key attributes are also underlined. A relation, say r1, may include among its
attributes the primary key of another relation, say r2. This attribute is called a foreign key
from r1, referencing r2.
Schema Diagrams
A database schema, along with primary key and foreign key dependencies, can be depicted
by schema diagrams.
28
DATABASE MANGAEMENT SYSTEM
UNIT III
STRUCTURED QUERY LANGUAGE
What is SQL?
• SQL is Structured Query Language, which is a database language designed for the
retrieval and management of data in a relational database.
• All the RDBMS systems like MySQL, MS Access, Oracle, Sybase, Postgres, and
SQL Server use SQL as their standard database language.
History of SQL
"A Relational Model of Data for Large Shared Data Banks" was a paper which was published
by the great computer scientist "E.F. Codd" in 1970.
The IBM researchers Raymond Boyce and Donald Chamberlin originally developed the
SEQUEL (Structured English Query Language) after learning from the paper given by E.F.
Codd. They both developed the SQL at the San Jose Research laboratory of IBM Corporation
in 1970. In 1979, Relational Software, Inc. (now Oracle) introduced the first commercially
available implementation of SQL.
29
DATABASE MANGAEMENT SYSTEM
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of
the International Organization for Standardization (ISO) in 1987.[11] Since then, the standard
has been revised to include a larger set of features. Despite the existence of standards, most
SQL code requires at least some changes before being ported to different database systems.
New versions of the standard were published and most recently, 2016.
Types of SQL
All operations performed on the information in a database are run using SQL statements. A
SQL statement consists of identifiers, parameters, variables, names, data types, and
SQL reserved words.
What is DDL?
30
DATABASE MANGAEMENT SYSTEM
• The Commands (or) statements used to define the structute of database are:
1. CREATE
2. ALTER
3. DROP
4. TRUNCATE
5. RENAME
1.CREATE
(i) Databases
(ii) Tables and
(iii) Views.
(i)Creating Database
Syntax:
create databasedabasename;
(ii)Creating Table
Syntax:
Create table tablename(Columnname1 Datatype,
Columnname2 Datatype,
……..,
Columnnamendatatype);
Ex: create table Student(SRno integer(5),
31
DATABASE MANGAEMENT SYSTEM
Snamevarchar(20),
Address varchar(15));
2.ALTER Command
• The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
ALTERTABLE Customers
ADD Email varchar(255);
To delete a column in a table, use the following syntax (notice that some database systems
don't allow deleting a column):
Syntax:
Ex: The following SQL deletes the "Email" column from the "Customers" table:
32
DATABASE MANGAEMENT SYSTEM
ALTERTABLE Customers
DROP COLUMN Email;
To change the data type of a column in a table, use the following syntax:
33
DATABASE MANGAEMENT SYSTEM
3.Drop Command
Syntax
To drop a column in an existing table, the SQL ALTER TABLE syntax is:
Example
Let's look at an example that drops (ie: deletes) a column from a table.
For example:
TRUNCATE:
This command used to delete all the rows from the table and free the space containing the
table.
Syntax:
Example:
34
DATABASE MANGAEMENT SYSTEM
Data Manipulation Language (DML) allows user to modify the database instance by
inserting, modifying, and deleting its data. It is responsible for performing all types data
modification in a database.
There are three basic constructs which allow database program and user to enter data and
information are:
• INSERT
• UPDATE
• DELETE
INSERT:This statement is a SQL query. This command is used to insert data into the row of
a table.
Syntax:
For example:
INSERT INTO students (RollNo, FIrstName, LastName) VALUES ('60', 'Tom', Erichsen');
UPDATE:
This command is used to update or modify the value of a column in the table.
Syntax:
35
DATABASE MANGAEMENT SYSTEM
For example:
UPDATE students
SET FirstName = 'Jhon', LastName= 'Wick'
WHERE StudID = 3;
DELETE:
Syntax:
For example:
What is DCL?
DCL (Data Control Language) includes commands like GRANT and REVOKE, which are
useful to give "rights & permissions." Other permission controls parameters of the database
system.
• Grant
• Revoke
Grant:
Syntax:
36
DATABASE MANGAEMENT SYSTEM
For example:
Revoke:
Syntax:
For example:
What is TCL?
Transaction control language or TCL commands deal with the transaction within the
database.
Commit: This command is used to save all the transactions to the database.
Syntax:
Commit;
For example:
Rollback
Rollback command allows you to undo transactions that have not already been saved to the
database.
Syntax:
37
DATABASE MANGAEMENT SYSTEM
ROLLBACK;
Example:
SAVEPOINT
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Example:
SAVEPOINT RollNo;
What is DQL?
Data Query Language (DQL) is used to fetch the data from the database. It uses only one
command:
SELECT:
This command helps you to select the attribute based on the condition described by the
WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT FirstName
FROM Student
38
DATABASE MANGAEMENT SYSTEM
TCL Commands
TCL Commands in SQL- Transaction Control Language Examples: Transaction Control
Language can be defined as the portion of a database language used for maintaining
consistency of the database and managing transactions in database. A set of SQL statements
that are co-related logically and executed on the data stored in the table is known as
transaction. In this tutorial, you will learn different TCL Commands in SQL with examples
and differences between them.
1. Commit Command
2. Rollback Command
3. Savepoint Command
39
DATABASE MANGAEMENT SYSTEM
• Now have a look at the below diagram where ‘Meena’ is updated and there is a lock
on her record. The updated value is permanently saved in the database after the use of
commit and lock is released.
2. Rollback
• Using this command, the database can be restored to the last committed state.
• Additionally, it is also used with savepoint command for jumping to a savepoint in a
transaction.
Syntax:
Rollback to savepoint-name;
For example
• This command is used when the user realizes that he/she has updated the wrong
information after the student name and wants to undo this update.
40
DATABASE MANGAEMENT SYSTEM
• The users can issues ROLLBACK command and then undo the update.
Have a look at the below tables to know better about the implementation of this command.
3. Savepoint
The main use of the Savepoint command is to save a transaction temporarily. This way users
can rollback to the point whenever it is needed.
The general syntax for the savepoint command is mentioned below:
savepointsavepoint-name;
For Example
Following is the table of a school class
Use some SQL queries on the above table and then watch the results
INSERT into CLASS VALUES (101, ‘Rahul);
Commit;
UPDATE CLASS SET NAME= ‘Tyler’ where id= 101;
41
DATABASE MANGAEMENT SYSTEM
SAVEPOINT A;
INSERT INTO CLASS VALUES (102, ‘Zack’);
Savepoint B;
INSERT INTO CLASS VALUES (103, ‘Bruno’)
Savepoint C;
Select * from Class;
The result will look like
42
DATABASE MANGAEMENT SYSTEM
1. Database can be restored to the last Saves modification It saves the transaction
committed state made by DML temporarily.
Commands and it
permanently saves
the transaction.
START TRANSACTION;
savepoint a;
update t1 set n1=18 where n1=13;
rollbackto a;
In relational database the data is stored as well as retrieved in the form of relations (tables).
Table 1 shows the relational database with only one relation called STUDENT which
stores ROLL_NO, NAME, ADDRESS, PHONE and AGE of students.
43
DATABASE MANGAEMENT SYSTEM
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.
Column: Column represents the set of values for a particular attribute. The column
ROLL_NO isextracted from relation STUDENT.
ROLL_NO
The Set Operators combine a similar type of data from two or more SQL database tables. It
mixes the result, which is extracted from two or more SQL queries, into a single result.
Set operators combine more than one select statement in a single query and return a specific
result set.
44
DATABASE MANGAEMENT SYSTEM
UNION
In SQL the UNION clause combines the results of two SQL queries into a single table of all
matching rows. The two queries must result in the same number of columns and compatible
data types in order to unite. Any duplicate records are automatically removed unless UNION
ALL is used.
Syntax of UNION:
UNION
A simple example would be a database having tables sales2005 and sales2006 that have
identical structures but are separated because of performance considerations. A UNION
query could combine results from both tables.
Note that UNION ALL does not guarantee the order of rows. Rows from the second operand
may appear before, after, or mixed with rows from the first operand. In situations where a
specific order is desired, ORDER BY must be used.
Note that UNION ALL may be much faster than plain UNION.
sales2005
person amount
Joe 1000
Alex 2000
45
DATABASE MANGAEMENT SYSTEM
Bob 5000
sales2006
person amount
Joe 2000
Alex 2000
Zach 35000
yields this result set, though the order of the rows can vary because no ORDER BY clause
was supplied:
person amount
Joe 1000
Alex 2000
Bob 5000
46
DATABASE MANGAEMENT SYSTEM
Joe 2000
Zach 35000
UNION ALL gives different results, because it will not eliminate duplicates. Executing this
statement:
would give these results, again allowing variance for the lack of an ORDER BY statement:
person amount
Joe 1000
Joe 2000
Alex 2000
Alex 2000
Bob 5000
Zach 35000
47
DATABASE MANGAEMENT SYSTEM
INTERSECT
The SQL INTERSECT operator takes the results of two queries and returns only rows that
appear in both result sets. For purposes of duplicate removal the INTERSECT operator does
not distinguish between NULLs.
The INTERSECT operator removes duplicate rows from the final result set. The
INTERSECT ALL operator does not remove duplicate rows from the final result set, but if a
row appears X times in the first query and Y times in the second, it will appear min(X, Y)
times in the result set.
The data type and the number of columns must be the same for each SELECT statement used
with the INTERSECT operator.
Syntax of INTERSECT
INTERSECT
Let's understand the below example which explains how to execute INTERSECT operator in
Structured Query Language:
In this example, we used two tables. Both tables have four columns Emp_Id, Emp_Name,
Emp_Salary, and Emp_City.
Employee_details1:
48
DATABASE MANGAEMENT SYSTEM
Employee_details2:
Suppose, we want to see a common record of the employee from both the tables in a single
output. For this, we have to write the following query in SQL:
INTERSECT
EXCEPT
The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do
not appear in a second result set. For purposes of row elimination and duplicate removal, the
EXCEPT operator does not distinguish between NULLs. The EXCEPT ALL operator does
not remove duplicates, but if a row appears X times in the first query and Y times in the
second, it will appear max(X - Y, 0) times in the result set.
Notably, the Oracle platform provides a MINUS operator which is functionally equivalent to
the SQL standard EXCEPT DISTINCT operator.
49
DATABASE MANGAEMENT SYSTEM
The following example EXCEPT query returns all rows from the Orders table where
Quantity is between 1 and 49, and those with a Quantity between 76 and 100.
Worded another way; the query returns all rows where the Quantity is between 1 and 100,
apart from rows where the quantity is between 50 and 75.
EXCEPT
Joins
A join is a query that combines rows from two or more tables, views, based on a common
field between them.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|3|kaushik|23|Kota|2000.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
|102|2009-10-0800:00:00|3|3000|
50
DATABASE MANGAEMENT SYSTEM
|100|2009-10-0800:00:00|3|1500|
|101|2009-11-2000:00:00|2|1560|
|103|2008-05-2000:00:00|4|2060|
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as shown below.
+----+----------+-----+--------+
| ID | NAME | AGE | AMOUNT |
+----+----------+-----+--------+
| 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
| 2 | Khilan | 25 | 1560 |
| 4 | Chaitali | 25 | 2060 |
+----+----------+-----+--------+
Here, it is noticeable that the join is performed in the WHERE clause. Several operators can
be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they
can all be used to join tables. However, the most common operator is the equal to symbol.
The are two types of SQL JOINS - EQUI JOIN and NON EQUI JOIN
The SQL EQUI JOIN is a simple SQL join uses the equal sign(=) as the comparison operator
for the condition. It has two types - SQL Outer join and SQL Inner join.
The SQL NON EQUI JOIN is a join uses comparison operator other than the equal sign like
>, <, >=, <= with the condition.
51
DATABASE MANGAEMENT SYSTEM
The SQL EQUI JOIN can be classified into two types - INNER JOIN and OUTER JOIN
This type of EQUI JOIN returns all rows from tables where the key record of one table is
equal to the key records of another table.
This type of EQUI JOIN returns all rows from one table and only those rows from the
secondary table where the joined condition is satisfying i.e. the columns are equal in both
tables.
b) Name of the columns of two or more tables, based on which a condition will perform.
Syntax:
FROM table1
join_type table2
[ON (join_condition)]
ON can be replaced with WHERE
52
DATABASE MANGAEMENT SYSTEM
3 Hina 3 Delhi
4 Megha 2 Delhi
6 Gouri 2 Delhi
id Class City
9 3 Delhi
10 2 Delhi
12 2 Delhi
53
DATABASE MANGAEMENT SYSTEM
EQUI JOIN :
EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative tables.
EQUI JOIN also create JOIN by using JOIN with ON and then providing the names of the
columns with their relative tables to check equality using equal sign (=).
Syntax :
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
Example –
SELECT student.name, student.id, record.class, record.city
FROM student, record
WHERE student.city = record.city;
Output :
Hina 3 3 Delhi
Megha 4 3 Delhi
Gouri 6 3 Delhi
Hina 3 2 Delhi
Megha 4 2 Delhi
Gouri 6 2 Delhi
Hina 3 2 Delhi
54
DATABASE MANGAEMENT SYSTEM
Megha 4 2 Delhi
Gouri 6 2 Delhi
Syntax:
SELECT *
Example –
SELECT student.name, record.id, record.city
Output :
name Id city
Hina 9 Delhi
Megha 9 Delhi
Gouri 9 Delhi
Hina 10 Delhi
Megha 10 Delhi
55
DATABASE MANGAEMENT SYSTEM
name Id city
Gouri 10 Delhi
Hina 12 Delhi
Megha 12 Delhi
Gouri 12 Delhi
• The inner SELECT statement retrieves the IDs of departments with location ID 1800.
These department IDs are needed by the outer query block, which retrieves names of
employees in the departments whose IDs were supplied by the subquery.
• The structure of the SQL statement does not force the database to execute the inner
query first. For example, the database could rewrite the entire query as a join of
employees and departments, so that the subquery never executes by itself.
Subqueries can be correlated or uncorrelated.
Correlated subquery - In correlated subquery, inner query is dependent on the outer query.
Outer query needs to be executed before inner query
56
DATABASE MANGAEMENT SYSTEM
Non-Correlated subquery - In non-correlated query inner query does not dependent on the
outer query. They both can run separately.
Correlated Subqueries
A correlated subquery typically obtains values from its outer query before it executes. When
the subquery returns, it passes its results to the outer query.
In the following example, the subquery needs values from the addresses.state column in the
outer query:
• The query extracts and evaluates each addresses.state value in the outer subquery
records.
• Then the query—using the EXISTS predicate—checks the addresses in the inner
(correlated) subquery.
• Because it uses the EXISTS predicate, the query stops processing when it finds the
first match.
NoncorrelatedSubqueries
A noncorrelatedsubquery executes independently of the outer query. The subquery executes
first, and then passes its results to the outer query, For example:
=> SELECT name, street, city, state FROM addresses WHERE state IN (SELECT state
FROM states);
This query is executed as follows:
57
DATABASE MANGAEMENT SYSTEM
In WHERE clause
=> SELECT COUNT(*) FROM SubQ1 WHERE SubQ1.a = (SELECT y from SubQ2);
In HAVING clause
=> SELECT COUNT(*) FROM SubQ1 GROUP BY SubQ1.a HAVING SubQ1.a =
(SubQ1.a & (SELECT y from SubQ2)
Aggregate functions:
Aggregate functions operate on values across rows to perform mathematical calculations such
as sum, average, counting, minimum/maximum values, standard deviation, and estimation, as
well as some non-mathematical operations.
An aggregate function takes multiple rows (actually, zero, one, or more rows) as input and
produces a single output.
Various Aggregate Functions:
1. COUNT ([DISTINCT] A): The number of (unique) values in the A column.
2. SUM ([DISTINCT] A): The sum of all (unique) values in the A column.
3. AVG ([DISTINCT] A): The average of all (unique) values in the A column.
4. MAX (A): The maximum value in the A column.
5. MIN (A): The minimum value in the A column.
selectx,yfromsimpleorderbyx,y;
+----+----+
| X| Y|
|----+----|
| 10 | 20 |
| 20 | 44 |
| 30 | 70 |
+----+----+
The aggregate function returns one output row for multiple input rows:
selectsum(x)
58
DATABASE MANGAEMENT SYSTEM
fromsimple;
+--------+
| SUM(X) |
|--------|
| 60 |
+--------+
Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null
Count():
Count(*): Returns total number of records .i.e 6.
Count(salary): Return number of Non Null values over the column salary. i.e 5.
Count(Distinct Salary): Return number of distinct Non Null values over the column salary
.i.e 4.
Sum():
sum(salary): Sum all Non Null values of Column salary i.e., 310
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.
Avg():
Avg(salary) = Sum(salary) / count(salary) = 310/5
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 250/4
59
DATABASE MANGAEMENT SYSTEM
Min():
Min(salary): Minimum value in the salary column except NULL i.e., 40.
Max(salary): Maximum value in the salary i.e., 80.
Some aggregate functions can be passed more than one column. For example:
select count(col1, col2) from table1;
In these instances, the aggregate function ignores a row if any individual column is NULL.
insertintot(x,y)values
(1,2),-- No NULLs.
(3,null),-- One but not all columns are NULL.
(null,6),-- One but not all columns are NULL.
(null,null);-- All columns are NULL.
selectcount(x,y)fromt;
+-------------+
| COUNT(X, Y) |
|-------------|
| 1|
+-------------+
Similarly, if SUM is called with an expression that references two or more columns, and if
one or more of those columns is NULL, then the expression evaluates to NULL, and the row
is ignored:
selectsum(x+y)fromt;
+------------+
60
DATABASE MANGAEMENT SYSTEM
| SUM(X + Y) |
|------------|
| 3|
+------------+
SQL also provides a special comparison operator IS NULL to test whether a column value is
null; for example the value of y IS NULL returns true when x is 3 and IS NOT NULL
returns false.
INTRODUCTION TO VIEWS
A view is a table whose rows are not explicitly stored in the database but are computed as
needed.
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database. We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table or specific rows based
on certain condition.
Sample Tables:
StudentDetails
StudentMarks
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created from a single
table or multiple tables.
61
DATABASE MANGAEMENT SYSTEM
Syntax:
CREATE VIEW view_name ASSELECT column1, column2.....
FROM table_nameWHERE condition;
To see the data in the View, we can query the view in the same manner as we query a table.
Creating View from multiple tables: In this example we will create a View named
MarksView from two tables StudentDetails and StudentMarks. To create a View from
multiple tables we can simply include multiple tables in the SELECT statement.
Query:
CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
62
DATABASE MANGAEMENT SYSTEM
DELETING VIEWS
SQL allows us to delete an existing View. We can delete or drop a View using the DROP
statement.
Syntax:
There are certain conditions needed to be satisfied to update a view. If any one of these
conditions is not met, then we will not be allowed to update the view.
1. The SELECT statement which is used to create the view should not include GROUP
BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
63
DATABASE MANGAEMENT SYSTEM
5. The view should be created from a single table. If the view is created using multiple
tables then we will not be allowed to update the view.
We can use the CREATE OR REPLACE VIEW statement to add or remove fields from a
view.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1,coulmn2,..
FROM table_name
WHERE condition;
For example, if we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this as:
64
DATABASE MANGAEMENT SYSTEM
Example:
In the below example we will insert a new row in the View DetailsView which we have
created above in the example of “creating views from a single table”.
INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("Suresh","Gurgaon");
Output:
WHERE condition;
WHERE NAME="Suresh";
65
DATABASE MANGAEMENT SYSTEM
Output:
TRIGGERS
Action: A procedure that is executed when the trigger is activated and its con-dition is true.
A trigger action can examine the answers to the query in the condition part of the trigger,
refer to old and new values of tuples modified by the statement activating the trigger,
execute new queries, and make changes to the database.
Syntax:
create trigger [trigger_name]
[before | after]
on [table_name]
[trigger_body]
66
DATABASE MANGAEMENT SYSTEM
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 and AFTER of Trigger:
BEFORE triggers run the trigger action before the triggering statement is run.
AFTER triggers run the trigger action after the triggering statement is run.
The trigger called init count initializes a counter variable before every execution of an
INSERT statement that adds tuples to the Students relation. The trigger called incr count
increments the counter for each inserted tuple that satisfies the condition age < 18.
DECLARE
count INTEGER;
BEGIN
count := 0;
END
/* Action */
67
DATABASE MANGAEMENT SYSTEM
count := count + 1;
END
(identifying the modified table, Students, and the kind of modifying statement, an
INSERT), and the third field is the number of inserted Students tuples with age < 18. (The
trigger in Figure 5.19 only computes the count; an additional trigger is required to insert the
appropriate tuple into the statistics table.)
INSERT /* Action */
68
DATABASE MANGAEMENT SYSTEM
UNIT IV
DEPENDENCIES AND NORMAL FORMS
Importance of a good schema design
A database schema is a blueprint that represents the tables and relations of a data set. Good
database schema design is essential to making your data tractable so that you can make sense
of it and build the dashboards, reports, and data models that you need.
69
DATABASE MANGAEMENT SYSTEM
tables into smaller tables and make a relationship between them. It can remove the redundant
data and ease to add, manipulate or delete table fields.
A normalization defines rules for the relational table as to whether it satisfies the normal
form. A normal form is a process that evaluates each relation against defined criteria and
removes the multi valued, joins, functional and trivial dependency from a relation. If any data
is updated, deleted or inserted, it does not cause any problem for database tables and help to
improve the relational table' integrity and efficiency.
Objective of Normalization
1. It is used to remove the duplicate data and database anomalies from the relational
table.
2. Normalization helps to reduce redundancy and complexity by examining new data
types used in the table.
3. It is helpful to divide the large database table into smaller tables and link them using
relationship.
4. It avoids duplicate data or no repeating groups into a table.
5. It reduces the chances for anomalies to occur in a database.
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It typically
exists between the primary key and non-key attribute within a table.
For any relation R, attribute Y is functionally dependent on attribute X (usually the PK), if for
every valid instance of X, that value of X uniquely determines the value of Y. This
relationship is indicated by the representation below :
X→Y
The left side of FD is known as a determinant, the right side of the production is known as a
dependent.
For example:
70
DATABASE MANGAEMENT SYSTEM
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee table
because if we know the Emp_Id, we can tell that employee name associated with it.
Emp_Id → Emp_Name
For example in the below table A → B is true, but B → A is not true as there are different
values of A for B = 3.
A B
------
1 3
2 3
4 0
1 3
4 0
Examples
o ABC → AB
o ABC → A
o ABC → ABC
71
DATABASE MANGAEMENT SYSTEM
o Name → DOB
o AB → BC,
o AD → DC
If F is a set of functional dependencies then the closure of F, denoted as F+, is the set of all
functional dependencies logically implied by F. Armstrong's Axioms are a set of rules, that
when applied repeatedly, generates a closure of functional dependencies.
1. Axiom of reflexivity –
If X is a set of attributes and Y is subset of X, then X holds Y.
If X ⊇ Y then X → Y
Example:
X = {a, b, c, d, e}
Y = {a, b, c}
This property is trivial property.
2. Axiom of augmentation –
The augmentation is also called as a partial dependency. In augmentation, if X determines
Y, then XZ determines YZ for any Z.
If X → Y then XZ → YZ
72
DATABASE MANGAEMENT SYSTEM
Example:
For R(ABCD), if A → B then AC → BC
3. Axiom of transitivity –
In the transitive rule, if X determines Y and Y determine Z, then X must also determine
Z.
If X → Y and Y → Z then X → Z
Secondary Rules –
1. Union–
Union rule says, if X determines Y and X determines Z, then X must also determine Y
and Z.
If X → Y and X → Z then X → YZ
2. Decomposition–
Decomposition rule is also known as project rule. It is the reverse of union rule.This Rule
says, if X determines Y and Z, then X determines Y and X determines Z separately.
If X → YZ then X → Y and X → Z
3. Pseudo Transitivity –
In Pseudo transitive Rule, if X determines Y and YZ determines W, then XZ determines
W.
If X → Y and YZ → W then XZ → W
Minimal Covers:
The formal definition is: A set of FD F to be minimal if it satisfies the following conditions −
73
DATABASE MANGAEMENT SYSTEM
• We cannot remove any dependency from F and still have a set of dependencies that
are equivalent to F.
Canonical cover is called minimal cover which is called the minimum set of FDs. A set of FD
FC is called canonical cover of F if each FD in FC is a −
• Simple FD.
• Non-redundant FD.
Example
A -> BC
B -> C
A -> B
AB -> C
• Minimal cover: The minimal cover is the set of FDs which are equivalent to the given
FDs.
• Canonical cover: In canonical cover, the LHS (Left Hand Side) must be unique.
First of all, we will find the minimal cover and then the canonical cover.
74
DATABASE MANGAEMENT SYSTEM
A -> B
A -> C
B -> C
A -> B
AB -> C
A+ = {A, B, C}
A -> B
A -> C
B -> C
A -> B
A -> C
A -> B
A -> C
Now, we will convert the above set of FDs into canonical cover.
The canonical cover for the above set of FDs will be as follows −
A -> BC
B -> C
NORMAL FORMS
Given a relation schema, we need to decide whether it is a good design or whether we need to
decompose it into smaller relations. Such a decision must be guided by an understanding of
75
DATABASE MANGAEMENT SYSTEM
what problems, if any, arise from the current schema. To provide such guidance, several
normal forms have been proposed. If a relation schema is in one of these normal forms, we
know that certain kinds of problems cannot arise.
The normal forms based on FDs:
First Normal Form is defined in the definition of relations (tables) itself. This rule defines that
all the attributes in a relation must have atomic domains. The values in an atomic domain are
indivisible units.
In the first normal form, only single values are permitted at the intersection of each row and
column; hence, there are no repeating groups.
To normalize a relation that contains a repeating group, remove the repeating group and form
two new relations.
Each attribute must contain only a single value from its pre-defined domain.
Before we learn about the second normal form, we need to understand the following −
76
DATABASE MANGAEMENT SYSTEM
• For the second normal form, the relation must first be in 1NF. The relation is
automatically in 2NF if, and only if, the Prime Key comprises a single attribute.
• If the relation has a composite Prime Key, then each non-key attribute must be fully
dependent on the entire PK and not on a subset of the PK.
• A relation is in 2NF if it has No Partial Dependency.
• Partial Dependency – If the proper subset of candidate key determines non-prime
attribute, it is called partial dependency.
We see here in Student_Project relation that the prime key attributes are Stu_ID and Proj_ID.
According to the rule, non-key attributes, i.e. Stu_Name and Proj_Name must be dependent
upon both and not on any of the prime key attribute individually. But we find that Stu_Name
can be identified by Stu_ID and Proj_Name can be identified by Proj_ID independently. This
is called partial dependency, which is not allowed in Second Normal Form.
We broke the relation in two as depicted in the above picture. So there exists no partial
dependency.
To be in third normal form, the relation must be in second normal form. Also
- all transitive dependencies must be removed; a non-key attribute may not be functionally
dependent on another non-key attribute.
77
DATABASE MANGAEMENT SYSTEM
We find that in the above Student_detail relation, Stu_ID is the key and only prime key
attribute. We find that City can be identified by Stu_ID as well as Zip itself. Neither Zip is a
superkey nor is City a prime attribute. Additionally, Stu_ID → Zip → City, so there
exists transitive dependency.
To bring this relation into third normal form, we break the relation into two relations as
follows −
In the above example, Stu_ID is the super-key in the relation Student_Detail and Zip is the
super-key in the relation ZipCodes. So,
and
Zip → City
78
DATABASE MANGAEMENT SYSTEM
DECOMPOSITIONS
A relation in BCNF is free of redundancy and a relation schema in 3NF comes close.
If a relation schema is not in one of these normal forms, the FDs that cause a violation can
give us insight into the potential problems..
A decomposition of a relation schema R consists of replacing the relation schema by
two (or more) relation schemas that each contain a subset of the attributes of R and together
include all attributes in R.
When a relation in the relational model is not appropriate normal form then the
decomposition of a relation is required. In a database, breaking down the table into multiple
tables termed as decomposition.
1. Attribute Preservation:
Using functional dependencies the algorithms decompose the universal relation schema
R in a set of relation schemas D = { R1, R2, ….. Rn } relational database schema,
where ‘D’ is called the Decomposition of R.
The attributes in R will appear in at least one relation schema Ri in the decomposition,
i.e., no attribute is lost. This is called the Attribute Preservation condition of
decomposition.
2. Dependency Preservation:
If each functional dependency X->Y specified in F appears directly in one of the
relation schemas Ri in the decomposition D or could be inferred from the dependencies
that appear in some Ri. This is the Dependency Preservation.
If a relation R is decomposed into relation R1 and R2, then the dependencies of R either
must be a part of R1 or R2 or must be derivable from the combination of functional
dependencies of R1 and R2.
For example, suppose there is a relation R (A, B, C, D) with functional dependency set
(A->BC). The relational R is decomposed into R1(ABC) and R2(AD) which is dependency
preserving because FD A->BC is a part of relation R1(ABC).
79
DATABASE MANGAEMENT SYSTEM
3.Lossless Join:
Lossless join property is a feature of decomposition supported by normalization. It is
the ability to ensure that any instance of the original relation can be identified from
corresponding instances in the smaller relations.
For example:
R : relation, F : set of functional dependencies on R,
X, Y : decomposition of R,
A decomposition {R1, R2, …, Rn} of a relation R is called a lossless decomposition for R
if the natural join of R1, R2, …, Rn produces exactly the relation R.
80
DATABASE MANGAEMENT SYSTEM
UNIT-V
TRANSACTION MANAGEMENT
What is a Transaction?
A transaction is an event which occurs on the database. Generally a transaction reads a value
from the database or writes a value to the database. If you have any concept of Operating
Systems, then we can say that a transaction is analogous to processes. Although a transaction
can both read and write on the database, there are some fundamental differences between
these two classes of operations. A read operation does not change the image of the database
in any way. But a write operation, whether performed with the intention of inserting,
updating or deleting data from the database, changes the image of the database. That is, we
may say that these transactions bring the database from an image which existed before the
transaction occurred (called the Before Image or BFIM) to an image which exists after the
transaction occurred (called the After Image or AFIM).
Atomicity: This means that either all of the instructions within the transaction will be
reflected in the database, or none of them will be reflected.
Say for example, we have two accounts A and B, each containing Rs 1000/-. We now start a
transaction to deposit Rs 100/- from account A to Account B.
Read A;
A = A – 100;
Write A;
Read B;
B = B + 100;
Write B;
81
DATABASE MANGAEMENT SYSTEM
Fine, is not it? The transaction has 6 instructions to extract the amount from A and submit it
to B.The AFIM will show Rs 900/- in A and Rs 1100/- in B.
Now, suppose there is a power failure just after instruction 3 (Write A) has been complete.
What happens now? After the system recovers the AFIM will show Rs 900/- in A, but the
same Rs 1000/- in B. It would be said that Rs 100/- evaporated in thin air for the power
failure. Clearly such a situation is not acceptable
The solution is to keep every value calculated by the instruction of the transaction not in any
stable storage (hard disc) but in a volatile storage (RAM), until the transaction completes its
last instruction. When we see that there has not been any error we do something known as a
COMMIT operation. Its job is to write every temporarily calculated value from the volatile
storage on to the stable storage. In this way, even if power fails at instruction 3, the post
recovery image of the database will show accounts A and B both containing Rs 1000/-, as if
the failed transaction had never occurred.
To give better performance, every database management system supports the execution of
multiple transactions at the same time, using CPU Time Sharing. Concurrently executing
transactions may have to deal with the problem of sharable resources, i.e. resources that
multiple transactions are trying to read/write at the same time. For example, we may have a
table or a record on which tw transaction are trying to read or write at the same time. Careful
mechanisms are created in order to prevent mismanagement of these sharable resources, so
that there should not be any change in the way a transaction performs. A transaction which
deposits Rs 100/- to account A must deposit the same amount whether it is acting alone or in
conjunction with another transaction that may be trying to deposit or withdraw some amount
at the same time.
82
DATABASE MANGAEMENT SYSTEM
Isolation: In case multiple transactions are executing concurrently and trying to access a
sharable resource at the same time, the system should create an ordering in their execution so
that they should not create any anomaly in the value stored at the sharable resource.
There are several ways to achieve this and the most popular one is using some kind of
locking mechanism. Again, if you have the concept of Operating Systems, then you should
remember the semaphores, how it is used by a process to make a resource busy before
starting to use it, and how it is used to release the resource after the usage is over. Other
processes intending to access that same resource must wait during this time. Locking is
almost similar. It states that a transaction must first lock the data item that it wishes to access,
and release the lock when the accessing is no longer required. Once a transaction locks the
data item, other transactions wishing to access the same data item must wait until the lock is
released.
Durability: It states that once a transaction has been complete the changes it has made
should be permanent.
As we have seen in the explanation of the Atomicity property, the transaction, if completes
successfully, is committed. Once the COMMIT is done, the changes which the transaction
has made to the database are immediately written into permanent storage. So, after the
transaction has been committed successfully, there is no question of any loss of information
even if the power fails. Committing a transaction guarantees that the AFIM has been reached.
There are several ways Atomicity and Durability can be implemented. One of them is called
Shadow Copy. In this scheme a database pointer is used to point to the BFIM of the
database. During the transaction, all the temporary changes are recorded into a Shadow Copy,
which is an exact copy of the original database plus the changes made by the transaction,
which is the AFIM. Now, if the transaction is required to COMMIT, then the database pointer
is updated to point to the AFIM copy, and the BFIM copy is discarded. On the other hand, if
the transaction is not committed, then the database pointer is not updated. It keeps pointing to
the BFIM, and the AFIM is discarded. This is a simple scheme, but takes a lot of memory
space and time to implement.
83
DATABASE MANGAEMENT SYSTEM
If you study carefully, you can understand that Atomicity and Durability is essentially the
same thing, just as Consistency and Isolation is essentially the same thing.
Transaction States
There are the following six states in which a transaction may exist:
• Active: The initial state when the transaction has just started execution.
• Partially Committed: At any given point of time if the transaction is executing
properly, then it is going towards it COMMIT POINT. The values generated during
the execution are all stored in volatile storage.
• Failed: If the transaction fails for some reason. The temporary values are no longer
required, and the transaction is set to ROLLBACK. It means that any change made to
the database by this transaction up to the point of the failure must be undone. If the
failed transaction has withdrawn Rs. 100/- from account A, then the ROLLBACK
operation should add Rs 100/- to account A.
• Aborted: When the ROLLBACK operation is over, the database reaches the BFIM.
The transaction is now said to have been aborted.
• Committed: If no failure occurs then the transaction reaches the COMMIT POINT.
All the temporary values are written to the stable storage and the transaction is said to
have been committed.
• Terminated: Either committed or aborted
84
DATABASE MANGAEMENT SYSTEM
Concurrent Execution
A schedule is a collection of many transactions which is implemented as a unit. Depending
upon how these transactions are arranged in within a schedule, a schedule can be of two
types:
• Serial: The transactions are executed one after another, in a non-preemptive manner.
• Concurrent: The transactions are executed in a preemptive, time shared method.
In Serial schedule, there is no question of sharing a single data item among many
transactions, because not more than a single transaction is executing at any point of time.
However, a serial schedule is inefficient in the sense that the transactions suffer for having a
longer waiting time and response time, as well as low amount of resource utilization.
In concurrent schedule, CPU time is shared among two or more transactions in order to run
them concurrently. However, this creates the possibility that more than one transaction may
need to access a single data item for read/write purpose and the database could contain
inconsistent value if such accesses are not handled properly. Let us explain with the help of
an example.
Let us consider there are two transactions T1 and T2, whose instruction sets are given as
following. T1 is the same as we have seen earlier, while T2 is a new transaction.
T1
Read A;
A = A – 100;
Write A;
Read B;
B = B + 100;
Write B;
T2
Read A;
Temp = A * 0.1;
Read C;
C = C + Temp;
Write C;
T2 is a new transaction which deposits to account C 10% of the amount in account A.
85
DATABASE MANGAEMENT SYSTEM
If we prepare a serial schedule, then either T1 will completely finish before T2 can begin, or
T2 will completely finish before T1 can begin. However, if we want to create a concurrent
schedule, then some Context Switching need to be made, so that some portion of T1 will be
executed, then some portion of T2 will be executed and so on. For example say we have
prepared the following concurrent schedule.
No problem here. We have made some Context Switching in this Schedule, the first one after
executing the third instruction of T1, and after executing the last statement of T2. T1 first
deducts Rs 100/- from A and writes the new value of Rs 900/- into A. T2 reads the value of
A, calculates the value of Temp to be Rs 90/- and adds the value to C. The remaining part of
T1 is executed and Rs 100/- is added to B.
Serializability
When several concurrent transactions are trying to access the same data item, the instructions
within these concurrent transactions must be ordered in some way so as there are no problem
in accessing and releasing the shared data item. There are two aspects of serializability which
are described here:
86
DATABASE MANGAEMENT SYSTEM
Conflict Serializability
Two instructions of two different transactions may want to access the same data item in order
to perform a read/write operation. Conflict Serializability deals with detecting whether the
instructions are conflicting in any way, and specifying the order in which these two
instructions will be executed in case there is any conflict. A conflict arises if at least one (or
both) of the instructions is a write operation. The following rules are important in Conflict
Serializability:
1. If two instructions of the two concurrent transactions are both for read operation, then
they are not in conflict, and can be allowed to take place in any order.
2. If one of the instructions wants to perform a read operation and the other instruction wants
to perform a write operation, then they are in conflict, hence their ordering is important. If the
read instruction is performed first, then it reads the old value of the data item and after the
reading is over, the new value of the data item is written. It the write instruction is performed
first, then updates the data item with the new value and the read instruction reads the newly
updated value.
3. If both the transactions are for write operation, then they are in conflict but can be allowed
to take place in any order, because the transaction do not read the value updated by each
other. However, the value that persists in the data item after the schedule is over is the one
written by the instruction that performed the last write.
View Serializability:
This is another type of serializability that can be derived by creating another schedule out of
an existing schedule, involving the same set of transactions. These two schedules would be
called View Serializable if the following rules are followed while creating the second
schedule out of the first. Let us consider that the transactions T1 and T2 are being serialized
to create two different schedules S1 and S2 which we want to be View Equivalent and both
T1 and T2 wants to access the same data item.
1. If in S1, T1 reads the initial value of the data item, then in S2 also, T1 should read the
initial value of that same data item.
87
DATABASE MANGAEMENT SYSTEM
2. If in S1, T1 writes a value in the data item which is read by T2, then in S2 also, T1 should
write the value in the data item before T2 reads it.
3. If in S1, T1 performs the final write operation on that data item, then in S2 also, T1 should
perform the final write operation on that data item. Let us consider a schedule S in which
there are two consecutive instructions, I and J , of transactions Ti and Tj , respectively (i _=
j). If I and J refer to different data items, then we can swap I and J without affecting the
results of any instruction
in the schedule. However, if I and J refer to the same data item Q, then the order of the two
steps may matter. Since we are dealing with only read and write instructions, there are four
cases that we need to consider:
I = read(Q), J = read(Q). The order of I and J does not matter, since the same value
of Q is read by Ti and Tj , regardless of the order.
I = read(Q), J = write(Q). If I comes before J , then Ti does not read the value of Q that is
written by Tj in instruction J . If J comes before I, then Ti reads the value of Q that is written
by Tj. Thus, the order of I and J matters.
I = write(Q), J = read(Q). The order of I and J matters for reasons similar to those of the
previous case.
4. I = write(Q), J = write(Q). Since both instructions are write operations, the order of these
instructions does not affect either Ti or Tj . However, the value obtained by the next read(Q)
instruction of S is affected, since the result of only the latter of the two write instructions is
preserved in the database. If there is no other write(Q) instruction after I and J in S, then the
order of I and J directly affects the final value of Q in the database state that results from
schedule S.
88
DATABASE MANGAEMENT SYSTEM
We say that I and J conflict if they are operations by different transactions on the same data
item, and at least one of these instructions is a write operation. To illustrate the concept of
conflicting instructions, we consider schedule 3in Figure above. The write(A) instruction of
T1 conflicts with the read(A) instruction of T2. However, the write(A) instruction of T2 does
not conflict with the read(B) instruction of T1, because the two instructions access different
data items.
Transaction Characteristics
Every transaction has three characteristics: access mode, diagnostics size, and isolation level.
The diagnostics size determines the number of error conditions that can be recorded.
If the access mode is READ ONLY, the transaction is not allowed to modify the database.
Thus, INSERT, DELETE, UPDATE, and CREATE commands cannot be executed. If we
have to execute one of these commands, the access mode should be set to READ WRITE. F
transactions with READ ONLY access mode, only shared locks need to be obtained, thereby
increasing concurrency.
The isolation level controls the extent to which a given transaction is exposed to the actions
of other transactions executing concurrently. By choosing one of four possible isolation level
settings, a user can obtain greater concurrency at the cost of increasing the transaction's
exposure to other transactions' uncommitted changes.
89
DATABASE MANGAEMENT SYSTEM
defined to be the possibility that a transaction retrieves a collection of objects (in SQL terms,
a collection of tuples) twice and sees different results, even though it does not modify any of
these tuples itself.
READ COMMITTED ensures that T reads only the changes made by committed
transactions, and that no value written by T is changed by any other transaction until T is
complete. However, a value read by T may well be modified by another transaction while T is
still in progress, and T is, of course, exposed to the phantom problem.
A READ COMMITTED transaction obtains exclusive locks before writing objects and
holds these locks until the end. It also obtains shared locks before reading objects, but these
locks are released immediately; their only effect is to guarantee that the transaction that last
modified the object is complete. (This guarantee relies on the fact that every SQL transaction
obtains exclusive locks before writing objects and holds exclusive locks until the end.)
A READ UNCOMMITTED transaction does not obtain shared locks before reading objects.
This mode represents the greatest exposure to uncommitted changes of other transactions; so
much so that SQL prohibits such a transaction from making any changes itself - a READ
UNCOMMITTED transaction is required to have an access mode of READ ONLY. Since
such a transaction obtains no locks for reading objects, and it is not allowed to write objects
(and therefore never requests exclusive locks), it never makes any lock requests
90
DATABASE MANGAEMENT SYSTEM
The SERIALIZABLE isolation level is generally the safest and is recommended for most
transactions. Some transactions, however, can run with a lower isolation level, and the
smaller number of locks requested can contribute to improved system performance.
For example, a statistical query that finds the average sailor age can be run at the READ
COMMITTED level, or even the READ UNCOMMITTED level, because a few incorrect or
missing values will not significantly affect the result if the number of sailors is large. The
isolation level and access mode can be set using the SET TRANSACTION command. For
example, the following command declares the current transaction to be SERIALIZABLE and
READ ONLY:
91
DATABASE MANGAEMENT SYSTEM
A precedence graph of the schedule D, with 3 transactions. As there is a cycle (of length 2;
with two edges) through the committed transactions T1 and T2, this schedule (history) is not
Conflict serializable.
92
DATABASE MANGAEMENT SYSTEM
RECOVERABLE SCHEDULES
93
DATABASE MANGAEMENT SYSTEM
• If T8 should abort, T9 would have read (and possibly shown to the user) an
inconsistent database state. Hence, database must ensure that schedules are
recoverable.
CASCADING ROLLBACKS
• Cascading rollback – a single transaction failure leads to a series of transaction
rollbacks. Consider the following schedule where none of the transactions has yet
committed (so the schedule is recoverable) If T10 fails, T11 and T12 must also be
rolled back.
• Can lead to the undoing of a significant amount of work
CASCADELESS SCHEDULES
• Cascade less schedules — for each pair of transactions Ti and Tj such that Tj reads a
data item previously written by Ti, the commit operation of Ti appears before the read
operation of Tj.
• Every cascade less schedule is also recoverable
• It is desirable to restrict the schedules to those that are cascade less.
• Example of a schedule that is NOT cascade less
94
DATABASE MANGAEMENT SYSTEM
CONCURRENCY SCHEDULE
A database must provide a mechanism that will ensure that all possible schedules are both:
• Conflict serializable.
• Recoverable and preferably cascade less
• A policy in which only one transaction can execute at a time generates serial
schedules, but provides a poor degree of concurrency.
• Concurrency-control schemes tradeoff between the amount of concurrency they allow
and the amount of overhead that they incur
• Testing a schedule for serializability after it has executed is a little too late!
• Tests for serializability help us understand why a concurrency control protocol is
correct
• Goal – to develop concurrency control protocols that will assure serializability.
95
DATABASE MANGAEMENT SYSTEM
RECOVERY SYSTEM
Failure Classification:
• Transaction failure :
• Logical errors: transaction cannot complete due to some internal error condition
• System errors: the database system must terminate an active transaction due to an
error condition (e.g., deadlock)
96
DATABASE MANGAEMENT SYSTEM
• System crash: a power failure or other hardware or software failure causes the system
to crash.
• Fail-stop assumption: non-volatile storage contents are assumed to not be corrupted as
result of a system crash
• Database systems have numerous integrity checks to prevent corruption of disk data
• Disk failure: a head crash or similar disk failure destroys all or part of disk storage
• Destruction is assumed to be detectable: disk drives use checksums to detect failures
RECOVERY ALGORITHMS
• Consider transaction Ti that transfers $50 from account A to account B
• Two updates: subtract 50 from A and add 50 to B Transaction Ti requires updates to
A and B to be output to the database.
• A failure may occur after one of these modifications have been made but before both
of them are made.
• Modifying the database without ensuring that the transaction will commit may leave
the database in an inconsistent state
• Not modifying the database may result in lost updates if failure occurs just after
transaction commits
• Recovery algorithms have two parts
•
1. Actions taken during normal transaction processing to ensure enough information
exists to recover from failures
2. Actions taken after a failure to recover the database contents to a state that ensures
atomicity, consistency and durability
STORAGE STRUCTURE
• Volatile storage:
• Does not survive system crashes
• Examples: main memory, cache memory
• Nonvolatile storage:
• Survives system crashes
• Examples: disk, tape, flash memory,
• Non-Volatile (battery backed up) RAM
97
DATABASE MANGAEMENT SYSTEM
Stable-Storage Implementation
• Maintain multiple copies of each block on separate disks
• copies can be at remote sites to protect against disasters such as fire or flooding.
• Failure during data transfer can still result in inconsistent copies.
• Block transfer can result in
• Successful completion
• Partial failure: destination block has incorrect information
• Total failure: destination block was never updated
• Protecting storage media from failure during data transfer (one solution):
• Execute output operation as follows (assuming two copies of each block):
If either copy of an inconsistent block is detected to have an error (bad checksum), overwrite
it by the other copy. If both have no error, but are different, overwrite the second block by the
first block.
98
DATABASE MANGAEMENT SYSTEM
DATA ACCESS
• Physical blocks are those blocks residing on the disk.
• System buffer blocks are the blocks residing temporarily in main memory.
• Block movements between disk and main memory are initiated through the following
two operations:
• input(B) transfers the physical block B to main memory.
• output(B) transfers the buffer block B to the disk, and replaces the appropriate
physical block there.
• We assume, for simplicity, that each data item fits in, and is stored inside, a single
block.
• Each transaction Ti has its private work-area in which local copies of all data items
accessed and updated by it are kept.
• Ti's local copy of a data item X is denoted by xi.
• BX denotes block containing X
• Transferring data items between system buffer blocks and its private work-area done
by:
• read(X) assigns the value of data item X to the local variable xi.
• write(X) assigns the value of local variable xi to data item {X} in the buffer block.
• Transactions
• Must perform read(X) before accessing X for the first time (subsequent reads can be
from local copy)
• The write(X) can be executed at any time before the transaction commits
• Note that output(BX) need not immediately follow write(X). System can perform the
output operation when it seems fit.
Lock-Based Protocols
A lock is a mechanism to control concurrent access to a data item
Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as well as written. X-lock is requested
using lock-X instruction.
2. shared (S) mode. Data item can only be read. S-lock is requested using lock-S instruction.
Lock requests are made to concurrency-control manager. Transaction can proceed only after
request is granted.
99
DATABASE MANGAEMENT SYSTEM
Lock-compatibility matrix
1) A transaction may be granted a lock on an item if the requested lock is compatible with
locks already held on the item by other transactions
2) Any number of transactions can hold shared locks on an item, but if any transaction holds
an exclusive on the item no other transaction may hold any lock on the item.
3) If a lock cannot be granted, the requesting transaction is made to wait till all incompatible
locks held by other transactions have been released. The lock is then granted.
100
DATABASE MANGAEMENT SYSTEM
Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to
release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its
lock on A. Such a situation is called a deadlock.
l .To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
2. The potential for deadlock exists in most locking protocols. Deadlocks are a necessary evil.
3.Starvation is also possible if concurrency control manager is badly designed. For
example:
• A transaction may be waiting for an X-lock on an item, while a sequence of other
transactions request and are granted an S-lock on the same item.
• The same transaction is repeatedly rolled back due to deadlocks.
4.Concurrency control manager can be designed to prevent starvation.
101
DATABASE MANGAEMENT SYSTEM
locking is needed for conflict serializability in the following sense: Given a transaction Ti that
does not follow two-phase locking, we can find a transaction Tj that uses two-phase locking,
and a schedule for Ti and Tj that is not conflict serializable.
TIMESTAMP-BASED PROTOCOLS
1.Each transaction is issued a timestamp when it enters the system. If an old transaction Ti has
time-stamp TS(Ti), a new transaction Tj is assigned time-stamp TS(Tj) such that TS(Ti)
<TS(Tj).
2.The protocol manages concurrent execution such that the time-stamps determine the
serializability order.
3.In order to assure such behavior, the protocol maintains for each data Q two timestamp
values:
• W-timestamp(Q) is the largest time-stamp of any transaction that executed write(Q)
successfully.
• R-timestamp(Q) is the largest time-stamp of any transaction that executed read(Q)
successfully.
4.The timestamp ordering protocol ensures that any conflicting read and write operations are
executed in timestamp order.
5.Suppose a transaction Ti issues a read(Q)
• If TS(Ti) W-timestamp(Q), then Ti needs to read a value of Q that was already
overwritten. n Hence, the read operation is rejected, and Ti is rolled back.
• If TS(Ti) W-timestamp(Q), then the read operation is executed, and Rtimestamp(
Q) is set to max(R-timestamp(Q), TS(Ti)).
6.Suppose that transaction Ti issues write(Q).
• If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is producing was needed
previously, and the system assumed that that value would never be produced. n
Hence, the write operation is rejected, and Ti is rolled back.
• If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of Q. n
Hence, this write operation is rejected, and Ti is rolled back.
• Otherwise, the write operation is executed, and W-timestamp(Q) is set to TS(Ti).
1.We now present a modification to the timestamp-ordering protocol that allows greater
potential concurrency than does the protocol i.e., Timestamp ordering Protocol . Let us
102
DATABASE MANGAEMENT SYSTEM
consider schedule 4 of Figure below, and apply the timestamp-ordering protocol. Since T27
starts before T28, we shall assume that TS(T27) < TS(T28). The read(Q) operation of T2
succeeds, as does the write(Q) operation of T28. When T27 attempts its write(Q) operation,
we find that TS(T27) < W-timestamp(Q), since Wtimestamp(Q) = TS(T28). Thus, the
write(Q) by T27 is rejected and transaction T27 must be rolled back.
2.Although the rollback of T27 is required by the timestamp-ordering protocol, it is
unnecessary. Since T28 has already written Q, the value that T27 is attempting to write is one
that will never need to be read. Any transaction Ti with TS(Ti ) < TS(T28) that attempts a
read(Q)will be rolled back, since TS(Ti)<W-timestamp(Q).
3.Any transaction Tj with TS(Tj ) > TS(T28) must read the value of Q written by T28, rather
than the value that T27 is attempting to write. This observation leads to a modified version, of
the timestamp-ordering protocol in which obsolete write operations can be ignored under
certain circumstances. The protocol rules for read operations remain unchanged. The protocol
rules for write operations, however, are slightly different from the time stamp order in
protocol.
The modification to the timestamp-ordering protocol, called Thomas’ write rule, is this:
Suppose that transaction Ti issues write(Q).
1. If TS(Ti ) < R-timestamp(Q), then the value of Q that Ti is producing was previously
needed, and it had been assumed that the value would never be produced. Hence, the system
rejects the write operation and rolls Ti back.
2. If TS(Ti ) < W-timestamp(Q), then Ti is attempting to write an obsolete value of Q. Hence,
this write operation can be ignored.
3. Otherwise, the system executes the write operation and setsW-timestamp(Q) to TS(Ti ).
VALIDATION-BASED PROTOCOLS
1) Phases in Validation-Based Protocols of the various data items and stores them in variables
local to Ti. It performs all write operations on temporary local variables, without updates of
the actual database.
103
DATABASE MANGAEMENT SYSTEM
2) Validation phase. The validation test is applied to transaction Ti. This determines whether
Ti is allowed to proceed to the write phase without causing a violation of serializability. If a
transaction fails the validation test, the system aborts the transaction.
3) Write phase. If the validation test succeeds for transaction Ti, the temporary local variables
that hold the results of any write operations performed by Ti are copied to the database.
Read-only transactions omit this phase.
104