Foreign keys

  • How can i check if my database has any circular references/relationships and how to get rid of them.

  •  

    Hi ,

    Use this sp..

     

    sp_depends <Table Name>

    ex. sp_depends 'authors'

     

    Regards ,

    Amit Gupta

    MVP

  • do i need to do that for each table..is there a way i can do for total database.

    Actually I have to check comeplte database for circular references and come up with solutions to get rid of them.

     

    but i dont think this will give me the relationships, this may result in store procs using that table.

  • Try this query to get a list of all the foreign keys and referenced tables in a database:

    select

    t.name as 'Table', c.name as 'Foreign_Key_Constraint', rt.name as Referenced_Table'

    from (select distinct constid, fkeyid, rkeyid from sysforeignkeys) as FK

    join sysobjects c on c.id = FK.constid

    join sysobjects t on t.id = FK.fkeyid

    join sysobjects rt on rt.id = FK.rkeyid

    order by 1,2,3

    Greg

     

    Greg

  • Greg

    by running your query i am able to get all the tables,references but how can I find circular references in it and get rid of them

  • If, by circular reference you mean a table referring to itself via a foreign key constraint, the query I posted should show that.  You can get rid of them by deleting the constraint.

    Greg

    Greg

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply