Assignment No 02
Assignment No 02
Normalization is a database design technique that organizes tables in a way that reduces
redundancy and dependency of data.
It divides large tables into smaller tables and links them using relationships. The main goal
of normalization is to decompose
complex relations into simpler ones to ensure that the database is efficient and easy to
manage.
A delete anomaly occurs when the deletion of data leads to the unintended loss of other valuable
data. This happens when related pieces of data are stored in the same table.
An update anomaly occurs when a change to data in one place requires changes in multiple
places to maintain consistency. If all places are not updated properly, the data becomes
m_ssn change
inconsistent.
03. Explain the 1NF, 2NF, 3NF with example.
In 1NF, the table should be broken down so that each value is atomic:
Students Table:
StudentID | Name
----------------
1 | Alice
2 | Bob
Courses Table:
StudentID | Course
-------------------
1 | Math
1 | Science
2 | English
Students Table:
StudentID | Name
----------------
1 | Alice
2 | Bob
Courses Table:
Course | Instructor
----------------------
Math | Dr. Smith
Science | Dr. Johnson
English | Dr. Clark
Enrollments Table:
StudentID | Course
-------------------
1 | Math
1 | Science
2 | English
04. Explain Informal Design guidelines for relation schema design. Examine the
Cursor with PGM.
1. Clear Attribute Semantics: Ensure each attribute has a clear meaning and purpose.
Attributes should be understandable and should represent only one concept.
2. Reduce Redundancy: Minimize redundant data storage to avoid anomalies and ensure
efficient updates. Use normalization to split tables as necessary.
3. Avoid NULLs: Design tables to minimize the use of NULL values, as they can lead to
ambiguous interpretations and require special handling.
4. Ensure Referential Integrity: Use foreign keys to maintain consistency between related
tables. This ensures that relationships between tables are valid and consistent.
Cursor in SQL:
A cursor is a database object that allows row-by-row processing of the result set. Cursors
are useful when you need to perform operations on each row individually.
Assertion:
An assertion is a condition specified in SQL that must always be true for the database.
However, not all database systems support assertions directly.
Instead, similar constraints can be implemented using triggers or check constraints.
Trigger:
A trigger is a set of SQL statements that automatically execute when a specified event occurs
in the database, such as an insert, update, or delete operation.
Example of a Trigger:
```sql
CREATE TABLE audit_log (
log_id INT PRIMARY KEY,
student_id INT,
action VARCHAR(50),
action_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
A debit/credit transaction involves transferring funds between two accounts, ensuring that
the total amount remains consistent.
2. Debit Account A:
```sql
UPDATE accounts
SET balance = balance - 100
WHERE account_id = 'A';
```
3. Credit Account B:
```sql
UPDATE accounts
SET balance = balance + 100
WHERE account_id = 'B';
```
4. Commit Transaction:
```sql
COMMIT;
```
State of Transaction:
- Active: The transaction is in progress and operations are being performed.
- Partially Committed: All operations are complete, but changes are not yet saved to the
database.
- Committed: The transaction is successfully completed, and all changes are permanently
saved.
- Failed: The transaction encountered an error, and the operations cannot proceed.
- Aborted: The transaction is rolled back, and all changes made during the transaction are
undone.
Transaction Diagram:
```plaintext
+-------------------------+
| Start Transaction |
+-------------------------+
|
V
+-------------------------+
| Debit Account A |
+-------------------------+
|
V
+-------------------------+
| Credit Account B |
+-------------------------+
|
V
+-------------------------+
| Commit Transaction |
+-------------------------+
|
V
+-------------------------+
| End Transaction |
+-------------------------+
```
The diagram represents the flow of a debit/credit transaction, showing the sequence of
operations from starting the transaction to committing and ending it.