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. Self Referenced Tables.

Self Referenced Tables.

Scheduled Pinned Locked Moved Database
databasetutorial
6 Posts 3 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.
  • X Offline
    X Offline
    Xandip
    wrote on last edited by
    #1

    Hi all. Please guide me thru this. I have a large database with many tables having self references. for eg: TestTable: TestId int, TestName varchar(100), ParentTestId int, Primary Key (TestId), Foreign Key (ParentTestId) References TestTable(TestId) I just wanted to query out the tables which are having similar type of relationships within themselves. Thanks in advance.

    The name is Sandeep

    P 1 Reply Last reply
    0
    • X Xandip

      Hi all. Please guide me thru this. I have a large database with many tables having self references. for eg: TestTable: TestId int, TestName varchar(100), ParentTestId int, Primary Key (TestId), Foreign Key (ParentTestId) References TestTable(TestId) I just wanted to query out the tables which are having similar type of relationships within themselves. Thanks in advance.

      The name is Sandeep

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      [Message Deleted]

      X 1 Reply Last reply
      0
      • P Parwej Ahamad

        [Message Deleted]

        X Offline
        X Offline
        Xandip
        wrote on last edited by
        #3

        thanks for the reply man. but that's not what i asked. :) I don't want to select any DATA from any table. I just wanted to know whether there are any other tables in my database, which have self references within themselves [just like the reference established in TestTable] I wanted a query which shows those type of tables.

        The name is Sandeep

        K 1 Reply Last reply
        0
        • X Xandip

          thanks for the reply man. but that's not what i asked. :) I don't want to select any DATA from any table. I just wanted to know whether there are any other tables in my database, which have self references within themselves [just like the reference established in TestTable] I wanted a query which shows those type of tables.

          The name is Sandeep

          K Offline
          K Offline
          Krishnraj
          wrote on last edited by
          #4

          Hi Xandip, Just try below query. and yes run it in SQL Server 2000

          select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where Table_Name = 'TestTable'
          and Constraint_Type = 'FOREIGN KEY'

          Hope it will help u.

          Krishnraj

          X 1 Reply Last reply
          0
          • K Krishnraj

            Hi Xandip, Just try below query. and yes run it in SQL Server 2000

            select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where Table_Name = 'TestTable'
            and Constraint_Type = 'FOREIGN KEY'

            Hope it will help u.

            Krishnraj

            X Offline
            X Offline
            Xandip
            wrote on last edited by
            #5

            thanx buddy, but isnt this the query to list the foreign keys of a table? anyways, i think i got my answer select * from sys.objects where object_id in (select parent_object_id from sys.foreign_key_columns where parent_object_id = referenced_object_id) Thanks for your effort guys..:)

            The name is Sandeep

            K 1 Reply Last reply
            0
            • X Xandip

              thanx buddy, but isnt this the query to list the foreign keys of a table? anyways, i think i got my answer select * from sys.objects where object_id in (select parent_object_id from sys.foreign_key_columns where parent_object_id = referenced_object_id) Thanks for your effort guys..:)

              The name is Sandeep

              K Offline
              K Offline
              Krishnraj
              wrote on last edited by
              #6

              Yes, my posted query displays the name of the table and all its constraints. but in sql server 2000. I see yr query and its of sql server 2005. anyways, now u got the solution. but its just for yr info.. :)

              Krishnraj

              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