<?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: Kamwemba Tinashe C</title>
    <description>The latest articles on Forem by Kamwemba Tinashe C (@dartybaby).</description>
    <link>https://forem.com/dartybaby</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%2F3450997%2F6fefe585-ea2e-4f15-8f5e-2279e57a0eaa.png</url>
      <title>Forem: Kamwemba Tinashe C</title>
      <link>https://forem.com/dartybaby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dartybaby"/>
    <language>en</language>
    <item>
      <title>MongoDB</title>
      <dc:creator>Kamwemba Tinashe C</dc:creator>
      <pubDate>Wed, 08 Oct 2025 17:31:21 +0000</pubDate>
      <link>https://forem.com/dartybaby/mongodb-37jf</link>
      <guid>https://forem.com/dartybaby/mongodb-37jf</guid>
      <description>&lt;p&gt;&lt;strong&gt;Schema (Collection: students)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "student_id": "S001",
    "name": "Santhosh",
    "age": 20,
    "department": "CSBS",
    "cgpa": 9.0,
    "year": 2
  },
  {
    "student_id": "S002",
    "name": "Ravi",
    "age": 21,
    "department": "CS",
    "cgpa": 8.5,
    "year": 3
  },
  {
    "student_id": "S003",
    "name": "Priya",
    "age": 19,
    "department": "ECE",
    "cgpa": 7.8,
    "year": 1
  },
  {
    "student_id": "S004",
    "name": "Kiran",
    "age": 22,
    "department": "CS",
    "cgpa": 6.9,
    "year": 4
  },
  {
    "student_id": "S005",
    "name": "Anita",
    "age": 20,
    "department": "MECH",
    "cgpa": 7.2,
    "year": 2
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Read (Query)**&lt;/p&gt;

&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%2Fvrjvxsu1em8w8v1s4r94.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%2Fvrjvxsu1em8w8v1s4r94.png" alt="read query" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;All Students with CGPA &amp;gt; 8&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ cgpa: { $gt: 8 } }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2F0yg4rlt5hwpwjmnsdx9b.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%2F0yg4rlt5hwpwjmnsdx9b.png" alt="cgpa &amp;gt; 8" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Students Belonging to the Computer Science Department:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ department: "CS" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fy2e77ozf2i65bavlpd0n.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%2Fy2e77ozf2i65bavlpd0n.png" alt="from cs department" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Update
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Update the CGPA of a specific student.&lt;/li&gt;
&lt;li&gt;Increase the year of study for all 3rd year students by 1
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ Task 3️ — Update
const { MongoClient } = require("mongodb");

// connection string
const uri = "mongodb+srv://William:4dwQ454VgRgDtNSO@assignment.jfmqse7.mongodb.net/?retryWrites=true&amp;amp;w=majority&amp;amp;appName=Assignment";

async function run() {
    const client = new MongoClient(uri);

    try {
        await client.connect();
        console.log("Connected to MongoDB Atlas");

        const db = client.db("Assign6");
        const students = db.collection("students");

        // 1️ Update CGPA of a specific student (e.g., student_id = S002)
        const updateCGPAResult = await students.updateOne(
            { student_id: "S002" }, // filter
            { $set: { cgpa: 9.0 } } // new CGPA
        );
        console.log(`Updated CGPA for S002 — modifiedCount: ${updateCGPAResult.modifiedCount}`);

        // 2️ Increase year of all 3rd-year students by 1
        const increaseYearResult = await students.updateMany(
            { year: 3 }, // filter
            { $inc: { year: 1 } } // increment year by 1
        );
        console.log(`Increased year for ${increaseYearResult.modifiedCount} student(s)`);

        // Display updated collection
        const updatedStudents = await students.find().toArray();
        console.log("\nUpdated Students Collection:");
        console.table(updatedStudents.map(s =&amp;gt; ({
            student_id: s.student_id,
            name: s.name,
            year: s.year,
            cgpa: s.cgpa
        })));

    } catch (err) {
        console.error("Error:", err);
    } finally {
        await client.close();
        console.log("Connection closed.");
    }
}

run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Feg3thckme79hm9zqzdgn.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%2Feg3thckme79hm9zqzdgn.png" alt=" " width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Delete
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Delete one student record by student_id.&lt;/li&gt;
&lt;li&gt;Delete all students having CGPA &amp;lt; 7.5.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { MongoClient } = require("mongodb");

const uri = "mongodb+srv://William:4dwQ454VgRgDtNSO@assignment.jfmqse7.mongodb.net/?retryWrites=true&amp;amp;w=majority&amp;amp;appName=Assignment";

async function run() {
  const client = new MongoClient(uri);

  try {
    await client.connect();
    console.log("Connected to MongoDB Atlas");

    const db = client.db("Assign6");
    const students = db.collection("students");

    // Delete one student record by student_id (example: S004)
    const deleteOneResult = await students.deleteOne({ student_id: "S004" });
    console.log("Deleted student S004 — deletedCount:", deleteOneResult.deletedCount);

    // Delete all students having CGPA &amp;lt; 7.5
    const deleteManyResult = await students.deleteMany({ cgpa: { $lt: 7.5 } });
    console.log("Deleted students with CGPA &amp;lt; 7.5 — deletedCount:", deleteManyResult.deletedCount);

    // Display remaining collection
    const remainingStudents = await students.find().toArray();
    console.log("Remaining Students Collection:");
    console.table(remainingStudents.map(s =&amp;gt; ({
      student_id: s.student_id,
      name: s.name,
      year: s.year,
      cgpa: s.cgpa
    })));

  } catch (err) {
    console.error("Error:", err);
  } finally {
    await client.close();
    console.log("Connection closed.");
  }
}

run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fn3vfppgem94u22jv0b5b.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%2Fn3vfppgem94u22jv0b5b.png" alt=" " width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Indexing, Hashing &amp; Query Optimization</title>
      <dc:creator>Kamwemba Tinashe C</dc:creator>
      <pubDate>Tue, 07 Oct 2025 17:45:41 +0000</pubDate>
      <link>https://forem.com/dartybaby/indexing-hashing-query-optimization-3kan</link>
      <guid>https://forem.com/dartybaby/indexing-hashing-query-optimization-3kan</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%2Fkkk7vsb8yehffqpvg7vd.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%2Fkkk7vsb8yehffqpvg7vd.png" alt="Question to Indexing, Hashing &amp;amp; Query Optimization " width="615" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup the Schema and Data
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Create the Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Students (
    roll_no NUMBER PRIMARY KEY,
    name VARCHAR2(50),
    dept VARCHAR2(50),
    cgpa NUMBER
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Insert at Least 20 Sample Records:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (101, 'Alice', 'CSBS', 8.5);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (102, 'Bob', 'ECE', 7.8);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (103, 'Charlie', 'CSBS', 9.0);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (104, 'David', 'MECH', 6.5);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (105, 'Eve', 'CSBS', 8.2);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (106, 'Frank', 'ECE', 7.9);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (107, 'Grace', 'CSBS', 8.7);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (108, 'Hank', 'MECH', 6.8);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (109, 'Ivy', 'CSBS', 9.1);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (110, 'Jack', 'ECE', 7.6);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (111, 'Kate', 'CSBS', 8.4);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (112, 'Leo', 'MECH', 6.9);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (113, 'Mia', 'CSBS', 8.9);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (114, 'Noah', 'ECE', 7.7);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (115, 'Olivia', 'CSBS', 8.3);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (116, 'Peter', 'MECH', 6.6);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (117, 'Quinn', 'CSBS', 9.2);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (118, 'Rose', 'ECE', 7.5);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (119, 'Sam', 'CSBS', 8.6);
INSERT INTO Students (roll_no, name, dept, cgpa) VALUES (120, 'Tom', 'MECH', 6.7);
COMMIT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Verify Data&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Students;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fuhlto5edgsr9towyzj36.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%2Fuhlto5edgsr9towyzj36.png" alt="Table Of All Students" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task 1: Create a B-Tree Index on the roll_no Column&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX idx_roll_no ON Students(roll_no);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 2: Execute a Query to Fetch Details of a Student with roll_no = 110&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Students WHERE roll_no = 110;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2F1tgqf434aey4sz3g2z8j.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%2F1tgqf434aey4sz3g2z8j.png" alt="Output For Jack" width="800" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task 3: Create a B+ Tree Index on the &lt;em&gt;cgpa&lt;/em&gt; Column&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Oracle uses B-Tree indexes by default, and B+ Tree is a variant not explicitly created but functionally similar. Create a standard B-Tree index:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX idx_cgpa ON Students(cgpa);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 4: Write a Query to Display All Students with cgpa &amp;gt; 8.0&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Students WHERE cgpa &amp;gt; 8.0 ORDER BY cgpa;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fdiqijym5qrcy66omnjne.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%2Fdiqijym5qrcy66omnjne.png" alt=" Students WHERE cgpa &amp;gt; 8.0 " width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task 5: Create a Hash Index on the dept Column&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Oracle doesn’t support direct hash indexes, but you can simulate this with a function-based index using a hash function (e.g., STANDARD_HASH):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX idx_dept_hash ON Students(STANDARD_HASH(dept));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 6: Run a Query to Retrieve All Students from the 'CSBS' Department&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Students WHERE dept = 'CSBS' ORDER BY roll_no;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fk0cxdwu4czrx7138ahgt.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%2Fk0cxdwu4czrx7138ahgt.png" alt="Students WHERE dept = 'CSBS' ORDER BY roll_no" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
    </item>
    <item>
      <title>Transactions, Deadlocks &amp; Log Based Recovery</title>
      <dc:creator>Kamwemba Tinashe C</dc:creator>
      <pubDate>Tue, 07 Oct 2025 16:42:41 +0000</pubDate>
      <link>https://forem.com/dartybaby/transactions-deadlocks-log-based-recovery-1feh</link>
      <guid>https://forem.com/dartybaby/transactions-deadlocks-log-based-recovery-1feh</guid>
      <description>&lt;h2&gt;
  
  
  1. Create the Table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Accounts (
    acc_no INT PRIMARY KEY,
    name VARCHAR(50),
    balance INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Insert Initial Data
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Accounts (acc_no, name, balance) VALUES
(1, 'Alice', 1000),
(2, 'Bob', 1500),
(3, 'Charlie', 2000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fnlqiheygoscma90edie0.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%2Fnlqiheygoscma90edie0.png" alt=" " width="726" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Transaction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Start the Transaction&lt;/strong&gt; (implicitly starts with the first DML):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No explicit &lt;strong&gt;START TRANSACTION&lt;/strong&gt; needed; the transaction begins with the first &lt;strong&gt;UPDATE&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Debit from Alice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance - 500 WHERE name = 'Alice';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;credit to Bob&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance + 500 WHERE name = 'Bob';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rollback the Transaction:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROLLBACK;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check Balances to Confirm No Changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Accounts;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;/p&gt;

&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%2Fi96ilhsdrqevb3cqyawv.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%2Fi96ilhsdrqevb3cqyawv.png" alt=" " width="780" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deadlock Simulation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Oracle supports row-level locking, which we can use to simulate a deadlock. Open two SQL*Plus sessions.
&lt;strong&gt;Session 1 (Window 1):&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lock Alice's account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance WHERE name = 'Alice';
-- Don't commit yet; leave it locked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Try to update Bob's account (run this after starting Session 2):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance WHERE name = 'Bob';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Session 2 (Window 2):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lock Bob's account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance WHERE name = 'Bob';
-- Don't commit yet; leave it locked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try to update Alice's account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance WHERE name = 'Alice';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;At this point, Session 1 waits for Session 2's lock on Bob, and Session 2 waits for Session 1's lock on Alice, causing a deadlock. Oracle will detect this and roll back one transaction, raising an error like:&lt;/li&gt;
&lt;/ul&gt;

&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%2F280ed5pzo49fm7ccglo1.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%2F280ed5pzo49fm7ccglo1.png" alt=" " width="795" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 1: Enable Logging&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logging is enabled by default in Oracle via redo logs and undo tablespaces. You don’t need to enable it manually unless it’s been disabled (unlikely in a standard setup). To confirm an undo tablespace exists (without DBA_TABLESPACES), try:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT tablespace_name FROM USER_TABLESPACES WHERE tablespace_name LIKE '%UNDO%';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Start a Transaction and Update a Record&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Accounts SET balance = balance + 100 WHERE name = 'Charlie';
-- Transaction starts implicitly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Rollback the Transaction&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROLLBACK;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Check the Log to Confirm Undo Operation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Accounts WHERE name = 'Charlie';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>sql</category>
    </item>
    <item>
      <title>Cursor + Trigger</title>
      <dc:creator>Kamwemba Tinashe C</dc:creator>
      <pubDate>Tue, 07 Oct 2025 07:05:37 +0000</pubDate>
      <link>https://forem.com/dartybaby/cursor-trigger-gkh</link>
      <guid>https://forem.com/dartybaby/cursor-trigger-gkh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Step 1: Create the Required Tables&lt;/strong&gt;&lt;br&gt;
To begin, we need to set up the database tables involved in the cursor and trigger tasks. This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Employee table: For storing employee details like name and salary (used for the cursor).&lt;/li&gt;
&lt;li&gt;Students table: For storing student details (used for the trigger).&lt;/li&gt;
&lt;li&gt;Student_Audit table: For logging audit entries when a new student is added.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Employee (
    id NUMBER PRIMARY KEY,
    name VARCHAR2(100),
    salary NUMBER
);

CREATE TABLE Students (
    student_id NUMBER PRIMARY KEY,
    name VARCHAR2(100)
);

CREATE TABLE Student_Audit (
    student_id NUMBER,
    registration_date DATE,
    action VARCHAR2(10)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Step 2: Insert Sample Data into the Employee Table&lt;/strong&gt;&lt;br&gt;
Next, populate the Employee table with sample data to test the cursor. I added four employees with salaries above and below 50,000 for variety.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Employee (id, name, salary) VALUES (1, 'John Doe', 60000);
INSERT INTO Employee (id, name, salary) VALUES (2, 'Jane Smith', 40000);
INSERT INTO Employee (id, name, salary) VALUES (3, 'Alice Johnson', 55000);
INSERT INTO Employee (id, name, salary) VALUES (4, 'Bob Brown', 45000);
COMMIT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2F9m6dpgouc5ifu2tkshb9.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%2F9m6dpgouc5ifu2tkshb9.png" alt=" " width="583" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Implement and Execute the Cursor&lt;/strong&gt;&lt;br&gt;
The cursor task is to display employee names where salary &amp;gt; 50,000 from the Employee table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DECLARE
    CURSOR emp_cursor IS
        SELECT name FROM Employee WHERE salary &amp;gt; 50000;
    emp_name VARCHAR2(100);
BEGIN
    OPEN emp_cursor;
    LOOP
        FETCH emp_cursor INTO emp_name;
        EXIT WHEN emp_cursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(emp_name);
    END LOOP;
    CLOSE emp_cursor;
END;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fbjpku1akz8tu4daqdw2i.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%2Fbjpku1akz8tu4daqdw2i.png" alt=" " width="339" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Create the AFTER INSERT Trigger&lt;/strong&gt;&lt;br&gt;
Now, create the trigger on the Students table to automatically log insertions into Student_Audit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE OR REPLACE TRIGGER student_audit_trigger
AFTER INSERT ON Students
FOR EACH ROW
BEGIN
    INSERT INTO Student_Audit (student_id, registration_date, action)
    VALUES (:NEW.student_id, SYSDATE, 'INSERT');
END;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;br&gt;
Output:**&lt;/p&gt;

&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%2Fqhi19hywlvdhxtm4kyk6.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%2Fqhi19hywlvdhxtm4kyk6.png" alt=" " width="288" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Insert a New Student to Test the Trigger&lt;/strong&gt;&lt;br&gt;
Insert a sample student to activate the trigger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Students (student_id, name) VALUES (1, 'New Student');
COMMIT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
text&lt;br&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%2Fl2l99rddzitcmpz9e6dy.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%2Fl2l99rddzitcmpz9e6dy.png" alt=" " width="207" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Verify the Audit Log&lt;/strong&gt;&lt;br&gt;
Finally, query the Student_Audit table to see the trigger's effect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Student_Audit;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fgl7afqpnep2m0cp2vev0.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%2Fgl7afqpnep2m0cp2vev0.png" alt=" " width="458" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>sql</category>
      <category>oracle</category>
    </item>
    <item>
      <title>Normalization</title>
      <dc:creator>Kamwemba Tinashe C</dc:creator>
      <pubDate>Tue, 07 Oct 2025 06:29:25 +0000</pubDate>
      <link>https://forem.com/dartybaby/normalization-24ph</link>
      <guid>https://forem.com/dartybaby/normalization-24ph</guid>
      <description>&lt;p&gt;&lt;strong&gt;Anomalies in the Base Table&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insertion Anomaly: Cannot add a new course or instructor without a student enrolled.&lt;/li&gt;
&lt;li&gt;Update Anomaly: Changing an instructor's phone (e.g., Dr. Kumar's) requires updating multiple rows.&lt;/li&gt;
&lt;li&gt;Deletion Anomaly: Deleting a student's enrollment might lose course or instructor data if it's the only record for that course.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SQL CREATE TABLE Statement for 1NF&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Enrollment (
    StudentID VARCHAR(10),
    StudentName VARCHAR(50),
    CourseID VARCHAR(10),
    CourseName VARCHAR(50),
    Instructor VARCHAR(50),
    InstructorPhone VARCHAR(15),
    PRIMARY KEY (StudentID, CourseID)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample INSERT Statements for 1NF&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Enrollment (StudentID, StudentName, CourseID, CourseName, Instructor, InstructorPhone) 
VALUES ('s01', 'Arjun', 'c01', 'DBMS', 'Dr. Kumar', '9876543210');

INSERT INTO Enrollment (StudentID, StudentName, CourseID, CourseName, Instructor, InstructorPhone) 
VALUES ('s01', 'Arjun', 'c02', 'Data Mining', 'Dr. Mehta', '9172467800');

INSERT INTO Enrollment (StudentID, StudentName, CourseID, CourseName, Instructor, InstructorPhone) 
VALUES ('s02', 'Priya', 'c01', 'DBMS', 'Dr. Kumar', '9876543210');

INSERT INTO Enrollment (StudentID, StudentName, CourseID, CourseName, Instructor, InstructorPhone) 
VALUES ('s03', 'Kiran', 'c03', 'AI', 'Dr. Rao', '9988776655');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fbgpmz2yyddp8ten8c8g5.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%2Fbgpmz2yyddp8ten8c8g5.PNG" alt=" " width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2NF Normalization&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Students (
    StudentID VARCHAR(10) PRIMARY KEY,
    StudentName VARCHAR(50)
);

CREATE TABLE Courses (
    CourseID VARCHAR(10) PRIMARY KEY,
    CourseName VARCHAR(50),
    Instructor VARCHAR(50),
    InstructorPhone VARCHAR(15)
);

CREATE TABLE Enrollment (
    StudentID VARCHAR(10),
    CourseID VARCHAR(10),
    PRIMARY KEY (StudentID, CourseID),
    FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
    FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fv229em97pxe8nj7wj195.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%2Fv229em97pxe8nj7wj195.png" alt=" " width="440" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&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%2Fxm0fs8iuecb1fmbgaoil.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%2Fxm0fs8iuecb1fmbgaoil.png" alt=" " width="693" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&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%2Ft9wc24gbtlv84akdkv0g.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%2Ft9wc24gbtlv84akdkv0g.png" alt=" " width="356" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3NF Normalization&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Students (
    StudentID VARCHAR(10) PRIMARY KEY,
    StudentName VARCHAR(50)
);

CREATE TABLE Courses (
    CourseID VARCHAR(10) PRIMARY KEY,
    CourseName VARCHAR(50),
    Instructor VARCHAR(50),
    FOREIGN KEY (Instructor) REFERENCES Instructors(Instructor)
);

CREATE TABLE Instructors (
    Instructor VARCHAR(50) PRIMARY KEY,
    InstructorPhone VARCHAR(15)
);

CREATE TABLE Enrollment (
    StudentID VARCHAR(10),
    CourseID VARCHAR(10),
    PRIMARY KEY (StudentID, CourseID),
    FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
    FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample INSERT Statements for 3NF&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Students (StudentID, StudentName) VALUES ('s01', 'Arjun');
INSERT INTO Students (StudentID, StudentName) VALUES ('s02', 'Priya');
INSERT INTO Students (StudentID, StudentName) VALUES ('s03', 'Kiran');

INSERT INTO Instructors (Instructor, InstructorPhone) 
VALUES ('Dr. Kumar', '9876543210');
INSERT INTO Instructors (Instructor, InstructorPhone) 
VALUES ('Dr. Mehta', '9172467800');
INSERT INTO Instructors (Instructor, InstructorPhone) 
VALUES ('Dr. Rao', '9988776655');

INSERT INTO Courses (CourseID, CourseName, Instructor) VALUES ('c01', 'DBMS', 'Dr. Kumar');
INSERT INTO Courses (CourseID, CourseName, Instructor) VALUES ('c02', 'Data Mining', 'Dr. Mehta');
INSERT INTO Courses (CourseID, CourseName, Instructor) VALUES ('c03', 'AI', 'Dr. Rao');

INSERT INTO Enrollment (StudentID, CourseID) VALUES ('s01', 'c01');
INSERT INTO Enrollment (StudentID, CourseID) VALUES ('s01', 'c02');
INSERT INTO Enrollment (StudentID, CourseID) VALUES ('s02', 'c01');
INSERT INTO Enrollment (StudentID, CourseID) VALUES ('s03', 'c03');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Fj6jlvq75b12zgaeaexfr.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%2Fj6jlvq75b12zgaeaexfr.png" alt=" " width="488" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&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%2Fgs8y3ymrdwky8e9slsao.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%2Fgs8y3ymrdwky8e9slsao.png" alt=" " width="410" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&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%2F45in33xaosw1ztllnfai.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%2F45in33xaosw1ztllnfai.png" alt=" " width="623" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&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%2F2odl2512o4s8aiq3pf2c.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%2F2odl2512o4s8aiq3pf2c.png" alt=" " width="458" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL query to list all students along with their courses and instructor names using JOINs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
    s.StudentID,
    s.StudentName,
    c.CourseID,
    c.CourseName,
    i.Instructor
FROM 
    Students s
INNER JOIN 
    Enrollment e ON s.StudentID = e.StudentID
INNER JOIN 
    Courses c ON e.CourseID = c.CourseID
INNER JOIN 
    Instructors i ON c.Instructor = i.Instructor;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2Flchyrdigtyubyz5stb2o.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%2Flchyrdigtyubyz5stb2o.png" alt=" " width="791" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>sql</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>College Student &amp; Course Management System: A Hands-On SQL Tutorial 📚💾</title>
      <dc:creator>Kamwemba Tinashe C</dc:creator>
      <pubDate>Thu, 21 Aug 2025 19:40:00 +0000</pubDate>
      <link>https://forem.com/dartybaby/college-student-course-management-system-a-hands-on-sql-tutorial-h8e</link>
      <guid>https://forem.com/dartybaby/college-student-course-management-system-a-hands-on-sql-tutorial-h8e</guid>
      <description>&lt;p&gt;Introduction 🚀&lt;/p&gt;

&lt;p&gt;Welcome to this exciting journey into building a College Student &amp;amp; Course Management System using SQL on Oracle! 🎓 This tutorial walks you through creating a database to manage students, courses, enrollments, and faculty, showcasing essential database concepts like table creation, data insertion, constraints, queries, joins, views, and stored procedures. Whether you're a beginner or sharpening your SQL skills, this hands-on guide is for you! 😊&lt;/p&gt;

&lt;p&gt;We’ll use Oracle SQL (via tools like Oracle LiveSQL) to implement a system that tracks student information, course enrollments, and faculty details. Let’s dive in! 🏊‍♂️&lt;/p&gt;




&lt;h2&gt;
  
  
  Database Schema 🏗️
&lt;/h2&gt;

&lt;p&gt;Our system consists of four tables: &lt;strong&gt;&lt;em&gt;Students, Courses, Enrollments, and Faculty&lt;/em&gt;&lt;/strong&gt;. Below is the schema with SQL commands to create them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Students Table
&lt;/h2&gt;

&lt;p&gt;Stores student details like ID, name, department, date of birth, and email.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Students (
    StudentID NUMBER PRIMARY KEY,
    Name VARCHAR2(50) NOT NULL,
    Dept VARCHAR2(30),
    DOB DATE,
    Email VARCHAR2(50) UNIQUE
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Courses Table&lt;/p&gt;

&lt;p&gt;Holds course information, including course ID, name, and credits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Courses (
    CourseID NUMBER PRIMARY KEY,
    CourseName VARCHAR2(50) NOT NULL,
    Credits NUMBER(2)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Enrollments Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Manages the many-to-many relationship between students and courses, including grades.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Enrollments (
    EnrollID NUMBER PRIMARY KEY,
    StudentID NUMBER REFERENCES Students(StudentID),
    CourseID NUMBER REFERENCES Courses(CourseID),
    Grade CHAR(2)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Faculty Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stores faculty information, created as part of our tasks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Faculty (
    FacultyID NUMBER PRIMARY KEY,
    FacultyName VARCHAR2(100) NOT NULL,
    Dept VARCHAR2(30),
    Email VARCHAR2(50) UNIQUE
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  1. Data Insertion 📥
&lt;/h2&gt;

&lt;p&gt;Let’s populate the Students and Courses tables with sample data to work with. We’ll also add some enrollments to simulate student-course relationships.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inserting Students
&lt;/h2&gt;

&lt;p&gt;Three students with different departments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Students (StudentID, Name, Dept, DOB, Email) VALUES
(1, 'Alice Smith', 'Computer Science', TO_DATE('2000-05-15', 'YYYY-MM-DD'), 'alice.smith@university.com');
INSERT INTO Students (StudentID, Name, Dept, DOB, Email) VALUES
(2, 'Bob Johnson', 'Mathematics', TO_DATE('1999-08-22', 'YYYY-MM-DD'), 'bob.johnson@university.com');
INSERT INTO Students (StudentID, Name, Dept, DOB, Email) VALUES
(3, 'Carol Lee', 'Physics', TO_DATE('2001-03-10', 'YYYY-MM-DD'), 'carol.lee@university.com');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inserting Courses
&lt;/h2&gt;

&lt;p&gt;Three courses with varying credits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Courses (CourseID, CourseName, Credits) VALUES (101, 'Databases', 4);
INSERT INTO Courses (CourseID, CourseName, Credits) VALUES (102, 'Algorithms', 3);
INSERT INTO Courses (CourseID, CourseName, Credits) VALUES (103, 'Physics', 5);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inserting Enrollments
&lt;/h2&gt;

&lt;p&gt;Linking students to courses with grades:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Enrollments (EnrollID, StudentID, CourseID, Grade) VALUES (1, 1, 101, 'A');
INSERT INTO Enrollments (EnrollID, StudentID, CourseID, Grade) VALUES (2, 2, 102, 'B+');
INSERT INTO Enrollments (EnrollID, StudentID, CourseID, Grade) VALUES (3, 3, 103, 'A-');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Table Alterations and Constraints 🔧
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Adding a Phone Number Column&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add&lt;/strong&gt; a PhoneNo column to the Students table to store 10-digit phone numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE Students ADD PhoneNo CHAR(10);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Restricting Course Credits
&lt;/h2&gt;

&lt;p&gt;Ensure Credits in the Courses table is between 1 and 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE Courses ADD CONSTRAINT chk_credits CHECK (Credits BETWEEN 1 AND 5);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. SQL Queries with Functions 🛠️
&lt;/h2&gt;

&lt;p&gt;Let’s manipulate data using SQL functions.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Uppercase Names and Email Length&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
Display student names in uppercase and the length of their email addresses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT UPPER(Name) AS UppercaseName, LENGTH(Email) AS EmailLength
FROM Students;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&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%2Fdsnjsb03bf6u1zumcjh1.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%2Fdsnjsb03bf6u1zumcjh1.png" alt=" " width="531" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Aggregate Functions 📊
&lt;/h2&gt;

&lt;p&gt;Calculate the average credits of all courses and the total number of students enrolled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
    (SELECT AVG(Credits) FROM Courses) AS Average_Credits,
    (SELECT COUNT(DISTINCT StudentID) FROM Enrollments) AS Total_Students_Enrolled
FROM DUAL;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. JOIN Queries 🤝
&lt;/h2&gt;

&lt;p&gt;List all students, their enrolled courses, and grades.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.Name AS StudentName, c.CourseName, e.Grade
FROM Students s
LEFT JOIN Enrollments e ON s.StudentID = e.StudentID
LEFT JOIN Courses c ON e.CourseID = c.CourseID
ORDER BY s.Name, c.CourseName;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Using LEFT JOIN ensures all students appear, even if they have no enrollments (though all sample students are enrolled).&lt;/p&gt;




&lt;h2&gt;
  
  
  6. GROUP BY and HAVING Clause 📈
&lt;/h2&gt;

&lt;p&gt;Count students per department, showing only departments with more than 2 students.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT Dept, COUNT(*) AS StudentCount
FROM Students
GROUP BY Dept
HAVING COUNT(*) &amp;gt; 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;: (Empty, as no department has more than 2 students with current data.)&lt;/p&gt;

&lt;p&gt;Let’s Add More Students for demonstration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Students (StudentID, Name, Dept, DOB, Email) VALUES
(4, 'David Brown', 'Computer Science', TO_DATE('2000-07-20', 'YYYY-MM-DD'), 'david.brown@university.com');
INSERT INTO Students (StudentID, Name, Dept, DOB, Email) VALUES
(5, 'Eve White', 'Computer Science', TO_DATE('1999-12-01', 'YYYY-MM-DD'), 'eve.white@university.com');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&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%2F9yhrw68mbpz73kp970i7.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%2F9yhrw68mbpz73kp970i7.png" alt=" " width="750" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Views 👀
&lt;/h2&gt;

&lt;p&gt;Create a view to simplify access to student-course-grade data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE OR REPLACE VIEW StudentCoursesView AS
SELECT s.Name AS StudentName, c.CourseName, e.Grade
FROM Students s
LEFT JOIN Enrollments e ON s.StudentID = e.StudentID
LEFT JOIN Courses c ON e.CourseID = c.CourseID;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. Stored Procedure ⚙️
&lt;/h2&gt;

&lt;p&gt;Create a procedure to update a student’s grade in the Enrollments table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE OR REPLACE PROCEDURE UpdateGrade (
    p_StudentID IN NUMBER,
    p_CourseID IN NUMBER,
    p_NewGrade IN CHAR
) AS
BEGIN
    UPDATE Enrollments
    SET Grade = p_NewGrade
    WHERE StudentID = p_StudentID AND CourseID = p_CourseID;
    IF SQL%ROWCOUNT = 0 THEN
        RAISE_APPLICATION_ERROR(-20001, 'No enrollment found for the given StudentID and CourseID');
    END IF;
    COMMIT;
EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        RAISE;
END;
/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary 🎉
&lt;/h2&gt;

&lt;p&gt;This tutorial covered key SQL concepts in Oracle:&lt;br&gt;
🛠️ Schema Design: Created tables with appropriate constraints.&lt;br&gt;
📥 Data Manipulation: Inserted and modified data.&lt;br&gt;
🔍 Queries: Used functions, aggregates, joins, and grouping.&lt;br&gt;
👀 Views: Simplified data access.&lt;br&gt;
⚙️ Stored Procedures: Automated updates with error handling.&lt;/p&gt;

&lt;p&gt;Try running these scripts in Oracle LiveSQL to see the system in action! 🚀 Share your thoughts or questions in the comments below. Happy coding! 😄&lt;/p&gt;

</description>
      <category>sql</category>
      <category>oracle</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
