DBMS Unit 3
DBMS Unit 3
Here is a step-by-step explanation of database triggers, its type, and syntax in DBMS:
A trigger in a database management system (DBMS) is a powerful tool that allows you to
automate certain actions or tasks when specific events occur. These events can be
specific actions, such as inserting, updating, or deleting data in a table. Triggers are an
essential concept in DBMS, and understanding them is crucial for data integrity and
efficient database management.
Types of Triggers:
There are two primary types of triggers:
1. Before Triggers: These triggers are executed before the triggering event, allowing you
to modify data before it is committed to the database.
2. After Triggers: These triggers are executed after the triggering event, allowing you to
perform actions after the data has been committed.
Syntax:
The syntax for triggers varies depending on the DBMS being used. Here is a general
syntax for creating a trigger:
trigger code
END;
Here:
" Logging: A DBMS keeps a log of all transactions to ensure recoverability. The log
consists of data about all adjustments made to the database, as well as the
transactions that made those changes. In the event of a failure, the log may be
used to repair the database to a consistent state.
Checkpointing: Acheckpoint is a point in time in which the DBMS records the
state of the database and logs it. This allows for faster recovery in case of a
failure, as only the transactions since the last checkpoint need to be rolled back or
replayed.
" Media Recovery: Media recovery refers to the ability of a DBMS to recover from a
failure that affects the storage media, such as a hard disk crash. This includes
restoring the database from a backup and using the log to bring it up to date.
Functional dependency is a relationship between two attributes in a relation, where
the value of one attribute determines the value of another attribute. It is denoted by
an arrow " The functional dependency of Aon Bis represented by A’B, where
A and B are the attributes of the relation.
Example:
Consider two transactions T1 and T2:
Time T1 T2
R1
1 W1
2 R2
3 W2
Time T1 T2T3
R1
1 W1 R2
2 W2 R3
3 W3
In this schedule, T1, T2, and T3 execute concurrently, and their operations overlap.
3. Recoverable Schedule
A recoverable schedule is a schedule that allows for the recovery of the database to
a consistent state after a transaction failure.
Example:
Time T1 T2
R1
1 W1
2 R2
3 W2
4. Non-Recoverable Schedule
Example:
Time T1 T2
R1
1 W1 R2
2 W2
In this schedule, T1 updates the database and then T2 reads the updated data,
making it non-recoverable.
5. Serializable Schedule
R1
1 W1
2 R2
3 W2
In this schedule, T1 and T2 execute concurrently, but the outcome is equivalent toa
serial schedule.
6. Non-Serializable Schedule
Example:
Time T1 T2
R1
1 W1 R2
2 W2
In this schedule, T1updates the database and then T2 reads the updated data,
making it non-serializable.
Database programming control structures exception handling stored
procedures triggers in dbms
Control Structures
Follow up
Control structures in database programming are used to control the flow of a
program. They are essential for managing the execution of database operations and What are the differences between
ensuring that the program runs efficiently. The most common control structures stored procedures and functions in a
used in database programming are: database?
" Conditional Statements: These statements allow you to execute different blocks
of code based on certain conditions. For example, you can use an IF-THEN How do stored procedures and triggers
statement to execute a block of code if a certain condition is true. impact database performance and
scalability?
Loops: These statements allow you to execute a block of code repeatedly for a
specified number of times. For example, you can use a WHILE loop to execute a
block of code until a certain condition is met. What are some best practices for
writing efficient and readable SQL
" Switch Statements: These statements allow you to execute different blocks of
queries in a database?
code based on the value of a variable. For example, you can use a sWITCH
statement to execute a block of code based on the value of a variable.
Exception Handling
Exception handling is an essential part of database programming. It allows you to
handle errors and exceptions that occur during the execution of a program. The
most common exception handling mechanisms used in database programming are:
" TRY-CATCH Blocks: These blocks allow you to catch and handle exceptions that
occur during the execution of a program. For example, you can use a TRY-CATCH
block to catch an exception that occurs when a database query fails.
ERROR HANDLING: This is a mechanism that allows you to handle errors and
exceptions that occur during the execution of a program. For example, you can
use error handling to catch and handle errors that occur when a database query
fails.
Stored Procedures
" Improve Security: Stored procedures can improve security by reducing the risk of
SQL injection attacks.
" Simplify Code: Stored procedures can simplify code by encapsulating complex
logic and reducing the amount of code that needs to be written.
Triggers
Triggers are stored procedures that are automatically executed in response to
certain events. They can be used to:
" Enforce Business Rules: Triggers can be used to enforce business rules and
ensure that data is consistent and accurate.
" Improve Data Integrity: Triggers can be used to improve data integrity by ensuring
that data is updated correctly and consistently.
" Simplify Code: Triggers can simplify code by encapsulating complex logic and
reducing the amount of code that needs to be written.
" Improved Performance: Stored procedures and triggers can improve performance
by reducing the amount of data that needs to be transferred between the client
and the server.
" Improved Security: Stored procedures and triggers can improve security by
reducing the risk of SQL injection attacks.
Simplified Code: Stored procedures and triggers can simplify code by
encapsulating complex logic and reducing the amount of code that needs to be
written.
" Improved Data Integrity: Stored procedures and triggers can improve data
integrity by ensuring that data is updated correctly and consistently.
Transaction states, Serializability
In a database management system (DBMS), a transaction goes through several
states duing its lifetime. These states govern the rules that decide the fate of the
transaction, whether it will commit or abort. The main transaction states are:
Serializability
Serializability is a concept in DBMS that ensures the corectness of concurrent
transactions. It guarantees that the outcome of multiple transactions executed
concurrently is the same as if they were executed serially, one after the other. In
other words, serializability ensures that the database remains in a consistent state,
even when multiple transactions are executed simultaneously.
There are two types of serializability: