πŸ”
Back

Define Cursor. List the two types of cursor.

0 like 0 dislike

2 Answers

βœ”οΈ
Best answer

Of course. Here is a detailed distinction between the Network Model and the Hierarchical Model, two early and influential database models.


Introduction

Both the Hierarchical Model and the Network Model are navigational database models, meaning that data is accessed by following pointers from one record to another. They predate the modern Relational Model (which uses tables) and were prominent from the 1960s to the 1980s.

The fundamental difference between them lies in how they structure data relationships: the hierarchical model uses a strict tree-like structure, while the network model uses a more flexible graph-like structure.


Hierarchical Model

The Hierarchical Model organizes data into a tree structure, similar to a file system or an organizational chart.

  • Structure: A tree consisting of nodes and branches. Each node represents a record type.
  • Key Concept: The Parent-Child Relationship.
    • Each "child" record type can have only one "parent" record type.
    • A "parent" record type can have one or more "child" record types.
    • This creates a strict one-to-many relationship from parent to child.
  • Root: The tree has a single starting point at the top called the "root." Data is accessed by starting at the root and traversing down the tree.

Analogy: A family tree where each child has only one biological father, or an organizational chart where each employee reports to only one manager.

Diagram:
`

          (Root)
        University
            |
   +--------+--------+
   |                 |
 Faculty           Student
   |                 |

+---+---+ +---+---+
| | | |
Course Professor Course Grade
`
In this strict model, Course under Faculty is different from Course under Student. This leads to data redundancy.


Network Model

The Network Model was developed to overcome the limitations of the hierarchical model. It allows for more complex relationships by organizing data in a graph structure.

  • Structure: A graph (or network) where nodes (record types) can be connected in a flexible way.
  • Key Concept: The Owner-Member Relationship (also known as a "set").
    • Each "member" record type can have one or more "owner" record types.
    • An "owner" can have multiple "members."
    • This allows for both one-to-many and many-to-many relationships to be represented directly.
  • No Single Root: Data can be accessed from multiple entry points.

Analogy: A social network where a person can be part of multiple groups (e.g., family, work, and a sports team), and each group has multiple members.

Diagram:
In this model, a single Student record can be linked to multiple Course records, and a single Course record can be linked to multiple Student records.

`

   Professor              Course
      \                  /   |
       \                /    |
        \              /     |
         +------------+      |
         |   Student  |------+
         +------------+

`
Here, the Student record is a "member" of both the Professor "owner" and the Course "owner," naturally representing a many-to-many relationship without redundancy.


Key Distinctions

Here are the primary differences between the two models:

  1. Data Structure:
    Hierarchical: A rigid tree structure.
    Network: A flexible graph (or network) structure.

  2. Relationship Representation:
    Hierarchical: Primarily supports one-to-many relationships. Representing many-to-many relationships is difficult and often requires duplicating data, leading to redundancy.
    Network: Directly supports both one-to-many and many-to-many relationships, which significantly reduces data redundancy.

  3. Parent/Owner Rules:
    Hierarchical: A child record can have exactly one parent.
    Network: A member record can have multiple owners. This is the core difference that grants the network model its flexibility.

  4. Complexity:
    Hierarchical: Simpler to design and understand due to its rigid, top-down structure.
    Network: Much more complex to design, implement, and manage because of the web of pointers between records.

  5. Data Access and Navigation:
    Hierarchical: Navigation is strictly vertical (top-down through a single path). Accessing a child record requires going through its parent.
    Network: Navigation is more flexible. A user can follow different paths through the sets to get to a record. However, this navigation is complex and requires the programmer to understand the physical pointer structure.

  6. Data Redundancy:
    Hierarchical: Tends to have higher data redundancy when modeling real-world scenarios that involve many-to-many relationships.
    Network: Tends to have lower data redundancy because it can model complex relationships directly.


Summary Table

| Feature | Hierarchical Model | Network Model |
| :--- | :--- | :--- |
| Basic Structure | Tree (Parent-Child) | Graph (Owner-Member) |
| Relationships | Only one-to-many | One-to-many and many-to-many |
| Child/Member Rule | A child has one parent. | A member can have multiple owners. |
| Data Redundancy | High, especially for M:N relationships. | Low, as M:N is handled directly. |
| Complexity | Relatively simple and rigid. | Highly complex and flexible. |
| Navigation | Simple, top-down traversal. | Complex, requires following paths in a graph. |
| Example System | IBM's IMS (Information Management System) | CODASYL standard, IDMS |

In conclusion, the Network Model was an evolution of the Hierarchical Model, created specifically to solve its inability to efficiently handle many-to-many relationships. While more powerful, its complexity paved the way for the much simpler and more flexible Relational Model to become the dominant standard.

0 like 0 dislike

Of course. Here is a definition of a cursor, followed by a list and explanation of its two main types.


Define Cursor

A Cursor is a database object that acts as a pointer to a result setβ€”the set of rows returned by a SQL query. It allows an application to process the rows of a result set one at a time, rather than processing the entire set at once.

In standard SQL, commands like SELECT, INSERT, UPDATE, and DELETE are set-based, meaning they operate on all qualifying rows simultaneously. However, there are times when you need to perform complex logic or operations on each individual row. A cursor provides a mechanism for this row-by-row processing.

Think of a cursor like a bookmark in a book. When you run a query, the database finds all the "pages" (rows) that match. The cursor is the bookmark you place on the first matching page, allowing you to read it, then move the bookmark to the next page, and so on, until you've processed all the relevant pages.

The typical lifecycle of using a cursor involves these steps:
1. DECLARE: Define the cursor by giving it a name and associating it with a SELECT statement.
2. OPEN: Execute the query, populate the result set, and position the cursor before the first row.
3. FETCH: Retrieve the current row the cursor is pointing to and advance the cursor to the next row. This step is usually performed inside a loop.
4. CLOSE: Release the active result set. The cursor can be reopened later.
5. DEALLOCATE: Completely remove the cursor definition and release all associated resources.


The Two Types of Cursors

Cursors are categorized into two main types based on how they are created and managed:

1. Implicit Cursors
  • Definition: An Implicit Cursor is a cursor that is automatically created and managed by the database system whenever a Data Manipulation Language (DML) statement (INSERT, UPDATE, DELETE) is executed. They are also used for SELECT statements that return only one row (e.g., SELECT ... INTO ... in PL/SQL or T-SQL).
  • Characteristics:
    • Automatic: The programmer does not explicitly DECLARE or OPEN them. The database handles everything behind the scenes.
    • Less Control: The programmer has no direct control over its operations.
    • Status Checking: You can access information about the most recent DML operation through cursor attributes (like SQL%FOUND, SQL%NOTFOUND, or SQL%ROWCOUNT in Oracle PL/SQL, or @@ROWCOUNT in SQL Server T-SQL).
  • When It's Used:
    • When you execute an UPDATE statement, the database internally uses a cursor to find all the rows to be updated.
    • When you INSERT a new record.
    • When you DELETE one or more records.
  • Example (Conceptual, in PL/SQL):
    When you run this simple UPDATE statement, an implicit cursor is at work.

    `sql
    -- This command uses an implicit cursor
    UPDATE Employees
    SET Salary = Salary * 1.10
    WHERE DepartmentID = 10;

    -- You can then check the result using an implicit cursor attribute
    IF SQL%FOUND THEN
    DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees received a raise.');
    END IF;
    `

2. Explicit Cursors
  • Definition: An Explicit Cursor is a cursor that is manually defined, opened, fetched from, and closed by the programmer. It gives the developer fine-grained control over processing a result set that may contain multiple rows.
  • Characteristics:
    • Manual Control: The programmer is responsible for the entire lifecycle (DECLARE, OPEN, FETCH, CLOSE).
    • Multi-Row Queries: Used specifically for handling SELECT statements that are expected to return more than one row.
    • Row-by-Row Logic: Allows for complex procedural logic to be executed for each row of the result set.
  • When It's Used:
    • When you need to retrieve a list of records and perform a different action on each one based on its values.
    • When you need to process data sequentially.
  • Example (Conceptual, in PL/SQL):
    This code explicitly defines a cursor to fetch all employees from department 10 and print their names one by one.

    `sql
    DECLARE
    -- 1. Declare the cursor
    CURSOR c_employees IS

    SELECT FirstName, LastName FROM Employees WHERE DepartmentID = 10;
      
    

    v_first_name Employees.FirstName%TYPE;
    v_last_name Employees.LastName%TYPE;

    BEGIN
    -- 2. Open the cursor
    OPEN c_employees;

    LOOP

    -- 3. Fetch data from the cursor into variables
    FETCH c_employees INTO v_first_name, v_last_name;
        
    -- Exit the loop when there are no more rows to fetch
    EXIT WHEN c_employees%NOTFOUND;
        
    -- Process the current row
    DBMS_OUTPUT.PUT_LINE('Employee: ' || v_first_name || ' ' || v_last_name);
    

    END LOOP;

    -- 4. Close the cursor
    CLOSE c_employees;
    END;
    /
    `

Summary Table

| Feature | Implicit Cursor | Explicit Cursor |
| :--- | :--- | :--- |
| Creation | Automatic, by the database system. | Manual, by the programmer. |
| Control | Indirect, through attributes. | Direct and full control over lifecycle. |
| Usage | For all DML statements (INSERT, UPDATE, DELETE). | For multi-row SELECT queries requiring row-by-row processing. |
| Visibility | Hidden from the programmer. | Declared and visible in the code. |

0 like 0 dislike
Next ⇨Next ⇨⇦ Previous⇦ Previous

Related questions

State the use of database trigger and also list types of trigger.
Answer : Of course. Here is a clear explanation of the use of database triggers and a list of their different types. --- ### The Use of a Database Trigger A **database trigger** is a ... instead of* the original DML statement, allowing you to define custom logic for updating complex, non-updatable views....

Show More

Explain cursor with example.
Answer : Of course. Here is a detailed explanation of a cursor in SQL/PL/SQL, including its purpose, a step-by-step lifecycle, and a complete, practical example. --- ### What is a Cursor? A ... resources that were allocated to the cursor's result set. This is a crucial step for good resource management....

Show More

Write step by step syntax to create, open and close cursor in PL/SQL.
Answer : Of course. Here is a step-by-step guide to the syntax for creating, opening, fetching from, and closing a cursor in PL/SQL, complete with a full, practical example. --- ### Introduction: The Cursor Lifecycle Working ... END IF; DBMS_OUTPUT.PUT_LINE('An error occurred.'); END; / ```...

Show More

Define Normalization, list its types.
Answer : Of course. Here is a comprehensive definition of Normalization, followed by a list and explanation of its most common types (normal forms). --- ### Define Normalization **Normalization** ... -world applications, achieving **3NF** is considered the standard for a well-designed relational database....

Show More

Describe the steps to perform the 'Tadasana' (Mountain Pose) and list two of its benefits.

Show More
Welcome to Computer Engineering, where you can ask questions and receive answers from other members of the community.

Categories

...