Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Querying multiple subclassed tables

Querying multiple subclassed tables

Scheduled Pinned Locked Moved Database
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Keith Andersch
    wrote on last edited by
    #1

    Hi all, Here's a situation. I have table A which has several subclassed tables, say A1, A2, and A3. Table A has a primary key named foo which will only exist once in one of the subclassed tables. Now, I want to do a select statement to grab foo from table A and where ever it might be in the subclassed tables. Is it possible to write a single select statement instead of having to write 3 seperate select statements to get to the subclassed data? Thanks, Keith

    A 1 Reply Last reply
    0
    • K Keith Andersch

      Hi all, Here's a situation. I have table A which has several subclassed tables, say A1, A2, and A3. Table A has a primary key named foo which will only exist once in one of the subclassed tables. Now, I want to do a select statement to grab foo from table A and where ever it might be in the subclassed tables. Is it possible to write a single select statement instead of having to write 3 seperate select statements to get to the subclassed data? Thanks, Keith

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      You would normally use the following:

      select A.*, A1.*, A2.*, A3.*
      from A
      left outer join A1 on A1.id = A.id
      left outer join A2 on A2.id = A.id
      left outer join A3 on A3.id = A.id
      where A.id = 'foo'
      

      This will only return one row - the outer joins will either return proper field values (for the appropriate sub-class table) or nulls (for the inappropriate ones). Regards Andy

      K 1 Reply Last reply
      0
      • A andyharman

        You would normally use the following:

        select A.*, A1.*, A2.*, A3.*
        from A
        left outer join A1 on A1.id = A.id
        left outer join A2 on A2.id = A.id
        left outer join A3 on A3.id = A.id
        where A.id = 'foo'
        

        This will only return one row - the outer joins will either return proper field values (for the appropriate sub-class table) or nulls (for the inappropriate ones). Regards Andy

        K Offline
        K Offline
        Keith Andersch
        wrote on last edited by
        #3

        Thanks, Andy. That seems to do the trick. I'm curious though, as you add subclassed tables, would performance degrade significantly? I'm using SQL Server 2000. I'm not too worried about performance right now but it's something that would be good to know. Thanks, Keith

        A 1 Reply Last reply
        0
        • K Keith Andersch

          Thanks, Andy. That seems to do the trick. I'm curious though, as you add subclassed tables, would performance degrade significantly? I'm using SQL Server 2000. I'm not too worried about performance right now but it's something that would be good to know. Thanks, Keith

          A Offline
          A Offline
          andyharman
          wrote on last edited by
          #4

          As long as each of the tables involved have a unique index or PK constraint (preferably clustered) specified on the key column the query should perform well. I would say that the problem comes with the maintenance effort required to add subclassed tables to all of your queries. I try not to overuse this design for that reason, rather than for performance. An alternative common design that may perform better would be to merge all of the sub-classed tables into the ancestor table. If you are worried about performance and encapsulation then you might consider using a hybrid of the two designs - so that the most commonly-used information is merged into the ancestor table, with the sub-classed tables only required in exceptional cases. Like many design decisions - there is no right or wrong answer:) Andy

          K 1 Reply Last reply
          0
          • A andyharman

            As long as each of the tables involved have a unique index or PK constraint (preferably clustered) specified on the key column the query should perform well. I would say that the problem comes with the maintenance effort required to add subclassed tables to all of your queries. I try not to overuse this design for that reason, rather than for performance. An alternative common design that may perform better would be to merge all of the sub-classed tables into the ancestor table. If you are worried about performance and encapsulation then you might consider using a hybrid of the two designs - so that the most commonly-used information is merged into the ancestor table, with the sub-classed tables only required in exceptional cases. Like many design decisions - there is no right or wrong answer:) Andy

            K Offline
            K Offline
            Keith Andersch
            wrote on last edited by
            #5

            I totally agree about the maintenance issue that arises from having so many subclassed tables in such a query. I began to explore a different path and ended up not going with huge query that you suggested. I'm trying my "bestest" to do an n-tier approach and that huge query didn't fit right with what I had. Though who knows if I'm doing it close to right. lol Thanks again. Keith

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups