<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: LOKHITHA S</title>
    <description>The latest articles on Forem by LOKHITHA S (@lokhitha_s_4).</description>
    <link>https://forem.com/lokhitha_s_4</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3458763%2Fde092448-f6d2-46ae-bd2c-a053c53e6e5e.png</url>
      <title>Forem: LOKHITHA S</title>
      <link>https://forem.com/lokhitha_s_4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lokhitha_s_4"/>
    <language>en</language>
    <item>
      <title>Transactions, Deadlocks &amp; Log Based Recovery</title>
      <dc:creator>LOKHITHA S</dc:creator>
      <pubDate>Thu, 09 Oct 2025 01:31:13 +0000</pubDate>
      <link>https://forem.com/lokhitha_s_4/transactions-deadlocks-log-based-recovery-50io</link>
      <guid>https://forem.com/lokhitha_s_4/transactions-deadlocks-log-based-recovery-50io</guid>
      <description>&lt;p&gt;Managing database transactions safely is crucial to ensure data integrity and prevent issues like partial updates or deadlocks. In this tutorial, we’ll explore transactions, deadlocks, and log-based recovery using a simple Accounts table.&lt;/p&gt;

&lt;p&gt;First, let’s create a sample table&lt;br&gt;
CREATE TABLE Accounts (&lt;br&gt;
acc_no INT PRIMARY KEY,&lt;br&gt;
name VARCHAR(50),&lt;br&gt;
balance INT&lt;br&gt;
);&lt;/p&gt;

&lt;p&gt;INSERT INTO Accounts VALUES&lt;br&gt;
(1, 'Alice', 1000),&lt;br&gt;
(2, 'Bob', 1500),&lt;br&gt;
(3, 'Charlie', 2000);&lt;/p&gt;

&lt;p&gt;*Transaction – Atomicity &amp;amp; Rollback&lt;br&gt;
*&lt;br&gt;
Suppose we want to transfer 500 from Alice to Bob. Using a transaction ensures that either both operations succeed or none.&lt;/p&gt;

&lt;p&gt;START TRANSACTION;&lt;/p&gt;

&lt;p&gt;UPDATE Accounts&lt;br&gt;
SET balance = balance - 500&lt;br&gt;
WHERE name = 'Alice';&lt;/p&gt;

&lt;p&gt;ROLLBACK;&lt;br&gt;
UPDATE Accounts&lt;br&gt;
SET balance = balance + 500&lt;br&gt;
WHERE name = 'Bob';&lt;/p&gt;

&lt;p&gt;Deadlock Simulation&lt;/p&gt;

&lt;p&gt;Deadlocks occur when two transactions block each other waiting for resources.&lt;/p&gt;

&lt;p&gt;START TRANSACTION;&lt;br&gt;
SELECT * FROM Accounts WHERE name='Alice' FOR UPDATE;&lt;br&gt;
UPDATE Accounts SET balance = balance + 100 WHERE name='Bob';&lt;/p&gt;

&lt;p&gt;START TRANSACTION;&lt;br&gt;
SELECT * FROM Accounts WHERE name='Bob' FOR UPDATE;&lt;br&gt;
UPDATE Accounts SET balance = balance - 100 WHERE name='Alice';RESULT&lt;br&gt;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction&lt;/p&gt;

&lt;p&gt;Log-Based Recovery&lt;/p&gt;

&lt;p&gt;Modern DBMS (MySQL/PostgreSQL) automatically maintain transaction logs. These logs help undo changes if a transaction fails.&lt;/p&gt;

&lt;p&gt;UPDATE Accounts SET balance = balance - 300 WHERE name='Alice';Log-based recovery ensures that undo operations are possible. Even if the database crashes, the system can restore a consistent state.&lt;/p&gt;

&lt;p&gt;Summary&lt;/p&gt;

&lt;p&gt;Transactions guarantee atomicity; either all operations succeed or none.&lt;/p&gt;

&lt;p&gt;Deadlocks occur when transactions block each other; they must be handled with care.&lt;/p&gt;

&lt;p&gt;Log-based recovery ensures durability and recoverability&lt;/p&gt;

</description>
      <category>sql</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Normalization</title>
      <dc:creator>LOKHITHA S</dc:creator>
      <pubDate>Thu, 09 Oct 2025 01:28:00 +0000</pubDate>
      <link>https://forem.com/lokhitha_s_4/normalization-1i0m</link>
      <guid>https://forem.com/lokhitha_s_4/normalization-1i0m</guid>
      <description>&lt;p&gt;Database normalization is the process of organizing data to reduce redundancy and improve data integrity. In this tutorial, we’ll go step-by-step from Unnormalized Table → 1NF → 2NF → 3NF, and implement it using **MySQL.&lt;/p&gt;

&lt;p&gt;We’ll also write a JOIN query to display students along with their courses and instructors.&lt;/p&gt;

&lt;p&gt;Insertion Anomaly: Cannot add a new course without assigning it to a student.&lt;/p&gt;

&lt;p&gt;Update Anomaly: If an instructor’s phone number changes, multiple rows must be updated.&lt;br&gt;
Deletion Anomaly: Removing a student could delete information about the course and instructor. 1.First Normal Form (1NF)&lt;br&gt;
Rule: Each column should have atomic values.&lt;/p&gt;

&lt;p&gt;Our table already satisfies 1NF.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>normalization</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Cursor + Trigger</title>
      <dc:creator>LOKHITHA S</dc:creator>
      <pubDate>Thu, 09 Oct 2025 01:23:01 +0000</pubDate>
      <link>https://forem.com/lokhitha_s_4/cursor-trigger-2fho</link>
      <guid>https://forem.com/lokhitha_s_4/cursor-trigger-2fho</guid>
      <description>&lt;p&gt;Cursor + Trigger — SQL Practice&lt;br&gt;
In this post, we’ll explore two powerful SQL concepts — Cursors and Triggers — with complete examples that you can run directly in Oracle LiveSQL.&lt;/p&gt;

&lt;p&gt;CURSOR — Process Cursor with Condition&lt;br&gt;
Problem Statement:&lt;/p&gt;

&lt;p&gt;Create a cursor that displays employee names whose salary is greater than 50,000 from the Employee table.&lt;br&gt;
CREATE TABLE Employee (&lt;br&gt;
EmpID NUMBER PRIMARY KEY,&lt;br&gt;
EmpName VARCHAR2(100),&lt;br&gt;
Salary NUMBER(10,2)&lt;br&gt;
);&lt;br&gt;
INSERT INTO Employee VALUES (1, 'Ankit', 45000);&lt;br&gt;
INSERT INTO Employee VALUES (2, 'Zeena', 52000);&lt;br&gt;
INSERT INTO Employee VALUES (3, 'Mukesh', 60000);&lt;br&gt;
INSERT INTO Employee VALUES (4, 'Vishal', 48000);&lt;br&gt;
COMMIT;&lt;br&gt;
SET SERVEROUTPUT ON;&lt;/p&gt;

&lt;p&gt;DECLARE&lt;br&gt;
v_EmpName Employee.EmpName%TYPE;&lt;br&gt;
v_Salary Employee.Salary%TYPE;&lt;br&gt;
CURSOR emp_cursor IS&lt;br&gt;
SELECT EmpName, Salary&lt;br&gt;
FROM Employee&lt;br&gt;
WHERE Salary &amp;gt; 50000;&lt;br&gt;
BEGIN&lt;br&gt;
OPEN emp_cursor;&lt;br&gt;
LOOP&lt;br&gt;
FETCH emp_cursor INTO v_EmpName, v_Salary;&lt;br&gt;
EXIT WHEN emp_cursor%NOTFOUND;&lt;br&gt;
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_EmpName || ' | Salary: ' || v_Salary);&lt;br&gt;
END LOOP;&lt;br&gt;
CLOSE emp_cursor;&lt;br&gt;
END;&lt;br&gt;
/&lt;/p&gt;

&lt;p&gt;*TRIGGER *— AFTER INSERT Trigger (Student Table)&lt;br&gt;
Problem Statement:&lt;/p&gt;

&lt;p&gt;Whenever a new student is added to the Students table, automatically insert a log entry into the Student_Audit table to keep track of the registration.&lt;br&gt;
CREATE TABLE Students (&lt;br&gt;
StudentID NUMBER PRIMARY KEY,&lt;br&gt;
StudentName VARCHAR2(100),&lt;br&gt;
Course VARCHAR2(100)&lt;br&gt;
);&lt;br&gt;
CREATE TABLE Student_Audit (&lt;br&gt;
AuditID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,&lt;br&gt;
StudentID NUMBER,&lt;br&gt;
StudentName VARCHAR2(100),&lt;br&gt;
ActionTaken VARCHAR2(100),&lt;br&gt;
ActionDate DATE&lt;br&gt;
);&lt;br&gt;
CREATE OR REPLACE TRIGGER trg_AfterStudentInsert&lt;br&gt;
AFTER INSERT ON Students&lt;br&gt;
FOR EACH ROW&lt;br&gt;
BEGIN&lt;br&gt;
INSERT INTO Student_Audit (StudentID, StudentName, ActionTaken, ActionDate)&lt;br&gt;
VALUES (:NEW.StudentID, :NEW.StudentName, 'New Student Registered', SYSDATE);&lt;br&gt;
END;&lt;br&gt;
INSERT INTO Students (StudentID, StudentName, Course)&lt;br&gt;
VALUES (1, 'hareesh', 'Computer Science');&lt;br&gt;
COMMIT;&lt;br&gt;
SELECT * FROM Student_Audit;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>dbms</category>
      <category>oracle</category>
    </item>
    <item>
      <title>SQL query</title>
      <dc:creator>LOKHITHA S</dc:creator>
      <pubDate>Mon, 25 Aug 2025 18:03:19 +0000</pubDate>
      <link>https://forem.com/lokhitha_s_4/sql-queries-jc1</link>
      <guid>https://forem.com/lokhitha_s_4/sql-queries-jc1</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgi8emob045twkbqf1zfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgi8emob045twkbqf1zfl.png" alt=" " width="372" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
