I have a database that was recently migrated from Oracle to SQL Server 2017. Whoever initially setup the database set all the foreign keys to be system generated names. In other words if I look at the foreign keys in a table they are named (SYS_C005214
, SYS_C005271
, SYS_C005272
). I would prefer to have these all renamed to:
FK_ChildTable_childColumn_ParentTable_parentColumn
I would like to setup a script that will change all of the foreign keys to the approprite name. Is there a way to do this without having to go into each key to see it's definition and then renaming it by hand?
Note: I now realize after running Aaron Bertrand's script, I also need this for primary keys.
Best Answer
Stealing from myself (please see the caveats and comments on that article):