tags:

views:

34

answers:

2

I renamed a table in my database with

EXEC sp_rename 'tblOldAndBusted', 'tblNewAndShiny'

and all the foreign key constraints were updated to the new table name, but they're still named based on the old table name. For example, I now have FK_tblOldAndBusted_tblTastyData but I'd like to have FK_tblNewAndShiny_tblTastyData.

Is there an easy way to script this?

Also, am I being too anal? I know the database works fine with the old table name in the constraints, but it feels like broken windows.

+1  A: 

Try:

exec sp_rename 'FK_tblOldAndBusted_tblTastyData', 'FK_tblNewAndShiny_tblTastyData', 'object'

Also, there is a bug regarding renaming such things when you deal with non-default schema.

Cannot rename a default constraint for a table in a schema which is not dbo by rsocol @Microsoft Connect

Denis Valeev
A: 

If there are too many to rename, how about export to dump, edit the dump in any text editor of choice to replace the table name, and then restore from dump? I mean export the dump of only constraints, not all.

Ashish Patil
Interesting idea, but in my situation I have to create a script that can be used to make the same changes on other copies of the database. Incidentally, its less than 10 tables, so not a huge deal.
Matt Casto
Ok. I had to use such method when one of my MySQL DB was migrated from Windows to Amazon RDS. We has all table names in upper case, while the DUMP we got from windows had all lower case. And on RDS, and under linux, MySQL table names are case sensitive.
Ashish Patil