Database Design and ER Model_copy
Database Design and ER Model_copy
Introduction to Transactions
A transaction in a database is a sequence of operations that perform a single logical function
in a database application. It consists of one or more SQL statements, such as INSERT,
UPDATE, DELETE, or SELECT, that must be executed in a way that ensures consistency,
correctness, and reliability. Transactions are crucial for maintaining database integrity,
particularly in multi-user environments where multiple transactions may occur
simultaneously. A well-implemented transaction management system ensures that even if a
failure occurs (such as a system crash, power outage, or software error), the database remains
in a consistent state.
For example, consider a banking system where a user transfers ₹10,000 from Account A to
Account B. This transaction involves two operations: first, deducting ₹10,000 from Account
A, and second, adding ₹10,000 to Account B. If both operations are successfully executed,
the transaction is committed, meaning the changes are permanently saved in the database.
However, if any issue arises during the execution (e.g., a system failure after deducting the
amount but before crediting it), the transaction must be rolled back, undoing the deduction to
ensure that no inconsistent state exists.
1. Atomicity ensures that a transaction is treated as a single unit of work, which either
completes entirely or does not occur at all. If any part of the transaction fails, all
changes made during the transaction are undone, restoring the database to its original
state. For instance, in the banking example, if the credit operation to Account B fails,
the deduction from Account A must also be reversed.
2. Consistency ensures that a database transitions from one valid state to another valid
state after a transaction. It prevents any operation from leaving the database in an
incorrect or corrupt state. For example, if a sales transaction updates both the
inventory and the revenue records, both changes must be executed correctly to
maintain database consistency.
3. Isolation ensures that concurrent transactions do not interfere with each other. When
multiple transactions execute simultaneously, they must be isolated to prevent
conflicts. Consider an e-commerce website where two users attempt to purchase the
last item in stock at the same time—without proper isolation, both transactions might
succeed, leading to an incorrect inventory count.
4. Durability guarantees that once a transaction is committed, its effects are permanently
stored in the database, even in the event of a system crash. For instance, once an
airline ticket is booked and payment is processed, the reservation must persist in the
database, even if the system crashes immediately after the confirmation.
Transaction States
A transaction progresses through multiple states during its execution:
1. Active State – The transaction starts and performs operations such as data retrieval or
modification.
2. Partially Committed State – The transaction has completed all its operations but has
not yet been permanently saved to the database.
3. Committed State – The transaction has successfully completed, and all changes are
made permanent.
4. Failed State – If an error occurs before a transaction commits, it enters the failed state.
5. Aborted State (Rollback) – The system undoes all changes made by the failed
transaction and restores the database to its previous consistent state.
For example, in an online shopping system, when a customer adds items to their cart and
proceeds to checkout, a transaction starts. If the payment succeeds, the transaction commits,
confirming the order. However, if the payment fails, the transaction rolls back, ensuring the
order is not placed incorrectly.
● Lost Updates occur when two transactions modify the same data simultaneously, and
one update is lost. Suppose two users attempt to update a product’s stock at the same
time—without proper control, one update may overwrite the other.
● Dirty Reads happen when a transaction reads uncommitted changes made by another
transaction. For example, if a finance system allows users to view their updated salary
before it is officially processed, incorrect data may be displayed if the salary update is
later rolled back.
● Non-Repeatable Reads occur when a transaction reads the same data twice, but
another transaction modifies it between reads, leading to inconsistencies.
● Phantom Reads occur when a transaction retrieves a set of records multiple times, but
another transaction inserts or deletes rows before the second read, causing different
results.
Deadlocks in Transactions
A deadlock occurs when two or more transactions wait indefinitely for each other to release
resources.
Since both transactions are waiting for each other to release locks, neither can proceed,
causing a deadlock.
Transaction Recovery
Transaction recovery ensures that a database remains consistent even in case of failures.
There are several types of failures:
● Deferred Update (No Undo, Only Redo) – Changes are not applied until the
transaction commits, ensuring that only committed transactions are saved.
● Immediate Update (Undo & Redo) – Changes are applied immediately but can be
undone if a transaction fails.
● Checkpointing – Periodic backups of the database state to speed up recovery after a
failure.
For example, if a hotel booking system crashes, checkpointing ensures that only transactions
recorded before the last checkpoint are reprocessed, reducing recovery time.
2. Weak Entities – Entities that depend on another entity for identification.
o Example:
📌 Representation in ER Diagrams:
● Double Rectangle → Weak Entity.
● Double Diamond → Identifying Relationship.
In the example below, school is a strong entity because it has a primary key attribute - school
number. Unlike school, the classroom is a weak entity because it does not have any primary
key and the room number here acts only as a discriminator.
A system must qualify as a relational database and use relational principles to manage
data. If a system doesn’t follow relational principles, it cannot be considered an
RDBMS.
All information must be stored in tables, including metadata (data about data). This
ensures that everything, including table structures and relationships, is represented in
📌
a structured format.
Example: A student database stores all student details in a "Students" table instead
of using separate files.
Each data element must be accessible via a unique combination of table name,
📌
primary key, and column name.
Example: A bank system retrieves a customer’s balance using the Account_ID
and Balance column instead of searching through unstructured records.
A relational database must have an online data dictionary (catalog) that is accessible
📌
using standard query language (SQL).
Example: In SQL databases, users can query system tables like
INFORMATION_SCHEMA.TABLES to retrieve metadata about tables.
The database must support a single, consistent language for data definition,
📌
manipulation, and control, such as SQL.
Example: SQL allows users to create tables (CREATE TABLE), insert data
(INSERT INTO), retrieve data (SELECT), and enforce security (GRANT/REVOKE).
📌
Views (virtual tables) must be updatable like base tables, ensuring data consistency.
Example: A view showing employee details should allow updates that reflect in
the underlying "Employees" table.
📌
rows at once instead of handling records individually.
Example: A university database should allow enrolling multiple students in a
course with a single INSERT query rather than inserting each record separately.
📌
The database’s logical structure must remain unaffected by physical storage changes.
Example: Moving a database from an HDD to an SSD should not require
rewriting queries or modifying application logic.
📌
applications that access the database.
Example: Adding a "Middle_Name" column in a "Customers" table should not
break existing queries fetching "First_Name" and "Last_Name."
● Rule 10: Integrity Independence
Integrity constraints (like primary keys, foreign keys, and data validation rules) must
📌
be stored in the database and not in the application code.
Example: A banking system should enforce "Balance cannot be negative" at the
database level instead of relying on application logic.
The database should work the same way, whether data is stored in a single location
📌
(centralized database) or multiple locations (distributed database).
Example: An airline reservation system with databases across multiple cities
should function seamlessly, regardless of where a user accesses it.
The database must prevent bypassing security rules through low-level access
📌
methods. All operations must go through the defined relational model.
Example: A user should not be able to bypass access controls by directly
modifying data files instead of using authorized SQL queries.
Codd’s 12 rules provide a foundation for designing relational database systems that are
efficient, scalable, and consistent. While no RDBMS follows all 12 rules perfectly, modern
relational databases like MySQL, PostgreSQL, Oracle, and SQL Server adhere to most of
these principles. Following these rules ensures data integrity, security, and ease of access,
making relational databases the standard for data management across industries.