DESCRIPTION:
I use this script to enforce/apply a naming standard to foreign key constraints. System named constraints are semi-random and cause problems when promoting changes between servers. By enforcing a naming standard for constraints, promoted code should be usable in all environments.
**Updated**
Changed output to colums rather than Print screen
Ordered result by Altered Table Name
Add Column to display Constraint Name Length (SQL Constraint Name max is 128)
Updated to use Information_Schema objects and work with multi-column constraints
**
HOW TO USE:
Use the variables at the top of the code to set any schema/table/column filters.
Copy the column values to a new query window and execute. The script alone will not make any changes.
WHAT IT DOES:
Column 1 drops the constraints.
Column 2 creates the constraints, disabled. This is done to ensure the constraint definition is not lost if the data does not meet the constraint requirements.
Column 3 enables the constraint and checks all data in the table. This step may take some time on a table with lots of data. If the data complies with the constraint, the constraint will be "trusted" by SQL Server. If the data is not compliant, this step will fail. At that point you should fix the data and re-enable the constraint.
**DISCLAIMER
As always, backup your database before using the output of this script; this script is provided "as-is" with no support or guarentee of the results or fitness for production. Use at your own risk.
**
However, it works very well for me and has solved a number of SQL code deployment problems. I generally run this script after each promotion, just in case someone has created a new system named constraint.