Enforce referential integrity in a hierarchical table

Last post 06-09-2008, 3:17 PM by acornman. 1 replies.
Sort Posts: Previous Next
  •  06-06-2008, 9:39 AM Post number 59011

    • abev is not online. Last active: 06-25-2008, 8:00 AM abev
    • Top 150 Contributor
    • Joined on 08-23-2007
    • Level 1: Deep thought

    Enforce referential integrity in a hierarchical table

    First off, it took me a few days to get my head around the concept of having a hierachical table. Specifically, where I could store "parents" and "children" in the same table. Apparently this is acceptable, and necessary, if you want a hierarchical structure with indefinate children.

    e.g.
    Parent
    ----Child 1
    ----Child 2

    So assuming your structure is ("ID", "Name", "ParentID") in a table, how exactly would you enforce referential integrity? What is the acceptable method? I would assume you would never do a straight "delete" of a row in the table, but instead create a stored procedure to check for children.
  •  06-09-2008, 3:17 PM Post number 59364 in reply to post number 59011

    Re: Enforce referential integrity in a hierarchical table

    You should be able to place a foriegn key on the table to itself between the id column and the parent id column.
View as RSS news feed in XML