Technical Article

Rename Foreign Key Constraints

,

This script will rename all FK constraint names in your database.  It will give them a user friendly name that can be used in error messages, etc. The FK contraint names will be formatted as FK_ChildTableName_ParentTableName_soid.

Note: You first need to enable 'Allow modifications to be made directly to the system catalogs'. This can be changed on the Server Settings tab of the SQL Server Properties window.

USE dbName

UPDATE sysobjects 
SET name= 'FK_' + Fks.ChildName + '_' + Fks.ParentName + '_' + cast(fks.soid as varchar(20))

FROM 
    (select so.id as SoId, 
            child.name as ChildName, 
            parent.name as ParentName
       from sysforeignkeys sfk
            inner join sysobjects so on sfk.constid = so.id
            inner join sysobjects child on sfk.fkeyid = child.id
            inner join sysobjects parent on sfk.rkeyid = parent.id
    ) As Fks
WHERE id = Fks.SoId
  and xtype = 'F'

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating