Sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Limno
    New Member
    • Apr 2008
    • 92

    Sql

    Can anyone suggest me for this, i was just confused whether i can create 3 separate table or i can simplify this table.

    My question is

    i wnt to create a treeview to display some values
    so that i m hving the table as

    1.For ParentTable
    ParentID
    ParentName

    2.For ChildTable
    ParentID
    ChildID
    ChildName

    3.SubChildTable
    ChildID
    GrandChildName

    i wnt to display data based on its ID like

    ParentName
    ---ChildName
    ------GrandChildName1
    ------GrandChildName2
    ---ChildName1

    goes on

    is any possibility to simplify this Tables.
    if soo, it'll give some repeated values

    i dont know wht ll i do.
    Help Me please
    Thanks in Advance
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Example:

    John is the Grandfather - his son is Peter. Peter is the father to Bill.

    Relationship is John - Peter - Bill. In a database you only need one table to represent this.

    tblPerson
    PersonID (Primary key)
    PersonName
    ChildID (Foreign key referencing the primary key of its own table)

    Data:

    PersonID PersonName ChildID
    1 John 2
    2 Peter 3
    3 Bill

    [CODE=sql]
    SELECT T1.PersonID, T1.PersonName, T2.PersonID, T2.PersonName, T3.PersonID, T3.PersonName
    FROM (tblPerson As T1 LEFT JOIN (tblPerson As T2
    LEFT JOIN tblPerson T3 ON T2.ChildID=T3.P arentID)
    ON T1.ChildID = T2.PersonID)
    [/CODE]

    I'm not sure what you are trying to represent in your treeview but this should give you some ideas.

    Comment

    • FishVal
      Recognized Expert Specialist
      • Jun 2007
      • 2656

      #3
      Originally posted by msquared
      ....

      tblPerson
      PersonID (Primary key)
      PersonName
      ChildID (Foreign key referencing the primary key of its own table)

      .....
      Actually ChildID reference should be replaced with ParentID reference as long as tree node may have one parent and multiple childs.

      tblNodes
      keyNodeID (PK)
      txtNodeName (Text)
      keyParentNodeID (FK(tblNodes.ke yNodeID))

      If number of parents is expected to be more than one, then additional table to implement many-to-many relationship is needed.

      tblNodes
      keyNodeID (PK)
      txtNodeName (Text)

      tblNodeRelation s
      keyParentNodeID (FK(tblNodes.ke yNodeID))
      keyChildNodeID (FK(tblNodes.ke yNodeID))

      Regards,
      Fish

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Thanks fish, I was looking at it purely from the database point of view and not the treeview structure. Your suggestion makes more sense.

        Comment

        Working...