DB2 Questions   «Prev  Next»

DB2 Self-referencing Constraints Questions

  1. What is the self-referencing constraint?

    Answer:

    A "self-referencing constraint" in DB2 (or any relational database) is a type of referential integrity constraint where a table's foreign key refers to its own primary key. This relationship is used to enforce a hierarchical or recursive relationship within the same table.
    Key Features of a Self-Referencing Constraint:
    1. Same Table Reference:
      • The foreign key column(s) in the table points to the primary key column(s) of the same table.
    2. Use Case:
      • It is commonly used to model hierarchical relationships, such as:
        • Organizational structures (e.g., employees reporting to a manager).
        • Parts in a bill of materials (e.g., a component that is part of another component).
        • Directory structures in file systems (e.g., folders containing subfolders).
    3. Behavior:
      • Ensures that the value in the foreign key column exists in the primary key column of the same table.

    Example in DB2:
    Consider an **Employee** table where employees report to a manager. The `EmployeeID` is the primary key, and the `ManagerID` is the foreign key that references `EmployeeID` within the same table.
    Table Definition:
    CREATE TABLE Employee (
        EmployeeID INT PRIMARY KEY,
        Name VARCHAR(100),
        ManagerID INT,
        FOREIGN KEY (ManagerID) REFERENCES Employee(EmployeeID)
    );
    

    Explanation:
    • `EmployeeID`: Unique identifier for each employee (Primary Key).
    • `ManagerID`: Refers to the `EmployeeID` of the manager for each employee.
    • A self-referencing constraint ensures that the value in ManagerID corresponds to an existing EmployeeID.
    Example Data:
    | EmployeeID | Name        | ManagerID |
    |------------|-------------|-----------|
    | 1          | Alice       | NULL      |
    | 2          | Bob         | 1         |
    | 3          | Charlie     | 1         |
    | 4          | Diana       | 2         |
    

    • Alice (EmployeeID 1) has no manager (ManagerID is NULL).
    • Bob (EmployeeID 2) and Charlie (EmployeeID 3) report to Alice (ManagerID is 1).
    • Diana (EmployeeID 4) reports to Bob (ManagerID is 2).

    Benefits:
    • Data Integrity: Ensures valid hierarchical relationships within the table.
    • Simplified Design: Avoids creating additional tables for hierarchical data when not necessary.

    Challenges: Can introduce complexities when querying hierarchical data, often requiring recursive queries or Common Table Expressions (CTEs). For example:
    WITH RECURSIVE EmployeeHierarchy AS (
        SELECT EmployeeID, Name, ManagerID
        FROM Employee
        WHERE ManagerID IS NULL
        UNION ALL
        SELECT e.EmployeeID, e.Name, e.ManagerID
        FROM Employee e
        INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID
    )
    SELECT * FROM EmployeeHierarchy;
    

    This query retrieves all employees in a hierarchy, starting with the top-level manager.
    The self-referencing constraint limits in a single table the changes to a primary key that the related foreign key defines. The foreign key in a self referencing table must specify the DELETE CASCADE rule.

  2. What are delete-connected tables?

    Answer: Tables related with a foreign key are called delete-connected because a deletion in the primary key table can affect the contents of the foreign key table.

  3. When can an insert of a new primary key value threaten referential integrity?

    Answer: Never. New primary key values are not a problem. However, the values of foreign key inserts must have corresponding primary key values in their related tables, and updates of primary key values may require changes in foreign key values to maintain referential integrity.

  4. In terms of DB2 indexing, what is the root page?

    Answer:The simplest DB2 index is the B-tree and the B-tree's top page is called the root page. The root page entries represent the upper range limits of the index and are referenced first in a search.

  5. How does DB2 use multiple table indexes ?

    Answer: DB2 use the multiple indexes to satisfy multiple predicates in a SELECT statement that are joined by an AND or OR.

  6. What are some characteristics of columns that benefit from indexes?

    Answer: Primary key and foreign key columns; columns that have unique values; columns that have aggregates computed frequently and columns used to test the existence of a value.

  7. What is a composite index and how does it differ from a multiple index?

    Answer: A multiple index is not one index but two indexes for two different columns of a table.
    A composite index is one index made up of combined values from two columns in a table. If two columns in a table will often be accessed together a composite index will be efficient.

  8. What is meant by index cardinality?

    Answer: The number of distinct values for a column is called index cardinality.
    DB2's RUNSTATS utility analyzes column value redundancy to determine whether to use a tablespace or index scan to search for data.

  9. What is a clustered index?

    Answer: For a clustered index DB2 maintains rows in the same sequence as the columns in the index for as long as there is free space. DB2 can then process that table in that order efficiently.

  10. What keyword does an SQL SELECT statement use for a string search?

    Answer: The LIKE keyword allows for string searches. The % sign is used as a wildcard.

SEMrush Software 3 SEMrush Banner 3