0% found this document useful (0 votes)
13 views

Database 3,4

The document provides an overview of SQL (Structured Query Language), its data types, constraints, and commands used in relational database management systems. It details various SQL commands categorized into DDL, DML, DCL, TCL, and DQL, along with examples for each command type. Additionally, it explains SQL operators, relational algebra, and the join operations used in querying databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Database 3,4

The document provides an overview of SQL (Structured Query Language), its data types, constraints, and commands used in relational database management systems. It details various SQL commands categorized into DDL, DML, DCL, TCL, and DQL, along with examples for each command type. Additionally, it explains SQL operators, relational algebra, and the join operations used in querying databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CORE-6: Database Systems - Unit-3

SQL: SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS). It is a standard
language for Relational Database System. It enables a user to create, read, update
and delete relational databases and tables. All the RDBMS like MySQL, Oracle,
SQL Server etc. use SQL as their standard database language.

SQL follows the following rules:

o Structure query language is not case sensitive. Generally, keywords of SQL


are written in uppercase.
o We can use a single SQL statement on one or multiple text line.
o SQL is based on tupple relational calculus and relational algebra.

SQL Data Types: Data types are used to represent the nature of the data that can
be stored in the database table. For example, in a particular column of a table, if we
want to store a string type of data then we will have to declare a string data type of
that column. Data types is mainly classified into three categories for every database.

o String Data types


o Numeric Data types
o Date and time Data types

MySQL Data Types : A list of data types used in MySQL database. This is based
on MySQL 8.0.

MySQL String Data Types


CHAR(Size) It is used to specify a fixed length string that can contain
numbers, letters, and special characters. Its size can be 0 to
255 characters. Default is 1.
VARCHAR(Size) It is used to specify a variable length string that can contain
numbers, letters, and special characters. Its size can be from 0
to 65535 characters.
TEXT(Size) It holds a string that can contain a maximum length of 255
characters.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 1 of 24


MySQL Numeric Data Types
BIT(Size) It is used for a bit-value type. The number of bits per value is
specified in size. Its size can be 1 to 64. The default value is 1.
INT(size) The size parameter specifies the max display width that is 255.
INTEGER(size) It is equal to INT(size).
FLOAT(size, d) It is used to specify a floating point number. Its size parameter
specifies the total number of digits. The number of digits
after the decimal point is specified by d parameter.
FLOAT(p) It is used to specify a floating point number. MySQL uses p
parameter to determine whether to use FLOAT or DOUBLE. If p
is between 0 to24, the data type becomes FLOAT (). If p is from
25 to 53, the data type becomes DOUBLE().
DOUBLE(size, d) It is a normal size floating point number. Its size parameter
specifies the total number of digits. The number of digits after
the decimal is specified by d parameter.
BOOL It is used to specify Boolean values true and false. Zero is
considered as false, and nonzero values are considered as
true.

MySQL Date and Time Data Types


DATE It is used to specify date format YYYY-MM-DD.
DATETIME(fsp) It is used to specify date and time combination. Its format is
YYYY-MM-DD hh:mm:ss.
TIME(fsp) It is used to specify the time format. Its format is hh:mm:ss.
YEAR It is used to specify a year in four-digit format.

SQL Constraints: SQL constraints are used to specify rules for the data in a
table. Constraints are used to prevent invalid data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any violation
between the constraint and the data action, the action is aborted. Constraints can be
column level or table level. Column level constraints apply to a column, and table
level constraints apply to the whole table. The following are the various types of
integrity constraints used in SQL:
1. Domain Integrity constraints – maintains value according to specifications
a. NOT NULL - Ensures that a column cannot have a NULL value
b. CHECK - Ensures that all values in a column satisfies a specific
condition
c. DEFAULT - Sets a default value for a column when no value is specified

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 2 of 24


2. Entity Integrity constraints – maintains uniqueness of records
a. UNIQUE - Ensures that all values in a column are different
b. PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table

3. Referential Integrity constraints – maintains relationship between two tables


a. FOREIGN KEY - Uniquely identifies a row/record in another table

SQL Commands
o SQL commands are instructions used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users etc.

Types of SQL Commands: There are five types of SQL commands: DDL, DML,
DCL, TCL, and DQL.

1. Data Definition Language (DDL)


o DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently save
all the changes in the database.
o The commands that come under DDL are CREATE, ALTER, DROP and
TRUNCATE

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 3 of 24


a. CREATE: It is used to create a new table in the database.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example: CREATE TABLE STUD(Name VARCHAR2(20), DOB DATE);

b. DROP: It is used to delete both the structure and record stored in the table.
Syntax: DROP TABLE <table_name>;
Example: DROP TABLE STUD;

c. ALTER: It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to add a new
attribute.
Syntax To add a new column in the table:
ALTER TABLE table_name ADD (column_name column_definition);
Syntax To modify existing column in the table:
ALTER TABLE table_name MODIFY (column_name column_definition);
EXAMPLE
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));

d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE EMPLOYEE;

2. Data Manipulation Language


o DML commands are used to modify the database. It is responsible for all form
of changes in the database.
o The command of DML is not auto-committed that means it can't permanently
save all the changes in the database. They can be rollback.
o The commands that come under DML are INSERT, UPDATE, and DELETE

a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
Syntax: INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Example:
INSERT INTO BOOK (Author, Subject) VALUES ("Korth", "DBMS");

b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
UPDATE table_name SET [column_name1=value1,...column_nameN=valueN]
[WHERE CONDITION]
Example: UPDATE students SET Name = 'Sonoo' WHERE Student_Id = '3'

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 4 of 24


c. DELETE: It is used to remove one or more row from a table.
Syntax: DELETE FROM table_name [WHERE condition];
Example: DELETE FROM BOOK WHERE Author="Korth";

3. Data Control Language: DCL commands are used to grant and take back
authority from any database user. The commands that come under DCL are Grant
and Revoke.

a. Grant: It is used to give user access privileges to a database.


Example: GRANT SELECT, UPDATE ON MY_TABLE TO USER1, USER2;

b. Revoke: It is used to take back permissions from the user.


Example: REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

4. Transaction Control Language: TCL commands can only use with DML
commands like INSERT, DELETE and UPDATE only. They cannot be used with
DDL commands as DDL commands are auto-committed. The commands that come
under TCL are COMMIT, ROLLBACK and SAVEPOINT.
a. Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example: DELETE FROM CUSTOMERS WHERE AGE = 25; COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax: ROLLBACK;
Example: DELETE FROM CUSTOMERS WHERE AGE = 25; ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without
rolling back the entire transaction.
Syntax: SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language: DQL is used to fetch the data from the database. It uses
only one command SELECT.
a. SELECT: This is the same as the projection operation of relational algebra. It is
used to select the attribute based on the condition described by WHERE clause.
Syntax: SELECT expressions FROM TABLES
WHERE conditions;
For example: SELECT emp_name FROM employee
WHERE age > 20;
SELECT Statement Clauses: The SELECT statement has the following clauses:
 SELECT
 FROM
 WHERE
 GROUP BY
 HAVING
 ORDER BY

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 5 of 24


SQL Operator: There are various types of SQL operator such as Arithmetic,
Comparison and logical Operators.

SQL Arithmetic Operators: Let's assume 'variable a' and 'variable b'. Here, 'a'
contains 20 and 'b' contains 10.

Operator Description Example


+ (a+b) It adds the value of both operands. a+b will give 30
- It subtracts b from a. a-b will give 10
* It is used to multiply the value of both operands. a*b will give 200
/ It divide a by b and returns the result. a/b will give 2
% It returns the reminder of a divided by b. a%b will give 0

SQL Comparison Operators: Let's assume 'variable a' and 'variable b'. Here,
'a' contains 20 and 'b' contains 10.

Operator Description Example


= It checks if two operands values are equal or not, if the (a=b) is not
values are queal then condition becomes true. true
!= It checks if two operands values are equal or not, if values (a!=b) is
are not equal, then condition becomes true. true
<> It checks if two operands values are equal or not, if values (a<>b) is
are not equal then condition becomes true. true
> It checks if the left operand value is greater than right (a>b) is not
operand value, if yes then condition becomes true. true
< It checks if the left operand value is less than right (a<b) is true
operand value, if yes then condition becomes true.
>= It checks if the left operand value is greater than or equal (a>=b) is
to the right operand value, if yes then condition becomes not true
true.
<= It checks if the left operand value is less than or equal to (a<=b) is
the right operand value, if yes then condition becomes true
true.
!< It checks if the left operand value is not less than the right (a!<b) is not
operand value, if yes then condition becomes true. true
!> It checks if the left operand value is not greater than the (a!>b) is
right operand value, if yes then condition becomes true. true

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 6 of 24


SQL Logical Operators: Here is the list of logical operator used in SQL:
Operator Description
ALL It compares a value to all values in another value set.
AND It allows the existence of multiple conditions in an SQL statement.
ANY It compares the values in the list according to the condition.
BETWEEN It is used to search for values that are within a set of values.
IN It compares a value to that specified list value.
NOT It reverses the meaning of any logical operator.
OR It combines multiple conditions in SQL statements.
EXISTS It is used to search for the presence of a row in a specified table.
LIKE It compares a value to similar values using wildcard operator.
Relational Algebra: Relational algebra is a procedural query language, which
takes instances of relations as input and yields instances of relations as output. It
uses operators to perform queries. An operator can be either unary or binary.
They accept relations as their input and yield relations as their output. Relational
algebra is performed recursively on a relation and intermediate results are also
considered relations. The various operations in relational algebra are as follows −
1. Select Operation 4. Set Intersection 7. Rename Operation
2. Project Operation 5. Set Difference 8. Join Operation
3. Union Operation 6. Cartesian product 9. Division Operation
Unary Relational Operations SELECT and PROJECT:
1. The Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).

Notation: σ p(r)
Where, p is propositional logic formula which may use connectors like: AND OR and
NOT. It can use relational operators like =, ≠, ≥, <, >, ≤. r is the relation on which
select operation is performed.
For example − σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
2. The Project Operation:
o This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
o It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where: A1, A2, A3 is used as attribute names of relation r.
For example − ∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 7 of 24


Binary Relational Operations: JOIN and DIVISION

1. The Join Operations: Join is a combination of a Cartesian product followed by a


selection process. A Join operation pairs two tuples from different relations, if and
only if a given join condition is satisfied. It is denoted by ⋈.

Types of Join operations:


Join
operation

Inner Outer
Join Join

Theta Equijoin Natural Left Right Full


Join Join Outer Outer Outer
Join Join Join
Inner Join: An inner join includes only those tuples with matching attributes and the
rest are discarded in the resulting relation. Theta Join, Equijoin, and Natural Join are
called inner joins.
Theta (θ) Join: Theta join combines tuples from different relations provided they
satisfy the theta condition. The join condition is denoted by the symbol θ.
 Notation: R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn)
respectivey such that the attributes don’t have anything in common, that is R1 ∩ R2
= Φ. Theta join can use all kinds of comparison operators.
Example: ρ Student_Detail
STUDENT⋈Student.Std = Subject.ClassSUBJECT
Student Subjects Student_detail
SID Name Std Class Subject SID Name Std Class Subject
101 Alex 10 10 Math 101 Alex 10 10 Math
102 Maria 11 10 English 101 Alex 10 10 English
11 Music 102 Maria 11 11 Music
11 Sports 102 Maria 11 11 Sports

Equijoin: When Theta join uses only equality comparison operator, it is said to be
equijoin. The above example corresponds to equijoin.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 8 of 24


Natural Join (⋈): Natural join does not use any comparison operator. It does not
concatenate the way a Cartesian product does. We can perform a Natural Join only
if there is at least one common attribute that exists between two relations. In
addition, the attributes must have the same name and domain. Natural join acts on
those matching attributes where the values of attributes in both the relations are
same.
Example:
Courses HoD Courses ⋈ HoD
CID Course Dept Dept Head Dept CID Course Head
CS01 Database CS CS Alex CS CS01 Database Alex
ME01 Mechanics ME ME Maya ME ME01 Mechanics Maya
EE01 Electronics EE EE Mira EE EE01 Electronics Mira
Outer Joins: Outer joins include all the tuples from the participating relations in the
resulting relation. There are three kinds of outer joins − left outer join, right outer
join, and full outer join.
Left Outer Join(R S): All the tuples from the Left relation, R, are included in the
resulting relation. If there are tuples in R without any matching tuple in the Right
relation S, then the S-attributes of the resulting relation are made NULL.
Example:
Courses HoD Courses HoD
CID CNAME CID HoDNAME CID CNAME HoDNAME
100 Database 100 Alex 100 Database Alex
101 Mechanics 102 Maya 101 Mechanics NULL
102 Electronics 104 Mira 102 Electronics Maya

Right Outer Join: ( R S ): All the tuples from the Right relation, S, are included
in the resulting relation. If there are tuples in S without any matching tuple in R, then
the R-attributes of resulting relation are made NULL.
Example:

Courses HoD Courses HoD


CID CNAME CID HoDNAME CID CNAME HoDNAME
100 Database 100 Alex 100 Database Alex
101 Mechanics 102 Maya 102 Electronics Maya
102 Electronics 104 Mira 104 NULL Mira

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 9 of 24


Full Outer Join: ( R S): All the tuples from both participating relations are
included in the resulting relation. If there are no matching tuples for both relations,
their respective unmatched attributes are made NULL.
Example:

Courses HoD Courses HoD


CID CNAME CID HoDNAME CID CNAME HoDNAME
100 Database 100 Alex 100 Database Alex
101 Mechanics 102 Maya 101 Mechanics NULL
102 Electronics 104 Mira 102 Electronics Maya
104 NULL Mira

Division Operator (÷): Division operator A÷B can be applied if and only if:
 Attributes of B is proper subset of Attributes of A.
 The relation returned by division operator will have attributes = (All attributes of A
– All Attributes of B)
 The relation returned by division operator will return those tuples from relation A
which are associated to every tuple of B.
Example:
STUDENT_SPORTS ALL_SPORTS STUDENT_SPORTS ÷
ALL_SPORTS
ROLL_NO SPORTS SPORTS ROLL_NO
1 Badminton Badminton 2
(ROLL_NO 2 is
2 Cricket Cricket
associated to
2 Badminton all tuples of B)
4 Badminton
 The operation is valid as attributes in ALL_SPORTS is a proper subset of
attributes in STUDENT_SPORTS.
 The attributes in resulting relation will have attributes
{ROLL_NO,SPORTS} - {SPORTS} = ROLL_NO
 The tuples in resulting relation will have those ROLL_NO which are associated
with all B’s tuple {Badminton, Cricket}. ROLL_NO 1 and 4 are associated to
Badminton only. ROLL_NO 2 is associated to all tuples of B. So the resulting
relation will be:
ROLL_NO
2

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 10 of 24


Other Operations in relational algebra:
Union Operation (∪ ): It performs binary union between two given relations and is
defined as − r ∪ s = { t | t ∈ r or t ∈ s}
Notation: r U s where r and s are either database relations or relation result
set (temporary relation).
For a union operation to be valid, the following conditions must hold −
 r, and s must have the same number of attributes.
 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.

Example: ∏ author (Books) ∪ ∏ author (Articles)


Output − Projects the names of the authors who have either written a book or an
article or both.
Set Difference (−): The result of set difference operation is tuples, which are
present in one relation but are not in the second relation.
Notation: r – s (Finds all the tuples that are present in r but not in s.)
Example: ∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ): Combines information of two different relations into one.
Notation − r Χ s Where r and s are relations and their output will be defined as:
r Χ s = { q t | q ∈ r and t ∈ s}
Example: σauthor = 'Korth'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by Korth.
Rename Operation (ρ): The results of relational algebra are also relations but
without any name. The rename operation allows us to rename the output relation.
'rename' operation is denoted with small Greek letter rho ρ.
Notation − ρ x (E) Where the result of expression E is saved with name of x.

Intersection Operation: The intersection operator gives the common data values
between the two data sets that are intersected. The two data sets that are
intersected should be similar for the intersection operator to work. Intersection also
removes all duplicates before displaying the result.
Example:
Select Student_Name from Art_Students
INTERSECT
Select Student_Name from Dance_Students
This will display the names of the students in the table Art_Students and in the table
Dance_Students i.e all the students that have taken both art and dance classes

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 11 of 24


Relational Calculus: Relational calculus is a non-procedural query language. In
the non-procedural query language, that is it tells what to do but never explains how
to do. Relational calculus is of tow types.
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)

1. Tuple Relational Calculus (TRC): The tuple relational calculus is specified to


select the tuples in a relation. In TRC, filtering variable uses the tuples of a relation.
The result of the relation can have one or more tuples.
Notation: {T | P (T)} or {T | Condition (T)}
Where T is the resulting tuples and P(T) is the condition used to fetch T.
Example: { T.name | Author(T) AND T.article = 'database' }
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a
tuple with 'name' from Author who has written an article on 'database'.

2. Domain Relational Calculus (DRC): In domain relational calculus, filtering


variable uses the domain of attributes. Domain relational calculus uses the same
operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Notation: { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formula built by inner attributes
Example:
{< article, subject > | < article, subject > ∈ Publication ∧ subject = 'database'}
Output: This query will yield the article and subject from the relation Publication,
where subject is database.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 12 of 24


CORE-6: Database Systems - Unit- 4
Transaction: The transaction is a set of logically related operation. It contains a
group of tasks. A transaction is an action or series of actions. It is performed by a
single user to perform operations for accessing the contents of the database. For
Example, Suppose an employee of bank transfers Rs 800 from X's account to Y's
account. This small transaction contains several low-level tasks:
X's Account Y's Account
Open_Account(X) Open_Account(Y)
Old_Balance = X.balance Old_Balance = Y.balance
New_Balance = Old_Balance - 800 New_Balance = Old_Balance + 800
X.balance = New_Balance Y.balance = New_Balance
Close_Account(X) Close_Account(Y)
Operations of Transaction: Following are the main operations of transaction:
Read(X): Read operation is used to read the value of X from the database and
stores it in a buffer in main memory.
Write(X): Write operation is used to write the value back to the database from the
buffer.
Let's take an example to debit transaction from an account which consists of
following operations:
1. R(X);
2. X = X - 500;
3. W(X);
Let's assume the value of X before starting of the transaction is 4000.
o The first operation reads X's value from database and stores it in a buffer.
o The second operation will decrease the value of X by 500. So buffer will
contain 3500.
o The third operation will write the buffer's value to the database. So X's final
value will be 3500.
But it may be possible that because of the failure of hardware, software or power,
etc. that transaction may fail before finished all the operations in the set.
For example: If in the above transaction, the debit transaction fails after executing
operation 2 then X's value will remain 4000 in the database which is not acceptable
by the bank. To solve this problem, we have two important operations:
Commit: It is used to save the work done permanently.
Rollback: It is used to undo the work done.
Transaction Property: The transaction has the four properties known as ACID
properties. These are used to maintain consistency in a database, before and after
the transaction. These are-
1. Atomicity
2. Consistency
3. Isolation
4. Durability

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 13 of 24


Atomicity: It states that all operations of the transaction take place at once if not,
the transaction is aborted. There is no midway, i.e., the transaction cannot occur
partially. Each transaction is treated as one unit and either run to completion or is
not executed at all. Atomicity involves the following two operations:
Abort: If a transaction aborts then all the changes made are not visible.
Commit: If a transaction commits then all the changes made are visible.
Example: Let's assume that transaction T consisting of T1 T2
T1 and T2. A consists of Rs 600 and B consists of Rs
300. Transfer Rs 100 from account A to account B. Read(A) Read(B)
After completion of the transaction, A consists of Rs 500 A:= A-100 Y:= Y+100
and B consists of Rs 400. Write(A) Write(B)
If the transaction T fails after the completion of transaction T1 but before completion
of transaction T2, then the amount will be deducted from A but not added to B. This
shows the inconsistent database state. In order to ensure correctness of database
state, the transaction must be executed in entirety.
Consistency: The integrity constraints are maintained so that the database is
consistent before and after the transaction. The execution of a transaction will leave
a database in either its prior stable state or a new stable state. The consistent
property of database states that every transaction sees a consistent database
instance. The transaction is used to transform the database from one consistent
state to another consistent state.
For example: The total amount must be maintained before or after the transaction.
Total before T occurs = 600+300=900
Total after T occurs= 500+400=900
Therefore, the database is consistent. In the case when T1 is completed but T2 fails,
then inconsistency will occur.
Isolation: It shows that the data which is used at the time of execution of a
transaction cannot be used by the second transaction until the first one is
completed. In isolation, if the transaction T1 is being executed and using the data
item X, then that data item can't be accessed by any other transaction T2 until the
transaction T1 ends. The concurrency control subsystem of the DBMS enforced the
isolation property.
Durability: The durability property is used to indicate the performance of the
database's consistent state. It states that the transaction made the permanent
changes. They cannot be lost by the erroneous operation of a faulty transaction or
by the system failure. When a transaction is completed, then the database reaches
a state known as the consistent state. That consistent state cannot be lost, even in
the event of a system's failure. The recovery subsystem of the DBMS has the
responsibility of Durability property.
States of Transaction: In a database, the transaction can be in one of the following
states -

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 14 of 24


Active state: The active state is the first state of every transaction. In this state, the
transaction is being executed. For example: Insertion or deletion or updating a
record is done here. But all the records are still not saved to the database.
Partially committed: In the partially committed state, a transaction executes its final
operation, but the data is still not saved to the database. In the total mark calculation
example, a final display of the total marks step is executed in this state.
Committed: A transaction is said to be in a committed state if it executes all its
operations successfully. In this state, all the effects are now permanently saved on
the database system.
Failed state: If any of the checks made by the database recovery system fails, then
the transaction is said to be in the failed state. In the example of total mark
calculation, if the database is not able to fire a query to fetch the marks, then the
transaction will fail to execute.
Aborted: If any of the checks fail and the transaction has reached a failed state then
the database recovery system will make sure that the database is in its previous
consistent state. If not then it will abort or roll back the transaction to bring the
database into a consistent state. If the transaction fails in the middle of the
transaction then before executing the transaction, all the executed transactions are
rolled back to its consistent state. After aborting the transaction, the database
recovery module will select one of the two operations:
1. Re-start the transaction
2. Kill the transaction
Schedule: A series of operation from one transaction to another transaction is
known as schedule. It is used to preserve the order of the operation in each of the
individual transaction.
1. Serial Schedule: The serial schedule is a type of schedule where one transaction
is executed completely before starting another transaction. In the serial schedule,
when the first transaction completes its cycle, then the next transaction is executed.
For example, suppose there are two transactions T1 and T2 which have some
operations. If it has no interleaving of operations, then there are the following two
possible outcomes:
1. Execute all the operations of T1 which was followed by all the operations of T2.
2. Execute all the operations of T2 which was followed by all the operations of T1.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 15 of 24


Fig(a) - Schedule A Fig(b) - Schedule B
o In Fig(a), Schedule A shows the serial schedule where T1 followed by T2.
o In Fig(b), Schedule B shows the serial schedule where T2 followed by T1.

2. Non-serial Schedule

Fig(c) - Schedule C Fig(d) - Schedule D


o If interleaving of operations is allowed, then there will be non-serial schedule.
o It contains many possible orders in which the system can execute the
individual operations of the transactions.
o In the above figure (c) and (d), Schedule C and Schedule D are the non-serial
schedules. It has interleaving of operations.
3. Serializable schedule
o A non-serial schedule will be serializable if its result is equal to the result of its
transactions executed serially.
Testing of Serializability: Precedence Graph is used to test the Serializability of a
schedule.
Assume a schedule S. For S, we
construct a graph known as precedence Ti Tj
graph. This graph has a pair G = (V, E),
where V consists a set of vertices, and E (Precedence graph for Schedule S)
consists a set of edges.
The set of vertices is used to contain all the transactions participating in the
schedule. The set of edges is used to contain all edges Ti ->Tj for which one of the
M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 16 of 24
three conditions holds:
1. Create a node Ti → Tj if Ti executes write (Q) before Tj executes read (Q).
2. Create a node Ti → Tj if Ti executes read (Q) before Tj executes write (Q).
3. Create a node Ti → Tj if Ti executes write (Q) before Tj executes write (Q).
o If a precedence graph contains a single edge Ti → Tj, then all the instructions
of Ti are executed before the first instruction of Tj is executed.
o If a precedence graph for schedule S contains a cycle, then S is non-
serializable. If the precedence graph has no cycle, then S is known as
serializable.

Example:
Schedule S1: Precedence graph of Schedule S1
Transaction Transaction
Time
T1 T2
T1 T2
t1 Read(A)
t2 Read(A) In above precedence graph of schedule
S1, contains two vertices T1 and T2, and
t3 Write(A) edges T1→ T2 and T2→ T1. In this
t4 A=A+50 Schedule S1, operations of T1 and T2
transaction are present in an interleaved
t5 Write(A) manner.
The precedence graph contains a cycle, that’s why schedule S1 is non-serializable.
Types of Serializability
1. Conflict Serializability
2. View Serializability
Conflict Serializable Schedule: A schedule is called conflict serializability if after
swapping of non-conflicting operations, it can transform into a serial schedule. The
schedule will be a conflict serializable if it is conflict equivalent to a serial schedule.
Conflicting Operations: The two operations become conflicting if all the following
conditions are satisfied.
1. Both belong to separate transactions.
2. They have the same data item.
3. They contain at least one write operation.

Example: Swapping is possible only if S1 and S2 are logically equal.


1. T1: Read(A) T2: Read(A)
T1 T2 swapped T1 T2
Read(A) Read(A)
Read(A) Read(A)
Schedule S1 Schedule S2
Here, S1 = S2. That means it is non-conflict.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 17 of 24


2. T1: Read(A) T2: Write(A)
T1 T2 swapped T1 T2
Read(A) Write(A)
Write(A) Read(A)
Schedule S1 Schedule S2
Here, S1 ≠ S2. That means it is conflict.
Conflict Equivalent: In conflict equivalent, one can be transformed to another by
swapping non-conflicting operations. In the given example, S2 is conflict equivalent
to S1 (S1 can be converted to S2 by swapping non-conflicting operations). Two
schedules are said to be conflict equivalent if and only if:
1. They contain the same set of the transaction.
2. If each pair of conflict operations are ordered in the same way.
Example:
Non-serial Schedule S2 is a serial schedule
Serial schedule because, in this, all operations of
schedule
T1 T2 T1 T2 T1 are performed before starting
Read(A) Read(A) any operation of T2. Schedule S1
can be transformed into a serial
Write(A) Write(A)
schedule by swapping non-
Read(A) Read(B) conflicting operations of S1. After
Write(A) Write(B) swapping of non-conflict
Read(B) Read(A) operations, the schedule S1
Write(B) Write(A) becomes the schedule S2 as
Read(B) Read(B) shown in the above figure. So, S1
is conflict serializable.
Write(B) Write(B)
Schedule S1 Schedule S2
View Serializability:
o A schedule will view serializable if it is view equivalent to a serial schedule.
o If a schedule is conflict serializable, then it will be view serializable.
o The view serializable which is not conflict serializable contains blind writes.

View Equivalent: Two schedules S1 and S2 are said to be view equivalent if they
satisfy the following conditions:
1. Initial Read: An initial read of both schedules must be the same. In schedule S1,
if a transaction T1 is reading the data item A, then in S2, transaction T1 should also
read A.
T1 T2 T1 T2
Read(A) Write(A)
Write(A) Read(A)
Schedule S1 Schedule S2
In the above example initial read operation is done by T1 in both the schedule S1
and S2.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 18 of 24


2. Updated Read: In schedule S1, if Ti is reading A which is updated by Tj then in
S2 also, Ti should read A which is updated by Tj.

T1 T2 T3 T1 T2 T3
Write(A) Write(A)
Write(A) Write(A)
Read(A) Read(A)
Schedule S1 Schedule S2
Above two schedules are not view equal because in S1, T3 is reading A which is
updated by T2 and in S2, T3 is reading A which is updated by T1.
3. Final Write: A final write must be the same between both the schedules. In
schedule S1, if a transaction T1 updates A at last then in S2, final writes operations
should also be done by T1.
T1 T2 T3 T1 T2 T3
Write(A) Read(A)
Read(A) Write(A)
Write(A) Write(A)
Schedule S1 Schedule S2
In the above example final write operation is done by T3 both in schedule S1 and
schedule S2.
Example:
T1 T2 T3With 3 transactions, the total number of possible
Read(A) schedule = 3! = 6
Write(A) S1 = <T1 T2 T3>
Write(A) S2 = <T1 T3 T2>
Write(A) S3 = <T2 T3 T1>
Schedule S S4 = <T2 T1 T3>
S5 = <T3 T1 T2>
S6 = <T3 T2 T1>
Taking first schedule S1:
T1 T2 T3 Step 1: Initial Read: The initial read operation
Read(A) in S is done by T1 and in S1, it is also done
Write(A) by T1.
Write(A) Step 2: Updated Read: In both schedules S
Write(A) and S1, there is no read except the initial
Schedule S1 read that's why we don't need to check that
condition.
Step 3: Final Write: The final write operation is done by T3 in both the schedule S
and S1. So, S and S1 are view Equivalent.
The first schedule S1 satisfies all three conditions, so we don't need to check other
schedules.
Hence, view equivalent serial schedule is: T1 → T2 → T3

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 19 of 24


Recoverability of Schedule: Sometimes a transaction may not execute
completely due to a software issue, system crash or hardware failure. In that case,
the failed transaction has to be rollback. But some other transaction may also have
used value produced by the failed transaction. So we also have to rollback those
transactions.
Irrecoverable schedule: The schedule will be irrecoverable if Tj reads the updated
value of Ti and Tj committed before Ti commit.
Table-1
T1 T1’s buffer space T2 T2’s buffer space Database
A = 6500
Read(A); A = 6500 A = 6500
A = A – 500; A = 6000 A = 6500
Write(A); A = 6000 A = 6000
Read(A); A = 6000 A = 6000
A = A + 1000; A = 7000 A = 6000
Write(A); A = 7000 A = 7000
Commit;
Failure Point
Commit;
The above table (Table-1) shows a schedule which has two transactions. T1 reads
and writes the value of A and that value is read and written by T2. T2 commits but
later on, T1 fails. Due to the failure, we have to rollback T1. T2 should also be
rollback because it reads the value written by T1, but T2 can't be rollback because it
is already committed. So this type of schedule is known as irrecoverable schedule.
Recoverable with cascading rollback: The schedule will be recoverable with
cascading rollback if Tj reads the updated value of Ti. Commit of Tj is delayed till
commit of Ti.
Table-2
T1 T1’s buffer space T2 T2’s buffer space Database
A = 6500
Read(A); A = 6500 A = 6500
A = A – 500; A = 6000 A = 6500
Write(A); A = 6000 A = 6000
Read(A); A = 6000 A = 6000
A = A + 1000; A = 7000 A = 6000
Write(A); A = 7000 A = 7000
Failure Point
Commit;
Commit;
The above table (Table-2) shows a schedule with two transactions. Transaction T1
reads and writes A, and that value is read and written by transaction T2. But later
on, T1 fails. Due to this, we have to rollback T1. T2 should be rollback because T2
has read the value written by T1. As it has not committed before T1 commits so we
can rollback transaction T2 as well. So it is recoverable with cascade rollback.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 20 of 24


Log-Based Recovery: The log is a sequence of records. Log of each
transaction is maintained in some stable storage so that if any failure occurs, then it
can be recovered from there. If any operation is performed on the database, then it
will be recorded in the log. But the process of storing the logs should be done before
the actual transaction is applied in the database. When the system is crashed, then
the system consults the log to find which transactions need to be undone and which
need to be redone.
Recovery using Checkpoint:
 The checkpoint is like a bookmark. While the execution of the transaction,
such checkpoints are marked, and the transaction is executed then using
the steps of the transaction, the log files will be created.
 When it reaches to the checkpoint, then the transaction will be updated into
the database, and till that point, the entire log file will be removed from the
file. Then the log file is updated with the new step of transaction till next
checkpoint and so on.
 The checkpoint is used to declare a point before which the DBMS was in
the consistent state, and all transactions were committed.
Concurrency Control: In the concurrency control, the multiple transactions can
be executed simultaneously. It may affect the transaction result. It is highly important
to maintain the order of execution of those transactions.
Problems of concurrency control: Several problems can occur when concurrent
transactions are executed in an uncontrolled manner. Following are the three
problems in concurrency control.
1. Lost updates
2. Dirty read
3. Inconsistent retrival
1. Lost update problem: If two transactions T1 and T2 read a record and then
update it, then the effect of updating of the first record will be overwritten by the
second update.
Example:
Time Transaction-X Transaction-Y Here, At time t1, transaction-X reads A's
t1 Read A value.
t2 Read A At time t2, Transaction-Y reads A's
t3 Update A value.
t4 Update A
At time t3, Transactions-X writes A's value on the basis of the value seen at time t1.
At time t4, Transactions-Y writes A's value on the basis of the value seen at time t2.
So at time T4, the update of Transaction-X is lost because Transaction y overwrites
it without looking at its current value. Such type of problem is known as Lost Update
Problem as update made by one transaction is lost here.
2. Dirty Read: The dirty read occurs in the case when one transaction updates an
item of the database, and then the transaction fails for some reason. The updated
database item is accessed by another transaction before it is changed back to the

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 21 of 24


original value. A transaction T1 updates a record which is read by T2. If T1 aborts
then T2 now has values which have never formed part of the stable database.

Time Transaction-X Transaction-Y  At time t1, transaction-Y writes A's value.


 At time t2, Transaction-X reads A's value.
t1 - Update-A
 At time t3, Transactions-Y rollbacks. So, it
t2 Read-A changes A's value back to that of prior to t1.
t3 Rollback
So, Transaction-X now contains a value which has never become part of the stable
database. Such type of problem is known as Dirty Read Problem, as one transaction
reads a dirty value which has not been committed.
3. Inconsistent Retrievals Problem: Ex- A transaction T1 reads a record and then
does some other processing during which the transaction T2 updates the record.
Now when the transaction T1 reads the record, then the new value will be
inconsistent with the previous value.
Example: Suppose two transactions operate on three accounts.
Account-1 Account-2 Account-3
Balance = 200 Balance = 250 Balance = 150
Transaction-X is doing the sum of all balance while transaction-Y is transferring an
amount 50 from Account-1 to Account-3.
Time Transaction-X Transaction-Y
Read balance of acc-1
t1 Sum  balance of acc-1
Read balance of acc-2
t2 Sum  sum + balance of acc-2 = 450
t3 Read balance of acc-3
Update balance of acc-3 balance
t4 of acc-3 + 50 = 200
t5 Read balance of acc-1
Update balance of acc-1 balance
t6 of acc-1 - 50 = 150
t7 Read balance of acc-3 commit
t8 Sum  sum + balance of acc-3 = 650
Here, transaction-X produces the result of 650 which is incorrect. If we write this
produced result in the database, the database will become an inconsistent state
because the actual sum is 600.
Concurrency Control Protocol: Concurrency control protocols ensure
atomicity, isolation, and serializability of concurrent transactions. There are mainly
two techniques of concurrency control protocol.
1. Lock based protocol
2. Time-stamp protocol

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 22 of 24


Lock-Based Protocol: In this type of protocol, any transaction cannot read or
write data until it acquires an appropriate lock on it. There are two types of lock:
1. Shared lock: It is also known as a Read-only lock. In a shared lock, the data item
can only read by the transaction. It can be shared between the transactions because
when the transaction holds a lock, then it can't update the data on the data item. It is
represented as S(Q) which denotes shared lock on data item ‘Q’ by a transaction.
2. Exclusive lock: In the exclusive lock, the data item can be both reads as well as
written by the transaction. This lock is mutually exclusive, and in this lock, multiple
transactions do not modify the same data simultaneously. It is represented as X(Q)
which denotes shared lock on data item ‘Q’ by a transaction. There are mainly two
types of lock protocols available.
1. Two-phase locking (2PL)
 The two-phase locking protocol divides the execution phase of the
transaction into three parts.
 In the first part, when the execution of the transaction starts, it seeks
permission for the lock it requires.
 In the second part, the transaction acquires all the locks. The third phase is
started as soon as the transaction releases its first lock.
 In the third phase, the transaction cannot demand any new locks. It only
releases the acquired locks.
 It may lead to cascade rollback.
 There are two phases in 2PL.
Growing phase: In the growing phase, a new lock on the data item may be
acquired by the transaction, but none can be released.
Shrinking phase: In the shrinking phase, existing lock held by the transaction may
be released, but no new locks can be acquired.
Example:
Tranction-T1 Tranction-T2 The following way shows how locking and
1 LOCK-S(A) unlocking work with 2-PL.
2 LOCK-S(A) Transaction T1:
3 LOCK-X(B)  Growing phase: from step 1-3
4 - -  Shrinking phase: from step 5-7
5 UNLOCK(A)  Lock point: at 4
6 LOCK-X(C)
7 UNLOCK(B) Transaction T2:
8 UNLOCK(A)  Growing phase: from step 2-7
 Shrinking phase: from step 8-9
9 UNLOCK(C)  Lock point: at 7
2. Strict Two-phase locking (Strict-2PL)
 The first phase of Strict-2PL is similar to 2PL, the only difference between
2PL and strict 2PL is that Strict-2PL does not release a lock after using it.
 Strict-2PL waits until the whole transaction to commit, and then it releases
all the locks at a time.
 Strict-2PL protocol does not have shrinking phase of lock release.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 23 of 24


 It does not have cascading abort as 2PL does.
Timestamp Ordering Protocol
 The Timestamp Ordering Protocol is used to order the transactions based
on their Timestamps. The order of transaction is nothing but the ascending
order of the transaction creation.
 The priority of the older transaction is higher that's why it executes first. To
determine the timestamp of the transaction, this protocol uses system time
or logical counter.
 The lock-based protocol is used to manage the order between conflicting
pairs among transactions at the execution time. But Timestamp based
protocols start working as soon as a transaction is created.
 Let's assume there are two transactions T1 and T2. Suppose the
transaction T1 has entered the system at 007 times and transaction T2 has
entered the system at 009 times. T1 has the higher priority, so it executes
first as it is entered the system first.
 The timestamp ordering protocol also maintains the timestamp of last 'read'
and 'write' operation on a data.
Basic Timestamp ordering protocol works as follows:
Let TS(Ti) denotes the timestamp of the transaction Ti.
R_TS(Q) denotes the largest time-stamp of any transaction that executed
Read(Q) successfully.
W_TS(Q) denotes the largest time-stamp of any transaction that executed
Write(Q) successfully.
1. The following conditions are checked whenever a transaction Ti issues a
Read (Q) operation:
 If TS(Ti) < W_TS(Q), then Ti needs to read a value of Q that was already
overwritten. Hence, the read operation is rejected and Ti is rolled back.
 If TS(Ti) >= W_TS(Q), then the read operation is executed and R_TS(Q) is
updated to TS(Ti).
2. The following conditions are checked whenever a transaction Ti issues
a Write(Q) operation:
 If TS(Ti) < R_TS(Q), then the value of Q that Ti is producing was needed
previously and the system assumed that value would never be produced.
Hence, the system rejects the write operation and rolls Ti back.
 If TS(Ti) < W_TS(Q), then Ti is attempting to write an obsolete value of Q.
Hence, the system rejects the write operation and rolls Ti back.
 Otherwise, the system executes the write operation and sets W_TS(Q) to
TS(Ti).
Advantages of Timestamp ordering protocol:
 Timestamp ordering protocol ensures serializability.
 TS protocol ensures freedom from deadlock.
Disadvantages of Timestamp ordering protocol:
 The schedule may not be recoverable and may not even be cascade-free.

M K Mishra, Asst. Prof. of Comp. Sc., FMAC, Bls. Page 24 of 24

You might also like