Now, before you go crazy with this stuff, remember, it’s not a magic bullet, but rather some automation help to save you some coding and to help you review your data model. The script doesn’t change the database, it only prints out its suggestions, and this is totally by design.
For this to work, you’ll obviously need proper primary keys or unique indexes on your referenced tables.
We’re working on the assumption that the referencing and referenced column names are the same. Go ahead and change the script to suit your naming standards (look for the comment in the CTE)
The script has no domain knowledge of your database, some of the suggestions are probably going to be downright silly.
This is a good first pass approach, especially if you have a larger database completely lacking in relational integrity.