SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Rename Foreign Key Constraints

By Wes Henriksen,


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.


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



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.


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.


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.

Total article views: 1302 | Views in the last 30 days: 2
Related Articles

Columns Explorer - With constraints create / drop scripts (no cursors)

Gets all columns infos, foreign keys (multicolumn fk handled), referenced primary keys, default and ...


Check Candidate Columns for NOT NULL Constraints

This script reports table columns that allow NULL values but that currently have no NULL values, mak...


Conversion of rule objects to column check constraints

This script can convert the usage of bound rule objects in tables to column check constraints


Column and table constraints

Column and Table Constraints Constraints can be column constraints or table constraints.A column co...


Script Task In SSIS to Check column with Same value and change it accordingly

SSIS Script Task for Changing column values