MCA_ADBMS_Module 3
MCA_ADBMS_Module 3
MODULE-3
SQL
SQL stands for Structured Query Language. It is used for storing and managing data in
Relational Database Management System (RDBMS).
• 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, Informix, Oracle, MS Access and SQL Server use SQL as
their standard database language.
• SQL allows users to query the database in a number of ways, using English-like statements.
• SQL stands for Structured Query Language. It is used for storing and managing data in
Relational Database Management System (RDBMS).
• Structure query language is not case sensitive. Generally, keywords of SQL are written in
uppercase.
• Statements of SQL are dependent on text lines. We can use a single SQL statement on one or
multiple text line.
• Using the SQL statements, you can perform most of the actions in a database.
• When an SQL command is executing for any RDBMS, then the system figure out the best
way to carry out the request and the SQL engine determines that how to interpret the task. • In
the process, various components are included.
These components can be optimization Engine, Query engine, Query dispatcher, classic, etc.
• All the non-SQL queries are handled by the classic query engine, but SQL query engine
won't handle logical files.
This section presents the syntax of a simple SQL query and explains its meaning through a
conceptual evaluation strategy. A conceptual evaluation strategy is a way to evaluate the
query that is intended to be easy to understand, rather than efficient. A DBMS would typically
execute a query in a different and more efficient way.
FROM from-list
WHERE qualification
FROM Sailors S
The answer is a set of rows, each of which is a pair hsname, agei. If two or more sailors
have the same name and age, the answer still contains just one pair with that name and age. This
query is equivalent to applying the projection operator of relational algebra.
If we omit the keyword DISTINCT, we would get a copy of the row hs,ai for each sailor with
name s and age a; the answer would be a multiset of rows.
The answer to this query with and without the keyword DISTINCT on instance S3 of Sailors is
shown in Figures 5.4 and 5.5. The only difference is that the tuple for Horatio appears twice if
DISTINCT is omitted; this is because there are two sailors called Horatio and age 35.
algebra.
FROM Sailors AS S
This query uses the optional keyword AS to introduce a range variable. Incidentally, when we
want to retrieve all columns, as in this query, SQL provides a convenient shorthand: We can
simply write SELECT *. This notation is useful for interactive querying, but it is poor style for
queries that are intended to be reused and maintained.
As these two examples illustrate, the SELECT clause is actually used to do projection, whereas
selections in the relational algebra sense are expressed using the WHERE clause! This mismatch
between the naming of the selection and projection operators in relational algebra and the syntax
of SQL is an unfortunate historical accident.
• The from-list in the FROM clause is a list of table names. A table name can be followed
by a range variable; a range variable is particularly useful when the same table name
appears more than once in the from-list.
• The select-list is a list of (expressions involving) column names of tables named in the
from-list. Column names can be prefixed by a range variable.
• The qualification in the WHERE clause is a boolean combination (i.e., an expression
using the logical connectives AND, OR, and NOT) of conditions of the form expression
op expression, where op is one of the comparison operators {<, <=, =, <>, >=, >}.2 An
expression is a column name, a constant, or an (arithmetic or string) expression.
• The DISTINCT keyword is optional. It indicates that the table computed as an answer to
this query should not contain duplicates, that is, two copies of the same row. The default
is that duplicates are not eliminated.
Although the preceding rules describe (informally) the syntax of a basic SQL query, they don’t
tell us the meaning of a query. The answer to a query is itself a relation—which is a multiset of
DoSR in Computer Applications 4
Relational Database Management Systems SHRUTHI B
2. Delete those rows in the cross-product that fail the qualification conditions.
This straightforward conceptual evaluation strategy makes explicit the rows that must be present
in the answer to the query. However, it is likely to be quite inefficient.
Find the names of sailors who have reserved boat number 103.
SELECT S.sname
The second step is to apply the qualification S.sid = R.sid AND R.bid=103. (Note that the first
part of this qualification requires a join operation.) This step eliminates all but the last row from
the instance shown in Figure 5.8. The third step is to eliminate unwanted columns; only sname
appears in the SELECT clause. This step leaves us with the result shown in Figure 5.9, which is a
table with a single column and, as it happens, just one row.
We now present several example queries, many of which were expressed earlier in relational
algebra and calculus (Chapter 4). Our first example illustrates that the use of range variables is
optional, unless they are needed to resolve an ambiguity. Query Q1, which we discussed in the
previous section, can also be expressed as follows:
SELECT sname
Only the occurrences of sid have to be qualified, since this column appears in both the Sailors
and Reserves tables. An equivalent way to write this query is:
SELECT sname
This query shows that table names can be used implicitly as row variables. Range variables need
to be introduced explicitly only when the FROM clause contains more than one occurrence of a
relation.3 However, we recommend the explicit use of range variables and full qualification of all
occurrences of columns with a range variable to improve the readability of your queries. We will
follow this convention in all our examples.
SELECT R.sid
This query contains a join of two tables, followed by a selection on the color of boats.
We can think of B and R as rows in the corresponding tables that ‘prove’ that a sailor with sid
R.sid reserved a red boat B.bid. On our example instances R2 and S3 (Figures 3The table name
cannot be used as an implicit range variable once a range variable is introduced for the relation.
SELECT S.sname
This query contains a join of three tables followed by a selection on the color of boats.
The join with Sailors allows us to find the name of the sailor who, according to Reserves tuple
R, has reserved a red boat described by tuple B.
SELECT B.color
This query is very similar to the previous one. Notice that in general there may be more than one
sailor called Lubber (since sname is not a key for Sailors); this query is still correct in that it will
return the colors of boats reserved by some Lubber, if there are several sailors called Lubber.
Find the names of sailors who have reserved at least one boat.
SELECT S.sname
The join of Sailors and Reserves ensures that for each selected sname, the sailor has
made some reservation. (If a sailor has not made a reservation, the second step in
the conceptual evaluation strategy would eliminate all rows in the cross-product that
The set operators work on complete rows of queries, so the results of the queries must have the
same column name and column order, and the types of columns must be compatible. There are the
following 4 set operators in SQL Server:
UNION: Combine two or more result sets into a single set without duplicates.
UNION ALL: Combine two or more result sets into a single set, including all duplicates.
INTERSECT: Takes the data from both result sets, which are in common.
EXCEPT: Takes the data from the first result set but not in the second result set (i.e., no
matching to each other)
• The result sets of all queries must have the same number of columns.
• In every result set, the data type of each column must be compatible (well-matched) with
the data type of its corresponding column in other result sets.
• An ORDER BY clause should be part of the last select statement to sort the result. The
first select statement must find out the column names or aliases.
Example
UNION Operator:
The Union operator will return all the unique rows from both queries. Notice that the
duplicates are removed from the result set.
UNION
Purpose: The UNION operator combines the result sets of two or more SELECT statements
into a single result set.
Distinct Values: It removes duplicate rows between the various SELECT statements.
Use Case: You would use UNION when listing all distinct rows from multiple tables or
queries.
UNION ALL
INTERSECT Operator:
The INTERSECT operator retrieves the common unique rows from the left and right queries.
Notice the duplicates are removed.
Purpose: The INTERSECT operator returns all rows common to both SELECT statements.
Distinct Values: Like UNION and EXCEPT, INTERSECT also removes duplicates.
Use Case: You would use INTERSECT when you need to find rows that are shared between
two tables or queries.
EXCEPT Operator:
The EXCEPT operator will return unique rows from the left query that aren’t in the right
query’s results.
EXCEPT
Purpose: The EXCEPT operator returns all rows from the first SELECT statement that are
absent in the second SELECT statement’s results.
Distinct Values: It automatically removes duplicates.
Use Case: EXCEPT is used when you want to find rows in one query that are not found in
another. It’s useful for finding differences between tables or queries.
NESTED QUERIES
One of the most powerful features of SQL is nested queries. A nested query is a query that has
another query embedded within it; the embedded query is called a subquery.
When writing a query, we sometimes need to express a condition that refers to a table that must
itself be computed. The query used to compute this subsidiary table is a subquery and appears as
part of the main query. A subquery typically appears within the WHERE clause of a query.
Subqueries can sometimes appear in the FROM clause or the HAVING clause (which we present
in Section 5.5). This section discusses only subqueries that appear in the WHERE clause. The
treatment of subqueries appearing elsewhere is quite similar.
As an example, let us rewrite the following query, which we discussed earlier, using a nested
subquery:
SELECT S.sname
FROM Sailors S
FROM Reserves R
The nested subquery computes the (multi)set of sids for sailors who have reserved boat 103 (the
set contains 22, 31, and 74 on instances R2 and S3), and the top-level query retrieves the names of
sailors whose sid is in this set. The IN operator allows us to test whether a value is in a given set
of elements; an SQL query is used to generate the set to be tested. Notice that it is very easy to
modify this query to find all sailors who have not reserved boat 103—we can just replace IN by
NOT IN
SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R
WHERE R.bid IN ( SELECT B.bid FROM Boats B WHERE B.color = ‘red’ )
The innermost subquery finds the set of bids of red boats (102 and 104 on instance B1). The
subquery one level above finds the set of sids of sailors who have reserved one of these boats. On
instances B1, R2, and S3, this set of sids contains 22, 31, and 64. The top-level query finds the
names of sailors whose sid is in this set of sids. For the example instances, we get Dustin, Lubber,
and Horatio.
Set-Comparison Operators We have already seen the set-comparison operators EXISTS, IN, and
UNIQUE, along with their negated versions. SQL also supports op ANY and op ALL, where op is
one of the arithmetic comparison operators {<=, =, <>, >=, >}. (SOME is also available, but it is
just a synonym for ANY.) (Q22)
Find sailors whose rating is better than some sailor called Horatio.
DoSR in Computer Applications 12
Relational Database Management Systems SHRUTHI B
SELECT S.sid FROM Sailors S WHERE S.rating > ANY ( SELECT S2.rating FROM Sailors
S2 WHERE S2.sname = ‘Horatio’ )
If there are several sailors called Horatio, this query finds all sailors whose rating is better than
that of some sailor called Horatio. On instance S3, this computes the sids 31, 32, 58, 71, and 74.
What if there were no sailor called Horatio? In this case the comparison S.rating > ANY ... is
defined to return false, and the above query returns an empty answer set. To understand
comparisons involving ANY, it is useful to think of the comparison being carried out repeatedly.
In the example above, S.rating is successively compared with each rating value that is an answer
to the nested query. Intuitively, the subquery must return a row that makes the comparison true, in
order for S.rating > ANY ... to return true
SELECT S.sid
FROM Sailors S
The subquery computes the set of all rating values in Sailors. The outer WHERE condition is
satisfied only when S.rating is greater than or equal to each of these rating values, i.e., when it is
the largest rating value. In the instance S3, the condition is only satisfied for rating 10, and the
answer includes the sids of sailors with this rating, i.e., 58 and 71.
Note that IN and NOT IN are equivalent to = ANY and <> ALL, respectively.
AGGREGATE OPERATORS
An aggregate function performs a calculation on a set of values, and returns a single value.
Except for COUNT(*), aggregate functions ignore null values. Aggregate functions are often used
with the GROUP BY clause of the SELECT statement.
All aggregate functions are deterministic. In other words, aggregate functions return the same
value each time that they are called, when called with a specific set of input values. The OVER
clause may follow all aggregate functions, except the STRING_AGG, GROUPING or
GROUPING_ID functions.
• A HAVING clause.
COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Syntax
COUNT(*)
or
Example:
COUNT()
Output:
10
Output:
Output:
Output:
Com1 5
Com2
Com3 3
2
Com1 5
Com2
3
Output:
SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Syntax
SUM()
or
Example: SUM()
Output:
DoSR in Computer Applications 16
Relational Database Management Systems SHRUTHI B
670
Output:
320
GROUP BY COMPANY;
Output:
Company1 150
Company2 170
Output:
Com1 335
Com3 170
AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax
AVG()
or
Example:
Output:
67.00
MAX Function
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax
MAX()
or
Example:
Output:
30
MIN Function
MIN function is used to find the minimum value of a certain column. This function determines
the smallest value of all selected values of a column.
Syntax
MIN()
or
Example:
Output:
10
NULL VALUES
Null means nothing. Null values are those values which are assigned to an attribute if its value is
unknown or is not applicable. Null values are used by DBMS when the user does not know the
type information to be entered for a particular field, so the user doesn’t enter any data and DBMS
assigns that field a NULL value. A NULL value does not represent a zero or spaces it just means
the absence of value for that field and this value can be inserted later.
For example in the employee table it is not necessary that all the employees should have a phone
number, so for the employer who does not have one yet, a NULL value can be assigned for that.
When we insert a tuple (124, ‘Kelly’, 6, NULL, 26, NULL) in employer table then for the
attribute e.sal and phone NULL values are assigned. Assignment of NULL value simply indicates
that the value is not known or is inapplicable for that attribute. A field which is not declared as
NOT NULL can have NULL values. But this inclusion of special type of value may result in the
complications of other operations. NULL values are used to deal with the exceptional data or the
data that is not complete and any operation performed on null values may result into inconsistent
data. In the Notes following section we will discuss the problems that arises due to NULL value.
When we compare valid values with NULL values boolean logic doesn’t come handy i.e., we
can’t use a boolean logic which is two valued - TRUE or FALSE. With NULL values we have to
use a then valued logic - TRUE or FALSE or UNKNOWN. For example, previous; we entered a
NULL value for salary attribute of Kelly. Now if apply condition that-list all the employees whose
salary > 20000, the evaluation of this condition for likely is UNKNOWN because salary is a
NULL value for Kelly. Similarly, for other comparison operators (>, <, =, <>), the evaluation of
the condition will always be unknown.
SQL provides is NULL comparison operator to compare to: NULL values i.e., to test whether a
value is NULL or not. If we apply this operator to salary attribute
IS NULL E.esal then it will return a TRUE to indicate that e.sal attribute is a null value.
Use of logical connectives with NULL values becomes a lit complicated if we doesn’t use three
valued logic. For example consider the following query. Query : List all the employees whose age
is less than 40 whose salary is grater than 30000.
Solution: SELECT *
All the tuples that satisfy this condition are selected but what about the tuple we inserted with
e.sal as NULL value? In this case, this condition will evaluate to unknown logical operators which
involves at least one column whose value is assigned as NULL will always result in an unknown
value. The following table will give you a better understanding of logical operators when used
with null values. Point to note here is that we are using a three valued logic TRUE, FALSE or
UNKNOWN i.e., the logical condition applied may evaluate to any one of them. (Unknown is
used in case of NULL values).
Integrity constraints in SQL are rules that help ensure the accuracy and reliability of data in the
database. They ensure that certain conditions are met when data is inserted, updated, or deleted.
DoSR in Computer Applications 20
Relational Database Management Systems SHRUTHI B
While primary key, unique, and foreign key constraints are commonly discussed and used, SQL
allows for more complex constraints through the use of CHECK and custom triggers. Here are
some examples of complex integrity constraints:
1. Check Constraints: Check constraints are used to restrict the range of values that can be
entered into a column. For example, a check constraint can be used to ensure that a date
column only contains dates in a certain range.
2. Assertions: Assertions are used to specify complex rules that cannot be expressed using
simple constraints. They are typically used to enforce business rules, such as ensuring that a
customer's age is greater than a certain value.
In this example, we are creating a table called "Customer" with columns for ID, Name, Age,
and Gender. We have added three constraints: one to ensure that the age is greater than or
equal to 18, another to ensure that the gender is either "M" or "F", and a third to ensure that if
the gender is "F", the age is greater than or equal to 21.
3.Domain Constraints
A user can define a new domain using the CREATE DOMAIN statement, which makes use
of CHECK constraints.
INTEGER is the base type for the domain ratingval, and every ratingval value must be of this
type. Values in ratingval are further restricted by using a CHECK constraint; in defining this
constraint, we use the keyword VALUE to refer to a value in the domain. By using this
facility, we can constrain the values that belong to a domain using the full power of SQL
queries. Once a domain is defined, the name of the domain can be used to restrict column
values in a table; we can use the following line in a schema declaration, for example:
rating ratingval
The optional DEFAULT keyword is used to associate a default value with a domain. If the
domain ratingval is used for a column in some relation, and no value is entered for this
column in an inserted tuple, the default value 0 associated with ratingval is used. (If a default
value is specified for the column as part of the table definition, this takes precedence over the
default value associated with the domain.) This feature can be used to minimize data entry
errors; common default values are automatically filled in rather than being typed in
A condition in a trigger can be a true/false statement (e.g., all employee salaries are less than
$100,000) or a query. A query is interpreted as true if the answer set is nonempty, and false if
the query has no answers. If the condition part evaluates to true, the action associated with the
trigger is executed.
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. In fact, an action can even execute a series of
data-definition commands (e.g., create new tables, change authorizations) and transaction-
oriented commands (e.g., commit), or call host language procedures.
An important issue is when the action part of a trigger executes in relation to the statement
that activated the trigger. For example, a statement that inserts records into the Students table
may activate a trigger that is used to maintain statistics on how many students younger than
18 are inserted at a time by a typical insert statement.
Depending on exactly what the trigger does, we may want its action to execute before
changes are made to the Students table, or after: a trigger that initializes a variable used to
DoSR in Computer Applications 23
Relational Database Management Systems SHRUTHI B
count the number of qualifying insertions should be executed before, and a trigger that
executes once per qualifying inserted record and increments the variable should be executed
after each record is inserted (because we may want to examine the values in the new record to
determine the action).
The examples shown in Figure 5.19, written using Oracle 7 Server syntax for defining triggers,
illustrate the basic concepts behind triggers. (The SQL:1999 syntax for these triggers is similar;
we will see an example using SQL:1999 syntax shortly.) 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;
count := 0;
END
count := count + 1;
END
Transaction Management
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)
Each individual transaction must display atomicity, consistency, isolation, and durability.
These four properties are sometimes referred to as the ACID test. Let’s look briefly at each of the
properties.
• Atomicity requires that all operations (SQL requests) of a transaction be completed; if not, the
transaction is aborted. If a transaction T1 has four SQL requests, all four requests must be
successfully completed; otherwise, the entire transaction is aborted.
• Consistency indicates the permanence of the database’s consistent state. A transaction takes a
database from one consistent state to another. When a transaction is completed, the database must
be in a consistent state. If any of the transaction parts violates an integrity constraint, the entire
transaction is aborted.
• Isolation means that the data used during the execution of a transaction cannot be used by a
second transaction until the first one is completed. In other words, if transaction T1 is being
executed and is using the data item X, that data item cannot be accessed by any other transaction
(T2 … Tn) until T1 ends. This property is particularly useful in multiuser database environments
because several users can access and update the database at the same time.
• Durability ensures that once transaction changes are done and committed, they cannot be
undone or lost, even in the event of a system failure.
In addition to the individual transaction properties indicated above, there is another important
property that applies when executing multiple transactions concurrently. For example, let’s
assume that the DBMS has three transactions (T1, T2 and T3) executing at the same time. To
properly carry out transactions, the DBMS must schedule the concurrent execution of the
transaction’s operations. In this case, each individual transaction must comply with the ACID
properties and, at the same time, the schedule of such multiple transaction operations must exhibit
the property of serializability. Serializability
ensures that the schedule for the concurrent execution of the transactions yields consistent results.
This property is important in multiuser and distributed databases in which multiple transactions
are likely to be executed concurrently. Naturally, if only a single transaction is executed,
serializability is not an issue.
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
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, the transaction finally reaches this state.
Concurrent Execution
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.
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
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
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;
Concurrency control
Concurrency control is a technique that ensures the correct and consistent execution of
transactions in a database system. Transactions are units of work that access and modify data
in a database. Concurrency control ensures that transactions do not interfere with each other
and preserve the ACID properties of the database.
There are different methods of concurrency control, such as timestamp-based, validation-
based, and lock-based. In this tutorial, you will learn about lock-based concurrency control,
which is one of the most widely used techniques in database systems. Lock-based
concurrency control uses locks to prevent concurrent transactions from accessing the same
data item at the same time.
You will also learn about the two-phase locking protocol, which is a set of rules that
transactions must follow to acquire and release locks. Two-phase locking ensures that
transactions are serializable, meaning that they produce the same result as if they were
executed one after another. You will explore the different variants of two-phase locking, such
as basic, strict, and rigorous, and their advantages and disadvantages.
Finally, you will learn about deadlock prevention and detection, which are techniques to
deal with the problem of deadlock. Deadlock is a situation where two or more transactions are
waiting for each other to release locks, and none of them can proceed. You will learn how to
prevent deadlock by imposing some restrictions on the transactions, and how to detect and
resolve deadlock by using a wait-for graph.
By the end of this tutorial, you will have a solid understanding of lock-based concurrency
control, two-phase locking, and deadlock prevention and detection. You will also be able to
apply these concepts to your own database applications and improve their performance and
reliability.
• Lock-Based Concurrency Control
Lock-based concurrency control is a technique that uses locks to prevent concurrent
transactions from accessing the same data item at the same time. A lock is a mechanism that
grants or denies access to a data item based on some rules. A transaction must acquire a lock
on a data item before reading or writing it, and release the lock after finishing its operation.
By using locks, transactions can ensure that they do not interfere with each other and preserve
the consistency and isolation of the database.
However, lock-based concurrency control also introduces some challenges, such as how to
manage the locks, how to avoid conflicts and deadlocks, and how to balance the performance
and concurrency of the system. In this section, you will learn about the basic concepts and
principles of lock-based concurrency control, such as lock types and modes, lock
compatibility matrix, and lock granularity. You will also learn how to apply these concepts to
your own database applications and improve their efficiency and reliability.
Let’s start by understanding the different types and modes of locks that are used in lock-based
concurrency control.
The two-phase locking protocol is a set of rules that transactions must follow to acquire and
release locks in a lock-based concurrency control system. The two-phase locking protocol
ensures that transactions are serializable, meaning that they produce the same result as if they
were executed one after another. The two-phase locking protocol also prevents some types of
anomalies, such as dirty reads, non-repeatable reads, and phantom reads, that may occur when
transactions access the same data concurrently.
The two-phase locking protocol consists of two phases: the growing phase and the shrinking
phase. In the growing phase, a transaction can acquire locks on the data items that it needs,
but it cannot release any lock. In the shrinking phase, a transaction can release locks on the
data items that it no longer needs, but it cannot acquire any new lock. The point where the
transaction switches from the growing phase to the shrinking phase is called the lock point.
The lock point is usually the same as the commit point, which is the point where the
transaction commits its changes to the database. However, the lock point can also be earlier or
later than the commit point, depending on the variant of the two-phase locking protocol.