DB Referential Integrity

  • I know there is an sp_something that checks the referential integrity of your tables but I can't remember what it was.

    I know "sp_helpconstraint " will give me the PK/FK constraints on the table but I need to make sure data integrity.

    Thanks

  • I don't know of a Stored Procedure but DBCC CheckTable should do most of what you want. Ckeck it out in BOL

  • Are you saying physical structures or logically if you have things set up in RI?

    The constraints are enforced, as are unique indexes if you didn't allow dups when you built them, but there isn't anything that is proper integrity. You set up the level of integrity you think is appropriate.

  • hmm...

    OK, let me rephrase the question. How do I check for "Orphaned" records?

  • Hi Warren,

    Checking for "Orphaned Records" can be a bear depending on how your schema (including referential integrity) is set up.

    Basically, you need to test each foreign key in the child table to see if it exists in the appropriate parent table. So depending on the nature of the Key that may be more or less complex. I have a legacy table in which a predecessor set up a "selector" column and a generic foreign key column so any test requires you to first look at the selector to determine which table is the parent for the key. Ugggh.

    I'm not the sharpest T-SQL guy but I ususally use Temp Tables to do this type of check. First load the temp table with the Foreign Keys, then delete from the Temp table where the Foreign Key is in the parent table. Anything left is "orphan".

    hope this helps.

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

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