Syntax for renaming a foreign key with sp_rename

Assuming the following tables:

CREATE TABLE Shopping.Product (CategoryID INT NOT NULL)
CREATE TABLE Shopping.Category (CategoryID INT PRIMARY KEY)

ALTER TABLE Shopping.Product
    ADD CONSTRAINT FK_Product_Caetgory FOREIGN KEY(CategoryID)
    REFERENCES Shopping.Category

There is a typo in the foreign key name. How would you fix this using sp_rename? Is it…

-- 1. Just the object name as seen in sys.objects:
sp_rename 'FK_Product_Caetgory', 'FK_Product_Category', 'OBJECT'

-- 2. Qualified with  the schema:
sp_rename 'Shopping.FK_Product_Caetgory', 'FK_Product_Category', 'OBJECT'

-- 3. Qualified with the schema and table:
sp_rename 'Shopping.Product.FK_Product_Caetgory', 'FK_Product_Category', 'OBJECT'

If you picked #2, you are correct. This took me 10 mins to figure out.

October 22, 2009

2 Comments

Javier Lope de Barrios on September 8, 2011 at 5:42 pm.

Thanks! The MS documentation for sp_rename is not clear in this regard.

douglas on December 21, 2011 at 3:35 pm.

thank you for that – i can stop banging my head against the wall now

Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>