Define Cursor. List the two types of cursor.
Of course. Here is a detailed distinction between the Network Model and the Hierarchical Model, two early and influential database models.
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.
The Hierarchical Model organizes data into a tree structure, similar to a file system or an organizational chart.
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.
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.
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.
Here are the primary differences between the two models:
Data Structure:
Hierarchical: A rigid tree structure.
Network: A flexible graph (or network) structure.
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.
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.
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.
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.
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.
| 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.
Of course. Here is a definition of a cursor, followed by a list and explanation of its two main types.
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.
Cursors are categorized into two main types based on how they are created and managed:
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).DECLARE
or OPEN
them. The database handles everything behind the scenes.SQL%FOUND
, SQL%NOTFOUND
, or SQL%ROWCOUNT
in Oracle PL/SQL, or @@ROWCOUNT
in SQL Server T-SQL).UPDATE
statement, the database internally uses a cursor to find all the rows to be updated.INSERT
a new record.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;
`
DECLARE
, OPEN
, FETCH
, CLOSE
).SELECT
statements that are expected to return more than one row.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;
/
`
| 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. |