Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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,

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)

**

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.

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 4 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 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: 712 | Views in the last 30 days: 2
 
Related Articles
SCRIPT

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

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

SCRIPT

Conversion of rule objects to column check constraints

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

SCRIPT

Check Candidate Columns for NOT NULL Constraints

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

FORUM

constraints on columns while creating the table

constraints on columns while creating the table

SCRIPT

Fix Column Check Constraint Naming

This stored procedure can be used for applying a custom column check constraint naming convention

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones