UNIT-3 DBMS Notes
UNIT-3 DBMS Notes
progressive fashion. The efficiency of the design gets better with each higher degree of
normalization.
Advantages of Relational Databases
The main advantages of relational databases are that they enable users to easily categorize and
store data that can later be queried and filtered to extract specific information for reports.
Relational databases are also easy to extend and aren't reliant on the physical organization.
After the original database creation, a new data category can be added without all existing
applications being modified.
Other Advantages
Accurate − Data is stored just once, which eliminates data deduplication.
Flexible − Complex queries are easy for users to carry out.
Collaborative −Multiple users can access the same database.
Trusted −Relational database models are mature and well-understood.
Secure − Data in tables within relational database management systems
(RDBMS) can be limited to allow access by only particular users.
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.
1. 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:
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.
1. Emp_Id → Emp_Name
42 abc 17
43 pqr 18
44 xyz 18
42 abc 17
43 pqr 18
44 xyz 18
42 abc 17
43 pqr 18
44 xyz 18
45 abc 19
42 abc CO 4
43 pqr EC 2
44 xyz IT 1
45 abc EC 2
Normalization
A large database defined as a single relation may result in data duplication. This
repetition of data may result in:
So to handle these problems, we should analyze and decompose the relations with
redundant data into smaller, simpler, and well-structured relations that are satisfy
desirable properties. Normalization is a process of decomposing the relations into
relations with fewer attributes.
What is Normalization?
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of relations.
It is also used to eliminate undesirable characteristics like Insertion, Update, and
Deletion Anomalies.
o Normalization divides the larger table into smaller and links them using relationships.
o The normal form is used to reduce redundancy from the database table.
The main reason for normalizing the relations is removing these anomalies. Failure to
eliminate anomalies leads to data redundancy and can cause data integrity and other
problems as the database grows. Normalization consists of a series of guidelines that
helps to guide you in creating a good database structure.
Normal Description
Form
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are fully
functional dependent on the primary key.
5NF A relation is in 5NF. If it is in 4NF and does not contain any join dependency,
joining should be lossless.
Advantages of Normalization
o Normalization helps to minimize data redundancy.
o Greater overall database organization.
o Data consistency within the database.
o Much more flexible database design.
o Enforces the concept of relational integrity.
Disadvantages of Normalization
o You cannot start building the database before knowing what the user needs.
o The performance degrades when normalizing the relations to higher normal forms, i.e.,
4NF, 5NF.
o It is very time-consuming and difficult to normalize relations of a higher degree.
o Careless decomposition may lead to a bad database design, leading to serious
problems.
Keys
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also used
to establish and identify relationships between tables.
For example, ID is used as a key in the Student table because it is unique for each
student. In the PERSON table, passport_number, license_number, SSN are keys since
they are unique for each person.
5. Alternate key
There may be one or more attributes or a combination of attributes that uniquely
identify each tuple in a relation. These attributes or combinations of the attributes are
called the candidate keys. One key is chosen as the primary key from these candidate
keys, and the remaining candidate key, if it exists, is termed the alternate key. In other
words, the total number of the alternate keys is the total number of candidate keys
minus the primary key. The alternate key may or may not exist. If there is only one
candidate key in a relation, it does not have an alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that
act as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the
other candidate key, PAN_No, acts as the Alternate key.
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite
key. This key is also known as Concatenated Key.
For example, in employee relations, we assume that an employee may be assigned
multiple roles, and an employee may work on multiple projects simultaneously. So the
primary key will be composed of all three attributes, namely Emp_ID, Emp_role, and
Proj_ID in combination. So these attributes act as a composite key since the primary
key comprises more than one attribute.
7. Artificial key
The key created using arbitrarily assigned data are known as artificial keys. These keys
are created when a primary key is large and complex and has no relationship with
many other relations. The data values of the artificial keys are usually numbered in a
serial order.
For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID,
is large in employee relations. So it would be better to add a new virtual attribute to
identify each tuple in the relation uniquely.
First Normal Form (1NF)
o A relation will be 1NF if it contains an atomic value.
o It states that an attribute of a table cannot hold multiple values. It must hold
only single-valued attribute.
o First normal form disallows the multi-valued attribute, composite attribute, and
their combinations.
EMPLOYEE table:
14 John 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
Example: Let's assume, a school can store the data of teachers and the subjects they
teach. In a school, a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
A relation is in third normal form if it holds atleast one of the following conditions for
every non-trivial function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Example: Let's assume there is a company where employees work in more than one
department.
EMPLOYEE table:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
EMP_DEPT table:
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
Now, this is in BCNF because left side part of both the functional dependencies is a
key.
A B C
12 25 34
10 36 09
12 42 30
It decomposes into the two sub relations −
R1 (A, B)
A B
12 25
10 36
12 42
R2 (B, C)
B C
25 34
36 09
42 30
Now, we can check the first condition for Lossless-join decomposition.
The union of sub relation R1 and R2 is the same as relation R.
R1 U R 2 = R
We get the following result −
A B C
12 25 34
10 36 09
12 42 30
The relation is the same as the original relation R. Hence, the above decomposition is Lossless-
join decomposition.
Relational Decomposition
o When a relation in the relational model is not in appropriate normal form then the
decomposition of a relation is required.
o In a database, it breaks the table into multiple tables.
o If the relation has no proper decomposition, then it may lead to problems like loss of
information.
o Decomposition is used to eliminate some of the problems of bad design like anomalies,
inconsistencies, and redundancy.
Types of Decomposition
Lossless Decomposition
o If the information is not lost from the relation that is decomposed, then the
decomposition will be lossless.
o The lossless decomposition guarantees that the join of relations will result in the same
relation as it was decomposed.
o The relation is said to be lossless decomposition if natural joins of all the
decomposition give the original relation.
Example:
EMPLOYEE_DEPARTMENT table:
The above relation is decomposed into two relations EMPLOYEE and DEPARTMENT
EMPLOYEE table:
22 Denim 28 Mumbai
33 Alina 25 Delhi
46 Stephan 30 Bangalore
52 Katherine 36 Mumbai
60 Jack 40 Noida
DEPARTMENT table
827 22 Sales
438 33 Marketing
869 46 Finance
575 52 Production
678 60 Testing
Now, when these two relations are joined on the common column "EMP_ID", then the
resultant relation will look like:
Employee ⋈ Department
Dependency Preserving
o It is an important constraint of the database.
o In the dependency preservation, at least one decomposed table must satisfy every
dependency.
o 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.
o 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).
Fourth normal form (4NF)
o A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
o For a dependency A → B, if for a single value of A, multiple values of B exists, then the
relation will be a multi-valued dependency.
Example
STUDENT
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent
entity. Hence, there is no relationship between COURSE and HOBBY.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
Fifth normal form (5NF)
o A relation is in 5NF if it is in 4NF and not contains any join dependency and joining
should be lossless.
o 5NF is satisfied when all the tables are broken into as many tables as possible in order
to avoid redundancy.
o 5NF is also known as Project-join normal form (PJ/NF).
Example
SUBJECT LECTURER SEMESTER
In the above table, John takes both Computer and Math class for Semester 1 but he
doesn't take Math class for Semester 2. In this case, combination of all these fields
required to identify a valid data.
Suppose we add a new Semester as Semester 3 but do not know about the subject
and who will be taking that subject so we leave Lecturer and Subject as NULL. But all
three columns together acts as a primary key, so we can't leave other two columns
blank.
So to make the above table into 5NF, we can decompose it into three relations P1, P2
& P3:
P1
SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math
P2
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMSTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
Domain-Key Normal Form
A relation is in DKNF when insertion or delete anomalies are not present in the database.
Domain-Key Normal Form is the highest form of Normalization. The reason is that the
insertion and updation anomalies are removed. The constraints are verified by the domain and
key constraints.
A table is in Domain-Key normal form only if it is in 4NF, 3NF and other normal forms. It is
based on constraints − 5NF*
Domain Constraint
Values of an attribute had some set of values, for example, EmployeeID should be four digits
long −
0921 Tom 33
0922 Jack 31
Key Constraint
An attribute or its combination is a candidate key
General Constraint
Predicate on the set of all relations.
Every constraint should be a logical sequence of the domain constraints and key constraints
applied to the relation. The practical utility of DKNF is less.
States of Transaction
In a database, the transaction can be in one of the following states -
Active state
o The active state is the first state of every transaction. In this state, the transaction is
being executed.
o 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
o In the partially committed state, a transaction executes its final operation, but the data
is still not saved to the database.
o 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
o If any of the checks made by the database recovery system fails, then the transaction
is said to be in the failed state.
o 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
o 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.
o 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.
o 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 T1 which was followed by all the operations of T2.
o In the given (a) figure, Schedule A shows the serial schedule where T1 followed by T2.
o In the given (b) figure, Schedule B shows the serial schedule where T2 followed by T1.
2. Non-serial Schedule
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 given figure (c) and (d), Schedule C and Schedule D are the non-serial schedules.
It has interleaving of operations.
3. Serializable schedule
o The serializability of schedules is used to find non-serial schedules that allow the
transaction to execute concurrently without interfering with one another.
o It identifies which schedules are correct when executions of the transaction have
interleaving of their operations.
o A non-serial schedule will be serializable if its result is equal to the result of its
transactions executed serially.
Here,
In the MongoDB developer certificate, the DBMS uses various locking systems to allow
the other processes while maintaining the integrity of the data. In MongoDB, the most
restricted level for serializability is the employee can be restricted by two-phase locking
or 2PL. In the first phase of the locking level, the data objects are locked before the
execution of the operation. When the transaction has been accomplished, then the
lock for the data object is released. This process guarantees that there is no conflict in
operation and that all the transaction views the database as a conflict database.
The two-phase locking or 2PL system provides a strong guarantee for the conflict of
the database.
It can reduce the decreased performance and then increase the overhead acquiring
capacity and then release the lock of the data. As a result, the system allows the
constraint serializability for better performance of the DBMS. This ensures that the final
result is the same as some sequential execution and performs the improvement of the
operation that is involved in the database.
Thus, serializability is the system's property that describes how the different process
operates the shared data. In DBMS, the overall Serializable property is adopted by
locking the data during the execution of other processes. Also, serializability ensures
that the final result is equivalent to the sequential operation of the data.
Types of Serializability
In DBMS, all the transaction should be arranged in a particular order, even if all the
transaction is concurrent. If all the transaction is not serializable, then it produces the
incorrect result.
In DBMS, there are different types of serializable. Each type of serializable has some
advantages and disadvantages. The two most common types of serializable are view
serializability and conflict serializability.
1. Conflict Serializability
Conflict serializability is a type of conflict operation in serializability that operates the
same data item that should be executed in a particular order and maintains the
consistency of the database. In DBMS, each transaction has some unique value, and
every transaction of the database is based on that unique value of the database.
This unique value ensures that no two operations having the same conflict value are
executed concurrently. For example, let's consider two examples, i.e., the order table
and the customer table. One customer can have multiple orders, but each order only
belongs to one customer. There is some condition for the conflict serializability of the
database. These are as below.
If there are two transactions that are executed concurrently, one operation has to add
the transaction of the first customer, and another operation has added by the second
operation. This process ensures that there would be no inconsistency in the database.
2. View Serializability
View serializability is a type of operation in the serializable in which each transaction
should produce some result and these results are the output of proper sequential
execution of the data item. Unlike conflict serialized, the view serializability focuses on
preventing inconsistency in the database. In DBMS, the view serializability provides the
user to view the database in a conflicting way.
o The first condition is each schedule has the same type of transaction. The meaning of
this condition is that both schedules S1 and S2 must not have the same type of set of
transactions. If one schedule has committed the transaction but does not match the
transaction of another schedule, then the schedule is not equivalent to each other.
o The second condition is that both schedules should not have the same type of read or
write operation. On the other hand, if schedule S1 has two write operations while
schedule S2 has one write operation, we say that both schedules are not equivalent to
each other. We may also say that there is no problem if the number of the read
operation is different, but there must be the same number of the write operation in
both schedules.
o The final and last condition is that both schedules should not have the same conflict.
Order of execution of the same data item. For example, suppose the transaction of
schedule S1 is T1, and the transaction of schedule S2 is T2. The transaction T1 writes
the data item A, and the transaction T2 also writes the data item A. in this case, the
schedule is not equivalent to each other. But if the schedule has the same number of
each write operation in the data item then we called the schedule equivalent to each
other.
For better understanding, let's explain these with an example. Suppose there are two
users Sona and Archita. Each executes two transactions. Let's transactions T1 and T2
are executed by Sona, and T3 and T4 are executed by Archita. Suppose transaction T1
reads and writes the data item A, transaction T2 reads the data item B, transaction T3
reads and writes the data item C and transaction T4 reads the data item D. Lets the
schedule the above transaction as below.
1. •
2. T1: Read A → Write A→ Read B → Write B`
3. •
4. `T2: Read B → Write B`
5. •
6. `T3: Read C → Write C→ Read D → Write D`
7. • `T4: Read D → Write D
In order for a schedule to be considered serializable, it must first satisfy the conflict
serializability property. In our example schedule above, notice that Transaction 1 (T1)
and Transaction 2 (T2) read data item B before either writing it. This causes a conflict
between T1 and T2 because they are both trying to read and write the same data item
concurrently. Therefore, the given schedule does not conflict with serializability.
However, there is another type of serializability called view serializability which our
example does satisfy. View serializability requires that if two transactions cannot see
each other's updates (i.e., one transaction cannot see the effects of another concurrent
transaction), the schedule is considered to view serializable. In our example,
Transaction 2 (T2) cannot see any updates made by Transaction 4 (T4) because they
do not share common data items. Therefore, the schedule is viewed as serializable.
It's important to note that conflict serializability is a stronger property than view
serializability because it requires that all potential conflicts be resolved before any
updates are made (i.e., each transaction must either read or write each data item before
any other transaction can write it). View serializability only requires that if two
transactions cannot see each other's updates, then the schedule is view serializable &
it doesn't matter whether or not there are potential conflicts between them.
All in all, both properties are necessary for ensuring correctness in concurrent
transactions in a database management system.
1. Predictable execution: In serializable, all the threads of the DBMS are executed at one
time. There are no such surprises in the DBMS. In DBMS, all the variables are updated
as expected, and there is no data loss or corruption.
2. Easier to Reason about & Debug: In DBMS all the threads are executed alone, so it is
very easier to know about each thread of the database. This can make the debugging
process very easy. So we don't have to worry about the concurrent process.
3. Reduced Costs: With the help of serializable property, we can reduce the cost of the
hardware that is being used for the smooth operation of the database. It can also
reduce the development cost of the software.
4. Increased Performance:In some cases, serializable executions can perform better than
their non-serializable counterparts since they allow the developer to optimize their
code for performance.
Conclusion
DBMS transactions must follow the ACID properties to be considered serializable.
There are different types of serializability in DBMS, each with its own benefits and
drawbacks. In most cases, selecting the right type of serializability will come down to
a trade-off between performance and correctness.
Selecting the wrong type of serializability can lead to errors in your database that can
be difficult to debug and fix. Hopefully, this guide has given you a better
understanding of how Serializability in DBMS works and what kinds of serializability
exist.
DBMS Concurrency Control
Concurrency Control is the management procedure that is required for controlling
concurrent execution of the operations that take place on a database.
But before knowing about concurrency control, we should know about concurrent
execution.
For example:
Consider the below diagram where two transactions T X and TY, are performed on
the same account A where the balance of account A is $300.
o At time t1, transaction TX reads the value of account A, i.e., $300 (only read).
o At time t2, transaction TX deducts $50 from account A that becomes $250 (only
deducted and not updated/write).
o Alternately, at time t3, transaction TY reads the value of account A that will be $300
only because TX didn't update the value yet.
o At time t4, transaction TY adds $100 to account A that becomes $400 (only added but
not updated/write).
o At time t6, transaction TX writes the value of account A that will be updated as $250
only, as TY didn't update the value yet.
o Similarly, at time t7, transaction TY writes the values of account A, so it will write as
done at time t4 that will be $400. It means the value written by T X is lost, i.e., $250 is
lost.
For example:
Thus, in order to maintain consistency in the database and avoid such problems that
take place in concurrent execution, management is needed, and that is where the
concept of Concurrency Control comes into role.
Concurrency Control
Concurrency Control is the working concept that is required for controlling and
managing the concurrent execution of database operations and thus avoiding the
inconsistencies in the database. Thus, for maintaining the concurrency of the database,
we have the concurrency control protocols.
Thus, in order to maintain consistency in the database and avoid such problems that
take place in concurrent execution, management is needed, and that is where the
concept of Concurrency Control comes into role.
Concurrency Control
Concurrency Control is the working concept that is required for controlling and
managing the concurrent execution of database operations and thus avoiding the
inconsistencies in the database. Thus, for maintaining the concurrency of the database,
we have the concurrency control protocols.
Locking in DBMS
Locking protocols are used in database management systems as a means of
concurrency control. Multiple transactions may request a lock on a data item
simultaneously. Hence, we require a mechanism to manage the locking requests
made by transactions. Such a mechanism is called as Lock Manager. It relies
on the process of message passing where transactions and lock manager
exchange messages to handle the locking and unlocking of data items. Data
structure used in Lock Manager – The data structure required for
implementation of locking is called as Lock table.
1. It is a hash table where name of data items are used as hashing index.
2. Each locked data item has a linked list associated with it.
3. Every node in the linked list represents the transaction which requested
for lock, mode of lock requested (mutual/exclusive) and current status
of the request (granted/waiting).
4. Every new lock request for the data item will be added in the end of
linked list as a new node.
5. Collisions in hash table are handled by technique of separate chaining.
Consider the following example of lock
table:
Explanation: In the above figure, the locked data items present in lock table
are 5, 47, 167 and 15. The transactions which have requested for lock have been
represented by a linked list shown below them using a downward arrow. Each
node in linked list has the name of transaction which has requested the data item
like T33, T1, T27 etc. The colour of node represents the status i.e. whether lock
has been granted or waiting. Note that a collision has occurred for data item 5
and 47. It has been resolved by separate chaining where each data item belongs
to a linked list. The data item is acting as header for linked list containing the
locking request. Working of Lock Manager –
1. Initially the lock table is empty as no data item is locked.
2. Whenever lock manager receives a lock request from a transaction
Ti on a particular data item Q i following cases may arise:
If Q i is not already locked, a linked list will be created and
lock will be granted to the requesting transaction T i.
If the data item is already locked, a new node will be added
at the end of its linked list containing the information about
request made by Ti.
3. If the lock mode requested by T i is compatible with lock mode of
transaction currently having the lock, T i will acquire the lock too and
status will be changed to ‘granted’. Else, status of T i’s lock will be
‘waiting’.
4. If a transaction Ti wants to unlock the data item it is currently
holding, it will send an unlock request to the lock manager. The lock
manager will delete Ti’s node from this linked list. Lock will be
granted to the next transaction in the list.
5. Sometimes transaction Ti may have to be aborted. In such a case all
the waiting request made by T i will be deleted from the linked lists
present in lock table. Once abortion is complete, locks held by T i will
also be released.
Advantages:
Disadvantages:
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.
The above 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 already committed.
So this type of schedule is known as irrecoverable schedule.
The above 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.
The above Table 3 shows a schedule with two transactions. Transaction T1 reads and
write A and commits, and that value is read and written by T2. So this is a cascade less
recoverable schedule.
Multiple Granularity
Let's start by understanding the meaning of granularity.
Multiple Granularity:
o It can be defined as hierarchically breaking up the database into blocks which can be
locked.
o The Multiple Granularity protocol enhances concurrency and reduces lock overhead.
o It maintains the track of what to lock and how to lock.
o It makes easy to decide either to lock a data item or to unlock a data item. This type of
hierarchy can be graphically represented as a tree.
In the above diagram, resource 1 and resource 2 have single instances. There is a
cycle R1 → P1 → R2 → P2. So, Deadlock is Confirmed.
ADVANTAGES OR DISADVANTAGES:
Stored Procedure
A stored procedure is a group of pre-compiled SQL statements (prepared SQL code) that can
be reused again and again.
They can be used to perform a wide range of database operations such as inserting, updating,
or deleting data, generating reports, and performing complex calculations. Stored procedures
are very useful because they allow you to encapsulate (bundle) a set of SQL statements as a
single unit and execute them repeatedly with different parameters, making it easy to manage
and reuse code.
Procedures have similar structure as functions; they accept parameters and perform
operations when we call them; but the difference between them is that SQL stored procedures
are simpler to write or create, whereas functions have a more rigid structure and support fewer
clauses and functionality.
Syntax
The basic syntax to create a stored procedure in SQL is as follows −
CREATE PROCEDURE procedure_name
@parameter1 datatype,
@parameter2 datatype
AS
BEGIN
-- SQL statements to be executed
END
Where,
The CREATE PROCEDURE statement is used to create the procedure. After
creating the procedure, we can define any input parameters that the procedure
may require. These parameters are preceded by the '@' symbol and followed by
their respective data types.
The AS keyword is used to begin the procedure definition. The SQL statements
that make up the procedure are placed between the BEGIN and END keywords.
Creating a Procedure
We can create a stored procedure using the CREATE PROCEDURE statement in SQL.
Following are the simple steps for creating a stored procedure −
Choose a name for the procedure.
Write the SQL code for the procedure, including to create the procedure in SQL
Server.
We can then test the stored procedure by executing it with different input
parameters.
Example
To understand it better let us consider the CUSTOMERS table which contains the personal
details of customers including their name, age, address and salary etc. as shown below −
CREATE TABLE CUSTOMERS (
);
Now insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
@CutomerAge INT
AS
BEGIN
SELECT * FROM CUSTOMERS
END
Output
This would produce the following result −
Commands completed successfully.
Verification
To verify the changes, once we have created the stored procedure, we can test it by executing
it with different input parameters as shown in the query below −
EXEC GetCustomerInfo @CutomerAge = 25
This will return all columns from the CUSTOMERS table where the cutomer’s age is 25.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+
SQL Trigger
Trigger: A trigger is a stored procedure in database which automatically invokes
whenever a special event in the database occurs. For example, a trigger can be
invoked when a row is inserted into a specified table or when certain table columns
are being updated.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of syntax:
1. create trigger [trigger_name]: Creates or replaces an existing trigger with
the trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. on [table_name]: This specifies the name of the table associated with the
trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected.
6. [trigger_body]: This provides the operation to be performed as trigger is
fired
BEFORE 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.
Example:
Given Student Report Database, in which student marks assessment is recorded. In
such schema, create a trigger so that the total and percentage of specified marks is
automatically inserted whenever a record is insert.
Here, as trigger will invoke before record is inserted so, BEFORE Tag can be used.
Suppose the database Schema –
mysql> desc Student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| tid | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| subj1 | int(2) | YES | | NULL | |
| subj2 | int(2) | YES | | NULL | |
| subj3 | int(2) | YES | | NULL | |
| total | int(3) | YES | | NULL | |
| per | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
SQL Trigger to problem statement.
create trigger stud_marks
before INSERT
on
Student
for each row
set Student.total = Student.subj1 + Student.subj2 + Student.subj3,
Student.per = Student.total * 60 / 100;
Above SQL statement will create a trigger in the student database in which
whenever subjects marks are entered, before inserting this data into the database,
trigger will compute those two values and insert with the entered values. i.e.,
mysql> insert into Student values(0, "ABCDE", 20, 20, 20, 0, 0);
Query OK, 1 row affected (0.09 sec)